【问题标题】:Woocommerce on Thank You Page get category to order_id感谢页面上的 Woocommerce 将类别添加到 order_id
【发布时间】:2017-10-24 10:05:15
【问题描述】:

我有一个来自 sendinblue 的跟踪代码 javascript。

我想在此站点中启动“自动化”以进行分段,为此,我必须使用其 API 将数据从我的站点发送到 sendinblue。

在 Wocommerce 的“谢谢”页面中,我需要知道客户购买的产品类别,并通过 JS 将这些数据发送给 sendinblue。

我已经把这段代码放在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
}
?>

当我模拟订单并查看 HTML 代码时,数组(用于发送的类别)为空。

    <script language="text/javascript">
sendinblue.track('categoria_acquisto', {    
  'categoria':'[{category:""}]';  
});
</script>

【问题讨论】:

    标签: php woocommerce hook-woocommerce sendinblue


    【解决方案1】:

    不确定您使用的是哪个版本的 WooCommerce,但对于 WC > 3.0,您可以使用类似:

    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 } ?>
    

    【讨论】:

    • 嗨,它返回一个空数组 不工作
    • 你的产品有分类吗? $product_js 仅是空的,如果没有订单项目有类别。对多个产品和订单进行了测试
    • 非常感谢。我没有说 php 代码是错误的,而是说数组是空的。也许是传递数组值的 JS 代码没有很好地通信。您对如何修改它有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2021-01-27
    • 2023-03-20
    • 2018-11-05
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多