Advertisement
ikamal7

thank you hook

Sep 27th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Kamal\Jom;
  4.  
  5. class GeneratePDF {
  6.     public function __construct() {
  7.         add_action( 'woocommerce_thankyou', 'get_order_data', 10, 1 );
  8.     }
  9.  
  10.     public function get_order_data( $order_id ) {
  11.  
  12.         if ( !$order_id ) {
  13.             return;
  14.         }
  15.  
  16.         // Getting an instance of the order object
  17.         $order = wc_get_order( $order_id );
  18.  
  19.         if ( $order->is_paid() ) {
  20.             $paid = 'yes';
  21.         } else {
  22.             $paid = 'no';
  23.         }
  24.  
  25.         // iterating through each order items (getting product ID and the product object)
  26.         // (work for simple and variable products)
  27.         foreach ( $order->get_items() as $item_id => $item ) {
  28.  
  29.             if ( $item['variation_id'] > 0 ) {
  30.                 $product_id = $item['variation_id']; // variable product
  31.             } else {
  32.                 $product_id = $item['product_id']; // simple product
  33.             }
  34.  
  35.             // Get the product object
  36.             $product = wc_get_product( $product_id );
  37.  
  38.         }
  39.         print_r( $order );
  40.  
  41.         // Ouptput some data
  42.         echo '<p>Order ID: ' . $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement