【问题标题】:Custom cart item weight in WoocommerceWoocommerce 中的自定义购物车商品重量
【发布时间】:2019-04-23 09:06:56
【问题描述】:

我正在创造尺寸和重量差异很大的产品。我没有使用 WooCommerce 产品变体来帮助我解决这个问题,因为我的产品是高度定制的。我正在使用第三方插件从许多变体选项中进行选择,并且我计划检查这些选项以确定产品重量。

问题是我想在将产品添加到购物车之前设置产品重量。然后,购物车上的运费计算器将根据您购物车的总重量计算您的运费(WooCommerce 基于重量的运费)。

我已经非常广泛地尝试了 2 个 WooCommerce 钩子来做到这一点,但没有运气。

add_filter( 'woocommerce_add_cart_item_data', 'update_weight_on_add_to_cart', 10, 3);

add_action( 'woocommerce_add_to_cart', 'update_weight_on_add_to_cart', 10, 6 );

我可以使用 add_filter 设置自定义字段。这样做我可以看到我成功地将产品重量设置为新的。但是一旦我进入购物车页面,重量就会恢复到编辑产品页面上设置的重量。

这是我目前用来检查的功能:

function update_weight_on_add_to_cart( $cart_item_data, $product_id, $variation_id )
{
    $product = wc_get_product( $product_id );

    $weight1 = $product->get_weight();

    $product->set_weight(3.45);
    $weight2 = $product->get_weight();

    $cart_item_data['weight1'] = $weight1;
    $cart_item_data['weight2'] = $weight2;

    return $cart_item_data;
}

然后我在购物车上显示这些字段,看看我是否使用以下钩子和函数成功更改了数据。 (我从博客文章或其他 StackOverflow 文章中获取了大部分内容)

add_filter( 'woocommerce_get_item_data', 'display_weight', 10, 2 );
function display_weight( $item_data, $cart_item ) {
    $item_id = $cart_item['variation_id'];
    if($item_id == 0) $item_id = $cart_item['product_id'];
    $product_qty = $cart_item['quantity'];
    $product = wc_get_product($item_id);
    $weight3 = $product->get_weight();

    $item_data[] = array(
        'key'       => __('Weight3', 'woocommerce'),
        'value'     => wc_clean( $weight3 ),
        'display'   => ''
    );
     $item_data[] = array(
        'key'     => __( 'Weight1', 'woocommerce' ),
        'value'   => wc_clean( $cart_item['weight1'] ),
        'display' => ''
    );
     $item_data[] = array(
        'key'     => __( 'Weight2', 'woocommerce' ),
        'value'   => wc_clean( $cart_item['weight2'] ),
        'display' => ''
    );

    return $item_data;
}

我最近使用上述代码的努力在购物车页面上产生了以下结果。

权重3: 1

重量1: 1

体重2: 3.45

任何帮助将不胜感激!如果不能以这种方式完成,有没有更好的方法来解决这个问题?也许这需要在购物车上完成?

【问题讨论】:

    标签: php wordpress woocommerce properties cart


    【解决方案1】:

    有一些错误、错误和遗漏的部分……这是解决方法(适用于所有产品类型,包括产品变体)

    // make and save a calculated weight as custom cart item data
    add_filter( 'woocommerce_add_cart_item_data', 'add_weight_custom_cart_item_data', 10, 3 );
    function add_weight_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
        // For product variations handling
        $product_id = $variation_id > 0 ? $variation_id : $product_id;
    
        // Get an instance of the product object
        $product = wc_get_product( $product_id );
    
        // The default product weight
        $cart_item_data['weight']['default'] = $product->get_weight();
    
        ## ====> HERE YOU CAN MAKE YOUR WEIGHT CALCULATIONS <==== ##
        $new_weight_value = 3.45;
    
        // Set the new calculated weight
        $cart_item_data['weight']['new'] = 3.45;
    
        return $cart_item_data;
    }
    
    add_filter( 'woocommerce_get_item_data', 'display_cart_item_weight', 10, 2 );
    function display_cart_item_weight( $item_data, $cart_item ) {
        if ( isset($cart_item['weight']) ) {
            // Display original weight
            if ( isset($cart_item['weight']['default']) ) {
                $item_data[] = array(
                    'key'       => __('Weight (original)', 'woocommerce'),
                    'value'     => wc_format_weight( $cart_item['weight']['default'] ),
                );
            }
    
            // Display calculated weight
            if ( isset($cart_item['weight']['new']) ) {
                $item_data[] = array(
                    'key'     => __( 'Weight (new)', 'woocommerce' ),
                    'value'   => wc_format_weight( $cart_item['weight']['new'] ),
                );
            }
        }
        return $item_data;
    }
    
    // Set the new weight in cart items
    add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_weight', 25, 1 );
    function set_custom_cart_item_weight( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        foreach( $cart->get_cart() as $cart_item ){
            if ( isset($cart_item['weight']['new']) ) {
                $cart_item['data']->set_weight($cart_item['weight']['new']);
            }
        }
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 你好!有没有办法使用 set_weight 改变实际重量?你的代码能做到吗?我想确保此代码与我计划实施的名为 WooCommerce Weight Based Shipping 的插件很好地配合。再次感谢。
    • $new_weight_value = 3.45; 是您计算的重量(在第一个函数中)...它在第三个函数中设置:$cart_item['data']-&gt;set_weight($cart_item['weight']['new']); ... 所以在第一个函数中您计算重量...
    • 嘿,我不是故意让你挂的,但我上周正在度假,直到今天早上才有机会尝试你的代码。此外,我想再次感谢您的工作。该代码完全按预期工作,并且也可以与我提到的插件一起使用。再次感谢,马修
    • 这个答案很棒——我用它来计算与原始重量相比的体积重量(使用产品上的尺寸)。但是,我需要将其更改为使用客户在产品页面上输入的重量、长度、宽度和高度,而不是产品详细信息中的内容(就像我有一个“命名你的价格”插件一样) )。产品页面上尚​​不存在这些字段...请问您可以帮助我吗?如果不是那么没有问题。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 2019-12-21
    • 2019-02-16
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2019-10-08
    • 2015-06-01
    相关资源
    最近更新 更多