修改WooCommerce购物车中的运输尺寸

时间:2018-01-31 02:46:38

标签: wordpress woocommerce

我正在尝试根据元数据修改WooCommerce购物车中的Shipping Dimensions。我需要帮助才能实现这一目标。元数据来自名为TM Global Product Options的WooCommerce插件。

add_action( 'woocommerce_before_calculate_totals', 'modify_cart', 10, 1);
function modify_cart( $cart_object ) {

    if ( (is_admin() && ! defined( 'DOING_AJAX' ) ) || $cart_object->is_empty() )
        return;

    foreach ( $cart_object->get_cart() as $cart_item ) {

        $product_id = $cart_item['product_id'];

        $data = get_post_meta( $product_id, '_tmcartepo_data'); 

        $cart_item['data']->set_width( $data['shipping_width'] );
        $cart_item['data']->set_height( $data['shipping_width'] );
        $cart_item['data']->set_length( $data['shipping_length'] );

    }
}

1 个答案:

答案 0 :(得分:0)

我必须做完全相同的事情。 WooCommerce将产品的更改保存在称为“更改”的属性中。为了应用它们,您必须在购物车项目上运行“ apply_changes”方法。我在下面修改了您的代码:

add_action( 'woocommerce_before_calculate_totals', 'modify_cart', 10, 1);
function modify_cart( $cart_object ) {

    if ( (is_admin() && ! defined( 'DOING_AJAX' ) ) || $cart_object->is_empty() )
        return;

    foreach ( $cart_object->get_cart() as $cart_item ) {

        $product_id = $cart_item['product_id'];

        $data = get_post_meta( $product_id, '_tmcartepo_data'); 

        $cart_item['data']->set_width( $data['shipping_width'] );
        $cart_item['data']->set_height( $data['shipping_width'] );
        $cart_item['data']->set_length( $data['shipping_length'] );

        if( !empty( $cart_item['data']->get_changes() ) )
           $cart_item['data']->apply_changes();

    }
}