php - 如何在 Woocommerce 中内爆每个订单商品的一系列商品详细信息?

标签 php wordpress loops woocommerce orders

我正在尝试从订单中提取订单项数据并以特定格式将其分解。它现在仅检索订单中最后一项的详细信息。我需要在每组单独的项目详细信息之间使用管道分隔符。

感谢任何帮助。

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->get_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 ) {

    $wgProduct = '';
    $wgProduct .= '777777';
    $wgProduct .= '::' . $order->get_line_total( $item, true, true );
    $wgProduct .= '::' . $item->get_name();
    $wgProduct .= '::' . $item->get_id();
    }
    $wgItems[] = $wgProduct;
    $wgItemsList = implode('|', $wgItems); //this is where the problem is, I think
    
?>
<script>
    
        items:  '<?php echo $wgItemsList ?>', //this is where i want to call the items in a string


</script>

最佳答案

您的代码中有一些错误和复杂性,请尝试以下方法(已注释):

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_coupons(); // Get an array of WC_Coupon objects

    // Loop through the array of WC_Coupon Objects
    foreach( $coupons as $coupon ){
        // Do something with $coupon variable (WC_Coupon object)
        $coupon_code = $coupon->get_code(); // Get coupon code
    }

    $total_exc_shipping = $order_total - $shipping_total;
    $order_discount = $order->get_discount_total();
    
    $wgItems = array(); // Initializing
    
    // Loop through order "line items"
    foreach ( $order->get_items() as $item ) {
        $wgProduct = ''; // Initializing
        $wgProduct .= '777777';
        $wgProduct .= '::' . $order->get_line_total( $item, true, true );
        $wgProduct .= '::' . $item->get_name();
        $wgProduct .= '::' . $item->get_id(); // <== That is the item Id which has nothing to do with the product id: $item->get_product_id();
    
        $wgItems[] = $wgProduct; // <== Moved inside the foreach loop
    }
    $wgItemsList = implode('|', $wgItems); // Now this works
    
    ?>
    <script>
       var items = '<?php echo $wgItemsList ?>'; //this is where i want to call the items in a string
    </script>

}

现在就可以了。

注意:要从订单项中获取产品 ID,请使用 get_product_id(),而不是 get_id(),后者给出 item_id 记录在相关数据库表中。


相关:

关于php - 如何在 Woocommerce 中内爆每个订单商品的一系列商品详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226681/

相关文章:

php - 使用 mysqli 插入数据

javascript - 带有 Javascript 的 Wordpress 插件

c++ - 检查值是否存在于数组的所有索引中

javascript - 根据对象数组中的重复值将对象分组

php - 获取多个数组中出现的值

php - MVC模式上的Controller和Martin Fowler描述的Page Controller模式一样吗?

php - Woocommerce:一次删除所有表单标签

wordpress - 用户注册后创建产品

javascript - AngularJS 检查对象数组中的字段是否存在

php - 如何在cakephp 3中保存多条记录