Woocommerce - 如何通过订单节省购物车重量?

时间:2013-10-18 16:30:41

标签: php wordpress woocommerce

我需要为每个订单保存购物车重量,以便稍后检索以生成UPS标签。我有这个工作,但我升级了Woocommerce,现在已经破产了。

我可以确认它收到了order_id,但$woocommerce->cart->cart_contents_weight没有返回任何内容。

我是否试图在清空后获得推车重量?结账过程中的什么时候会创建$order_id(又名$post)?

add_action('woocommerce_checkout_update_order_meta', 'add_cart_weight');

function add_cart_weight( $order_id ) {
    global $woocommerce;
    $weight = $woocommerce->cart->cart_contents_weight;
    update_post_meta($order_id, '_cart_weight', $weight);
}

1 个答案:

答案 0 :(得分:1)

WC_Cart Docs提示有刷新总计的功能,请尝试在$weight之前调用此功能:

<?php
    function add_cart_weight( $order_id ) {
        global $woocommerce;
        $woocommerce->cart->calculate_totals();
        $weight = $woocommerce->cart->cart_contents_weight;
        update_post_meta($order_id, '_cart_weight', $weight);
    }
?>
  

“我是否试图在清空后获得推车重量?”

不幸的是,该功能只是想在添加新项目后增加整个购物车重量的新总数,然后通过update_post_meta列标题{{1}更新数据库中的总重量记录和meta_id的{​​{1}}。

相关问题