【问题标题】:WooCommerce - Conditional Cart calculations based on subcategoriesWooCommerce - 基于子类别的条件购物车计算
【发布时间】:2016-12-06 13:26:58
【问题描述】:

我有非常具体的项目,我需要一些不同的购物车规则。我找不到有关如何实现此目的的插件或任何其他资源。

我有子类别 1(即桌子)和子类别 2(即椅子)。用户只能从 subcategory Tables 中添加 1 个产品(这是强制性的),以及从 subcategory Chairs 中添加任意数量的产品(非强制性)。

我需要下一条规则:如果用户还添加了 椅子子类别中的产品,则从 椅子子类别中减去 椅子子类别产品的总价格strong>子类别表产品。同样在这种情况下,如果价格将

有谁知道如何使用标准的 Wordpress Woocommerce 做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce categories cart


    【解决方案1】:

    可以根据您的子类别要求和计算在购物车中添加折扣来完成这项工作......

    代码:

    add_action( 'woocommerce_cart_calculate_fees','table_chairs_cart_discount', 10, 1 );
    function table_chairs_cart_discount($cart_object) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Initializing variables
        $chairs_total = 0;
        $table_total = 0;
        $discount = 0;
    
        // Iterating through each cart item
        foreach($cart_object->get_cart() as $item_key => $item):
    
            $item_line_total = $item["line_total"]; // Item total price (price x quantity)
    
            // Chairs subcategory items
            if(has_term('chairs', 'product_cat', $item['product_id']))
                $chairs_total += $item_line_total;
    
            // Table subcategory items
            if(has_term('table', 'product_cat', $item['product_id']))
                $table_total += $item_line_total;
    
        endforeach;
    
        // ## CALCULATIONS ##
        if( $table_total <= $chairs_total && $chairs_total > 0 ) 
            $discount -= $table_total;
        elseif ($chairs_total > 0) 
            $discount -= $chairs_total;
    
        // Adding the discount
        if ($discount != 0)
            $cart_object->add_fee( __( 'Chairs discount', 'woocommerce' ), $discount, false );
            // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
    

    代码进入您活动的子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。


    相关回答:Discount for Certain Category Based on Total Number of Products

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 2019-02-02
      相关资源
      最近更新 更多