根据 Woocommerce 中的产品类别提供 2 种不同百分比折扣的优惠券 [英] Coupon with 2 different percentage discounts based on product category in Woocommerce

查看:20
本文介绍了根据 Woocommerce 中的产品类别提供 2 种不同百分比折扣的优惠券的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 Woocommerce 挂钩,它有助于在应用特定优惠券时根据 2 种不同的产品类别限制更改折扣百分比.

I am looking for a Woocommerce hook that will help to change the discount percentage based on 2 different product category restrictions when a specific coupon is applied.

例如,如果客户添加我想要的特定优惠券:

For example if customer add a specific coupon I will like to:

  1. 如果购物车中的商品来自产品类别 A,则该商品将获得 10% 的折扣.
  2. 如果它在产品类别 B 中,它将为该项目提供 20% 的折扣
  3. 更新购物车总价

是否有任何钩子可以实现这一目标?任何可用的动作钩子或过滤器钩子?

Is there any hook that could be available to achieve this? Any available action hook or filter hook?

这是我目前的代码:

add_filter( 'woocommerce_get_discounted_price', 'apply_coupon', 10);
function apply_coupon($price) {
     global $woocommerce;
     $product=$woocommerce->cart->product;
     if(has_term( 'duplex-blinds', 'A' ,$product->id)){
       get_product_cart_price; 
      10% DISCOUNT
     }
     if(has_term( 'duplex-blinds', 'A' ,$product->id)){
      20% DISCOUNT
     }
     upadte total_discunt_incart($new_discount);
     upadte new_price_in_cart($new_price);
     upadte new_price_in_checkout($new_price);
  return $price;
}

重要的是我需要修改购物车总价结账总价总折扣价和折扣价需要发送到支付宝.

The important thing is i need to modify the total cart price , total checkout price , total discount price and discount price need to send to Paypal.

我的商店有很多钩子,这就是为什么 woo commerce 默认优惠券计算会出错的原因.我注意到购物车页面中的折扣价格是根据自定义产品价值正确计算的,但它不会从原始购物车金额中更新,因此总价保持不变.

My shop have many hooks that's why woo commerce default coupon calculation is going to wrong. And i noticed that in cart page discount price is coming correctly based on the custom product value, but it not get updated from the original cart amount, so the total price remain the same.

但是在结帐页面中的折扣价是根据产品原价而不是产品定制价计算的,所以折扣是错误的,而且也不是从总价中最小化......

But in checkout page discount price is calculated based on the product original price not the product custom price so the discount is coming wrong and also it is not minimize from the total price also...

推荐答案

以下是一种完全不同的实现方式……此答案代码将启用具有基于 2 个特定产品类别的 2 个不同折扣百分比的优惠券代码.

The following is a completely different way to make that works… This answer code will enable a coupon code with 2 different discounts percentage based on 2 specific product categories.

例如,您的相关产品类别是:

Lest say for example that your related product categories are:

  • 对于10%的优惠券折扣,产品类别slug将为'hoodies'
  • 对于 20% 的优惠券折扣,产品类别 slug 将为 't-shirts'
  • For the coupon discount of 10%, the product category slug will be 'hoodies'
  • For the coupon discount of 20%, the product category slug will be 't-shirts'

(您可以在代码中使用产品类别 ID、slug 或名称)

这需要两个步骤:

  1. 优惠券设置(正确设置您的优惠券代码):
    • 折扣类型:百分比
    • 数量:10
    • 限制 > 产品类别(显示的名称):连帽衫"和T 恤"
    • 如果需要,您可以进行其他设置
  • 优惠券代码:将您的优惠券代码设置为小写
  • 't-shirts' 产品类别 slug (20% 的折扣).

<小时>

现在代码来了(您将在其中添加设置):


Now Here comes the code (where you will add your settings):

add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 );
function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){

    ## ---- Your settings ---- ##

    // Related coupons codes to be defined in this array (you can set many)
    $coupon_codes = array('10percent');

    // Product categories at 20% (IDs, Slugs or Names)  for 20% of discount
    $product_category20 = array('hoodies'); // for 20% discount

    $second_percentage = 0.2; // 20 %

    ## ---- The code: Changing the percentage to 20% for specific a product category ---- ##

    if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $product_category20, 'product_cat', $cart_item['product_id'] ) ){
            $original_coupon_amount = (float) $coupon->get_amount();
            $discount = $original_coupon_amount * $second_percentage * $discounting_amount;
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $round;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这是一个带插图的真实工作示例(带有屏幕截图):

Here is an illustrated real working example (with screen shots):

  • 第 1 个购物车商品(来自 'hoodies' 产品类别)获得 10% 的折扣 $40 x 10% = $4
  • 第二个购物车商品(来自 't-shirts' 产品类别)获得 20% 的折扣 $30 x 20% = $6

所以总折扣是 $4 + $6 = $10 ......效果很好!

So the total discount is $4 + $6 = $10 … That works!

这篇关于根据 Woocommerce 中的产品类别提供 2 种不同百分比折扣的优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆