2

I have a Tracking Code javascript from sendinblue.

I want to start an "automation" in this site for segmentation and, for that, I must send data from my site to sendinblue using its API.

In Wocommerce's "Thank You" page, I need to know the categories of products that a customer is buying and send this data to sendinblue via JS.

I have put this code in functions.php

add_action( 'woocommerce_thankyou', 'analisi' );    

function analisi($order_id) {
    $order = new WC_Order($order_id);   
    $items = $order->get_items();        

    foreach ($items as $item_id => $item_data)
    {
        //getting product object
        $_product = wc_get_product($item_data['item_meta']['_product_id'][0]);

        //getting all the product category
        $pro_cat_array = wp_get_post_terms($_product->ID, 'product_cat');

        $pro_cat = implode(',', $pro_cat_array);
        //storing all the line item as a string form
        $product_js[] = '{category:"' . $pro_cat . '"}';
    }
?>
    <!-- Paste Tracking Code Under Here -->
<script language="text/javascript">
sendinblue.track('categoria_acquisto', {    
  'categoria':'[<?= implode(',', $product_js) ?>]';  
});
</script>
<?php
}
?>

When I simulate the order and look at the HTML code the array (for sent categories) is empty.

    <script language="text/javascript">
sendinblue.track('categoria_acquisto', {    
  'categoria':'[{category:""}]';  
});
</script>
Grokify
  • 15,092
  • 6
  • 60
  • 81

1 Answers1

0

Not sure which version of WooCommerce you are using, but for WC > 3.0, you can use something like:

add_action( 'woocommerce_thankyou', 'analisi' );
function analisi( $order_id ) {
    $order      = wc_get_order( $order_id );
    $items      = $order->get_items();
    $product_js = array();

    /**
     * @var WC_Order_Item_Product $item_data
     */
    foreach ( $items as $item_id => $item_data ) {
        $product    = $item_data->get_product();
        $categories = array();

        $terms = get_the_terms( $product->get_id(), 'product_cat' );

        if ( is_wp_error( $terms ) || empty( $terms ) ) {
            continue;
        }

        foreach ( $terms as $term ) {
            $categories[] = $term->name;

            // Comment out the above and use the line below to have category IDs instead
            // $categories[] = $term->term_id;
        }

        $categories_list = implode( ',', $categories );
        if ( $categories_list ) {
            $product_js[] = '{category:"' . $categories_list . '"}';
        }
    }
    ?>
    <!-- Paste Tracking Code Under Here -->
    <script language="text/javascript">
        sendinblue.track('categoria_acquisto', {
            'categoria': '[<?php echo implode( ',', $product_js ) ?>]';
        })
        ;
    </script>
<?php } ?>
  • Hi, it return an empty array not working – Giandomenico Oct 24 '17 at 11:14
  • And your product does have categories? The `$product_js` is only empty, if none of the order items have categories. Did test with multiple products and orders – VanboDevelops Oct 24 '17 at 12:36
  • many many thank. I did not say the php code is wrong but that the array is empty. Perhaps it is the JS code that passes the array value that does not communicate well. Do you have any suggestions on how to modify it? – Giandomenico Oct 24 '17 at 13:58