【问题标题】:Two different set of discounts that should be applied with respect to 2 different categories in woocommerce应针对 woocommerce 中的 2 个不同类别应用两组不同的折扣
【发布时间】:2019-02-24 17:23:41
【问题描述】:

这是与 woocommerce 集成的 WordPress 网站。在这家商店中,它们是两个主要类别 Category1 和 Category2。

我想针对 2 个不同的类别设置两组不同的折扣。

例如:

类别 1 的场景:

  1. 购买 $500-$999 可获得 5% 折扣
  2. 购买 $1000-$2000 可获得 15% 的折扣
  3. 购买 $2001-$5000 即可享受 25% 的折扣

类别 2 的场景:

  1. 购买 $500-$999 即可享受 20% 的折扣
  2. 购买 $1000-$2000 可获得 40% 的折扣
  3. 购买 $2001-$5000 可获得 60% 的折扣

考虑一下,如果我从 Category1 购买价值 1000 美元的 product1 和 product2,从 Category2 购买 product5 和 product6,价值 4000 美元。

所以总结购物车应该如下所示:

    Cart subtotal:                       $5000
    Category 1 Discount (15%) of $1000: -$150
    Category 2 Discount (60%) of $4000  -$2400
    Amount to Pay:                       $2450

应根据每个类别的小计应用折扣。我尝试了很多插件并自定义了代码,但我无法在 woocoomerce 中实现功能。

您能否建议任何插件或自定义代码来实现此功能?

更新:

我已经根据线程Auto apply a percentage or fixed cart discount based on the total in WooCommerce 尝试了以下代码。

尝试根据 2 个条件应用累进折扣。

  1. 基于购物车价值
  2. 关于特定类别。

但是尝试的代码不起作用。我需要在此代码中更改什么?我需要为 2 个不同的类别设置累进折扣。

add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_wise_discount', 10, 1 );

    function cart_items_quantity_wise_discount($cart_object) {

        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

        // Set HERE your category (can be an ID, a slug or the name)
        $category = '224'; // our category id

        $category_count = 0;
        $category_total = 0;
        $discount = 0;

        // Iterating through each cart item
        foreach($cart_object->get_cart() as $cart_item):
        //print_r($cart_item);exit;
            if( has_term( $category, 'product_cat', $cart_item['product_id']) ):
                $category_count += $cart_item['quantity'];
                $category_total += $cart_item["line_total"]; // calculated total items amount (quantity x price)
            endif;

        endforeach;

        $discount_text = __( 'Quantity discount of ', 'woocommerce' );

        if ( $category_total >=2000 && $category_total <=2999 ) {
            $discount -= $category_total * 0.3; // Discount of 10% 
            $discount_text_output = $discount_text . '10%';
        } 
        elseif ( $category_total >=3000 && $category_total <=4999 ) {
            $discount -= $category_total * 0.15; // Discount of 15%
            $discount_text_output = $discount_text . '15%';
        }
    // Adding the discount
        if ( $discount != 0 && $category_count >= 12 )
            $cart_object->add_fee( $discount_text_output, $discount, false );

        // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }

第二次更新(版本 2) - 我已经尝试了以下代码。但是我的结帐页面上的类别折扣不受影响。你能看看这个以及我在这段代码中做错了什么吗?

add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE set your 2 product category term names
    $category1 = 'Appliances';
    $category2 = 'Electronics';

    $total1 = $total2 = $percentage1 = $percentage2 = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( has_term( $category1, 'product_cat', $cart_item['product_id']) ) {
            $total1 += $cart_item["line_total"]; // Excluding taxes
        } elseif( has_term( $category2, 'product_cat', $cart_item['product_id']) ) {
            $total2 += $cart_item["line_total"]; // Excluding taxes
        }
    }

    // All Amounts need to be set without taxes

    // First category "Appliances" progressive percentage discount
    if ( $total1 >= 1000 && $total1 < 1500 ) { // <== set excluding taxes 
        $percentage1 = 5;
    }elseif ( $total1 >= 1500 && $total1 < 2500 ) { // <== set excluding taxes 
        $percentage1 = 10;
    }elseif ( $total1 >= 2500 ) { // <== set excluding taxes amounts
        $percentage1 = 15;
    }

    // Second category "Electronics" progressive percentage discount
    if ( $total2 >= 1000 && $total2 < 1500 ) { // <== set excluding taxes amounts
        $percentage2 = 10;
    }elseif ( $total2 >= 1500 && $total2 < 2500 ) { // <== set excluding taxes amounts
        $percentage2 = 15;
    }elseif ( $total2 >= 2500 ) { // <== set excluding taxes amounts
        $percentage2 = 20;
    }

    // Set the first discount for "Appliances"
    if( $percentage1 > 0 ){
        $discount1   = $total1 * $percentage1 / 100;
        $label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
            $category1, $percentage1 . '%', strip_tags(wc_price($total1)));
        $cart->add_fee( $label_text1, -$discount1 );
    }

    // Set the Second discount for "Electronics"
    if( $percentage2 > 0 ){
        $discount2   = $total2 * $percentage2 / 100;
        $label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
            $category2, $percentage2 . '%', strip_tags(wc_price($total2)));
        $cart->add_fee( $label_text2, -$discount2 );
    }

    // Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}

【问题讨论】:

    标签: wordpress woocommerce discount


    【解决方案1】:

    @Vinayagam - 请在您的子主题functions.php 中使用以下代码。我已经测试并且工作正常。

        add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
    
        function cart_items_quantity_multiple_discounts( $cart ) {
            if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                return;
    
            // HERE set your 2 product category term names
            $category1 = 250;//Appliances
            $category2 = 224;//Electronics
    
            $pdt_id_1 = $pdt_id_2 = $total1 = $total2 = $percentage1 = $percentage2 = 0;
            //$pdt_id_1 - Category One;
            //$pdt_id_2 - Category Two;
    enter code here
            // Loop through cart items
            foreach( $cart->get_cart() as $cart_item ) {
                $_product = $cart_item['data'];
                $terms = get_the_terms( $_product->id, 'product_cat' );
                foreach ($terms as $term) {
                    $_categoryid = $term->parent;
                    if ( $_categoryid === $category1 and $pdt_id_1!=$_product->id) {
                        $total1 += $cart_item["line_total"];
                        $pdt_id _1= $_product->id;
                    }
                    else if ( $_categoryid === $category2 and $pdt_id_2!=$_product->id) {
                        $total2 += $cart_item["line_total"];
                        $pdt_id_2 = $_product->id;
                    }
                }
            }
    
            // All Amounts need to be set without taxes
    
            // First category "Appliances" progressive percentage discount
            if ( $total1 >= 2000 && $total1 <= 2999 ) {
                $percentage1 = 3;
            }elseif ( $total1 >= 3000 && $total1 <= 4999 ) {
                $percentage1 = 5;
            }elseif ( $total1 >= 5000 && $total1 <= 9999) {
                $percentage1 = 8;
            }elseif ( $total1 >= 10000 && $total1 <= 14999) {
                $percentage1 = 10;
            }elseif ( $total1 >= 15000 && $total1 <= 24999) {
                $percentage1 = 13;
            }elseif ( $total1 >= 25000 && $total1 <= 29999) {
                $percentage1 = 15;
            }elseif ( $total1 >= 30000 && $total1 <= 49999) {
                $percentage1 = 18;
            }elseif ( $total1 >= 50000 && $total1 <= 59999) {
                $percentage1 = 19;
            }elseif ( $total1 >= 60000 && $total1 <= 499999) {
                $percentage1 = 20;
            }
    
            // Second category "Electronics" progressive percentage discount
            if ( $total2 >= 2000 && $total2 <= 2999 ) {
                $percentage2 = 5;
            }elseif ( $total2 >= 3000 && $total2 <= 4999 ) {
                $percentage2 = 10;
            }elseif ( $total2 >= 5000 && $total2 <= 9999 ) {
                $percentage2 = 15;
            }elseif ( $total2 >= 10000 && $total2 <= 14999 ) {
                $percentage2 = 20;
            }elseif ( $total2 >= 15000 && $total2 <= 24999 ) {
                $percentage2 = 25;
            }elseif ( $total2 >= 25000 && $total2 <= 29999 ) {
                $percentage2 = 30;
            }elseif ( $total2 >= 30000 && $total2 <= 49999 ) {
                $percentage2 = 40;
            }elseif ( $total2 >= 50000 && $total2 <= 59999 ) {
                $percentage2 = 45;
            }elseif ( $total2 >= 60000 && $total2 <= 499999 ) {
                $percentage2 = 50;
            }
    
            // Set the first discount for "Appliances"
            if( $percentage1 > 0 ){
                $discount1   = $total1 * $percentage1 / 100;
                $label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
                    'Appliances', $percentage1 . '%', strip_tags(wc_price($total1)));
                $cart->add_fee( $label_text1, -$discount1, true, '');
            }
    
            // Set the Second discount for "Electronics"
            if( $percentage2 > 0 ){
                $discount2   = $total2 * $percentage2 / 100;
                $label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
                    'Electronics', $percentage2 . '%', strip_tags(wc_price($total2)));
                $cart->add_fee( $label_text2, -$discount2 );
            }
            // Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多