I’m trying to send price, name and id of each item in the order, plus an extra static value to a single js variable formatted in this way,
items = eventID::price::name|eventID::price::name|eventID::price::name
I was trying to do it this way, but I’m getting an error on the last line which I mean to add a pipe if there is more than one product.
$line_items = $order->get_items(); //loop over line items $wgItemCount = $order->get_item_count(); $wgItems = array(); foreach ( $line_items as $item ) { $wgItem = array(); $wgItem ['eventId'] = '777777'; $wgItem ['eventId'] .= '::'; $wgItem ['price'] = $order->get_line_total( $item, true, true ); $wgItem ['price'] .= '::'; $wgItem ['name'] = $item->get_name(); if ($wgItemCount > 1) { //add divider if more than one item wgItem .= '|'; } $wgItems[] = $wgItem; }
That hasn’t worked so far, so I thought maybe I should create a custom object that contains the variables instead:
wgProduct = "77777", $order->get_line_total( $item, true, true ), $item->get_name();
and then call the items in a list later like
echo print ("|", wgProduct)
I had also tried using JSON to encode the data but the third party told me that is not going to work for their needs.
Edit: Code is now doing everything I need it to, but it’s only pulling one item from the list instead of a string of all of them.
Here’s what it looks like now based on the help I got here:
add_action( 'woocommerce_thankyou','wg_tracking' ); 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->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 ) { $line = ''; $line .= '77777'; $line.= '::' . $order->get_line_total( $item, true, true ); $line .= '::' . $item->get_name(); $line .= '::' . $item->get_id(); } $wgItems[] = $line; $itemsList = implode('|', $wgItems); ?> <script> (function(w,e,b,g,a,i,n,s){w['ITCVROBJ']=a;w[a]=w[a]||function(){ (w[a].q=w[a].q||[]).push(arguments)},w[a].l=1*new Date();i=e.createElement(b), n=e.getElementsByTagName(b)[0];i.async=1;i.src=g;n.parentNode.insertBefore(i,n) })(window,document,'script','https://analytics.webgains.io/cvr.min.js','ITCVRQ'); ITCVRQ('set', 'trk.programId', 88888); ITCVRQ('set', 'cvr', { value: '<?php echo $total_exc_shipping ?>', currency: '<?php echo $currency ?>', language: 'de_DE', eventId: 77777, orderReference : '<?php echo $order_id ?>', comment: '', multiple: '', checksum: '', items: '<?php echo $itemsList ?>', voucherId: '<?php if ( $order_discount > 0 ) echo $coupon->get_code(); ?>' }); ITCVRQ('conversion'); </script> <?php } ?>
Advertisement
Answer
I think this all can be simplified to just use array’s implode()
method.
Below are what I would do in your situation:
$wgItems = array(); foreach ( $line_items as $item ) { $line = ''; $line .= '777777'; $line .= '::' . $order->get_line_total( $item, true, true ); $line .= '::' . $item->get_name(); $wgItems[] = $line; } $items = implode('|', $wgItems);