2

I am trying to pull line item data from the order and implode it in a specific format. It's only retrieving the details of the last item in the order right now. I need a pipe separator between each individual set of item details.

I appreciate any help.

function wg_tracking( $order_id ) {
$order = wc_get_order( $order_id );

$shipping_total =  $order->get_shipping_total();
$order_total = $order->get_total();
$currency = $order->get_currency();
$coupons = $order->get_coupon_codes();
$items = $order->get_items();
$total_exc_shipping = $order_total - $shipping_total;
$order_discount = $order->get_discount_total();

foreach( $coupons as $coupon ){
    $coupon_post_object = get_page_by_title($coupon, OBJECT, 'shop_coupon');
    $coupon_id = $coupon_post_object->ID;

    $coupon = new WC_Coupon($coupon_id);
}


$wgItems = array();   

foreach ( $items as $item ) {

    $wgProduct = '';
    $wgProduct .= '777777';
    $wgProduct .= '::' . $order->get_line_total( $item, true, true );
    $wgProduct .= '::' . $item->get_name();
    $wgProduct .= '::' . $item->get_id();
    }
    $wgItems[] = $wgProduct;
    $wgItemsList = implode('|', $wgItems); //this is where the problem is, I think
    
?>
<script>
    
        items:  '<?php echo $wgItemsList ?>', //this is where i want to call the items in a string


</script>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
jobroni889
  • 55
  • 5

2 Answers2

3

There are some mistakes and complications in your code, try the following instead (commented):

function wg_tracking( $order_id ) {
    $order = wc_get_order( $order_id );
    
    $shipping_total =  $order->get_shipping_total();
    $order_total = $order->get_total();
    $currency = $order->get_currency();
    $coupons = $order->get_coupons(); // Get an array of WC_Coupon objects

    // Loop through the array of WC_Coupon Objects
    foreach( $coupons as $coupon ){
        // Do something with $coupon variable (WC_Coupon object)
        $coupon_code = $coupon->get_code(); // Get coupon code
    }

    $total_exc_shipping = $order_total - $shipping_total;
    $order_discount = $order->get_discount_total();
    
    $wgItems = array(); // Initializing
    
    // Loop through order "line items"
    foreach ( $order->get_items() as $item ) {
        $wgProduct = ''; // Initializing
        $wgProduct .= '777777';
        $wgProduct .= '::' . $order->get_line_total( $item, true, true );
        $wgProduct .= '::' . $item->get_name();
        $wgProduct .= '::' . $item->get_id(); // <== That is the item Id which has nothing to do with the product id: $item->get_product_id();
    
        $wgItems[] = $wgProduct; // <== Moved inside the foreach loop
    }
    $wgItemsList = implode('|', $wgItems); // Now this works
    
    ?>
    <script>
       var items = '<?php echo $wgItemsList ?>'; //this is where i want to call the items in a string
    </script>

}

Now it will work.

Note: to get the product id from an order item use get_product_id(), but not get_id() which gives the item_id as recorded on the related database tables.


Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Moving that last line inside the loop made the difference! Thank you greatly. – jobroni889 Oct 06 '20 at 14:11
  • 1
    @jobroni889 Also is better to use $order->get_coupons(); in your case as it seems that you want to use the WC_Coupon Object… – LoicTheAztec Oct 06 '20 at 14:13
  • I will try that too. The other part I'm not clear on from your code is this line: $order = wc_get_order( 167 ); Is that just a filler value? – jobroni889 Oct 06 '20 at 15:08
  • @jobroni889 Sorry that was an oversight from my tests… the correct code is `$order = wc_get_order( $order_id );` just like in your question. – LoicTheAztec Oct 06 '20 at 18:02
2

Have you tried the following? Assign the stored values to $wgItems array inside the loop. This should fix it on a quick look

foreach ( $items as $item ) {

    $wgProduct = '';
    $wgProduct .= '777777';
    $wgProduct .= '::' . $order->get_line_total( $item, true, true );
    $wgProduct .= '::' . $item->get_name();
    $wgProduct .= '::' . $item->get_id();
    $wgItems[] = $wgProduct;
    }
    
    $wgItemsList = implode('|', $wgItems); //this is where the problem is, I think
    
?>
melvin
  • 2,571
  • 1
  • 14
  • 37