简体   繁体   English

如何使用 json 编码格式化这个字符串?

[英]How do I format this string with json encode?

To fill the value "items" in the script I need to get the individual order item names and prices and list them in the following format: EventID::Price::Name::productID |要在脚本中填充值“items”,我需要获取单个订单项目名称和价格,并按以下格式列出它们: EventID::Price::Name::productID | EventID::Price::Name::productID事件 ID::价格::名称::产品 ID

I don't know exactly how to output them in the format outlined above.我不知道如何以上述格式输出它们。

My code (to be added to functions.php)我的代码(要添加到functions.php)

add_action( 'woocommerce_thankyou', 'wg_tracking' );

function wg_tracking( $order_id ) {

$order = wc_get_order( $order_id );    
$order_total = $order->get_total();
$code = $coupon->get_code();
$currency = $order->get_currency();

foreach ( $order->get_items() as $item_id => $item ) {
    $total = $item->get_total();
    $product_name = $item->get_name();
    $item_id = $item->get_id();

} $items_list = //something here to order the data?
}

?>

<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', 777777);
    ITCVRQ('set', 'cvr', {
        value: '<?php echo $order_total ?>',
        currency: '<?php echo $currency ?>',
        language: 'de_DE',
        eventId: 777777,
        orderReference : '<?php echo $order_id ?>',
        comment: '',
        multiple: '',
        checksum: '',
        items: '<?php echo SOMETHING ?>',
        voucherId: '<?php echo $code ?>',
    });
    ITCVRQ('conversion');
</script>

Based on the recommendation to use json encode, I've now got this.根据使用 json encode 的建议,我现在得到了这个。

add_action( 'woocommerce_thankyou', 'wg_tracking' );

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

//grab woocommerce objects
$order_total = $order->get_total();
$code = $coupon->get_code();
$currency = $order->get_currency();

// are there any coupons used?
$coupons = "";
$couponCount = 0;
foreach ($order->get_used_coupons() as $coupon) {
    $couponCount++;
    if($couponCount > 1) { // add comma if more than one coupon
        $coupons .= ',';
    } 
    $coupons .= $coupon;
}

// grab line items from the order
$line_items = $order->get_items();

//loop over line items
$wgItems = array();   
foreach ( $line_items as $item ) {
    $wgItem = array();
    $wgItem ['price'] = $order->get_line_total( $item, true, true );
    $wgItem ['name'] = $item->get_name();
    $wgItems[] = $wgItem;
}

}

?>

<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', 282105);
    ITCVRQ('set', 'cvr', {
        value: '<?php echo $order_total ?>',
        currency: '<?php echo $currency ?>',
        language: 'de_DE',
        eventId: 1064725,
        orderReference : '<?php echo $order_id ?>',
        comment: '',
        multiple: '',
        checksum: '',
        items:  '<?php echo json_encode($wgItems) ?>',
        voucherId: '<?php echo $coupons ?>',
    });
    ITCVRQ('conversion');
</script>

You can serialize the values to a string and encode with JSON.您可以将值序列化为字符串并使用 JSON 进行编码。json_encode should do the trick.json_encode应该可以解决问题。

<script>
  const data = <?= json_encode($YOUR_ARRAY) ?>
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM