【问题标题】:Set max weight per cart item in Woocommerce在 Woocommerce 中设置每个购物车项目的最大重量
【发布时间】:2018-04-10 09:18:00
【问题描述】:

如何设置每个产品(不是每个订单)的最大重量?

客户可以购买任意数量(数量)的产品,直到达到最大重量。如果他购买 5 个产品,每个产品的总单位重量不能达到最大重量。一个订单中可以有许多产品,但每个产品的总单位(数量)具有最大重量。

例如,在一个订单中:

  • 3件A产品(总重2kg)
  • 5件B产品(总重1.4kg)
  • 3件C产品(总重2kg)

【问题讨论】:

    标签: wordpress woocommerce attributes cart hook-woocommerce


    【解决方案1】:

    在此示例中,挂钩函数在添加到购物车操作时触发,您可以执行各种检查以验证或不验证操作(并显示自定义错误消息)

    所以你会看到你可以单独定位总物品重量……

    add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 20, 5 );
    function custom_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
        // HERE define the weight limit per item
        $weight_limit = 2; // 2kg
    
        $total_item_weight = 0;
    
        // Check cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            $item_product_id = empty($variation_id) ? $product_id : $variation_id;
    
            // If the product is already in cart
            if( $item_product_id == $cart_item['data']->get_id() ){
                // Get total cart item weight
                $total_item_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
            }
        }
    
        // Get an instance of the WC_Product object
        $product = empty($variation_id) ? wc_get_product($product_id) : wc_get_product($variation_id);
    
        // Get total item weight
        $total_item_weight += $product->get_weight() * $quantity;
    
        if( $total_item_weight > $weight_limit ){
            $passed = false ;
            $message = __( "Custom warning message for weight exceed", "woocommerce" );
            wc_add_notice( $message, 'error' );
        }
    
        return $passed;
    }
    

    您还需要一个附加的挂钩函数,该函数将在购物车商品数量更改时触发:

    add_filter( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
    function limit_cart_item_quantity( $cart_item_key, $new_quantity, $old_quantity, $cart ){
        // HERE define the weight limit per item
        $weight_limit = 2; // 2kg
        
        // Get an instance of the WC_Product object
        $product = $cart->cart_contents[ $cart_item_key ]['data'];
        
        $product_weight = $product->get_weight(); // The product weight
        
        // Calculate the limit allowed max quantity from allowed weight limit
        $max_quantity = floor( $weight_limit / $product_weight );
        
        // If the new quantity exceed the weight limit
        if( ( $new_quantity * $product_weight ) > $weight_limit ){
            
            // Change the quantity to the limit allowed max quantity
            $cart->cart_contents[ $cart_item_key ]['quantity'] = $max_quantity;
            
            // Add a custom notice
            $message = __( "Custom warning message for weight exceed", "woocommerce" );
            wc_add_notice( $message, 'notice' );
        }
    }
    

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

    【讨论】:

    • 非常感谢。我还发现,如果我增加购物车页面上的数量,它会向我显示警告信息并切换回之前的数量,这在某种程度上迫使买家保持在限制范围内,我喜欢它。
    • 但是如果我在单个产品页面上做同样的事情,即假设 2 个不同的产品重 2 公斤,总共 5 个单位,现在如果我添加产品 A(3 个单位),它会将我重定向到购物车页面,然后如果我添加产品 B(3 个单位 - 超重!),而不是再次显示该警告消息,它只是将我重定向到购物车页面(仅添加了以前的产品)。它会在我的单个产品页面上显示警告消息不能超过重量>2kg 并停止重定向到购物车页面。我的体重也设置为磅 (Ib),而不是公斤,请您解决这个问题。
    • 我试图为我的每个购物车项目更新自定义元数据,有两件事对我帮助很大。它们是:1)过滤器:add_filter( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 ); 和 2)更新购物车数据的方式:$cart->cart_contents[ $cart_item_key ]['quantity'] = $max_quantity;。感谢 TON 的分享!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2021-11-11
    • 2020-05-09
    相关资源
    最近更新 更多