3

我正在尝试将订单中每个项目的价格、名称和 ID 以及一个额外的静态值发送到以这种方式格式化的单个 js 变量,

items = eventID::price::name|eventID::price::name|eventID::price::name

我试图这样做,但我在最后一行出现错误,我的意思是如果有多个产品,则添加一个管道。

$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;
}

到目前为止还没有奏效,所以我想也许我应该创建一个包含变量的自定义对象:

wgProduct = "77777", $order->get_line_total( $item, true, true ), $item->get_name();

然后稍后调用列表中的项目,例如

echo print ("|", wgProduct) 

我也尝试过使用 JSON 对数据进行编码,但第三方告诉我这不能满足他们的需求。

编辑:代码现在正在做我需要做的一切,但它只是从列表中拉出一个项目,而不是所有项目的字符串。

根据我在这里得到的帮助,这就是现在的样子:

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

1 回答 1

2

我认为这一切都可以简化为只使用数组的implode()方法。以下是我在您的情况下会做的事情:

$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);
于 2020-09-22T03:25:09.110 回答