基于 Woocommerce 中特定产品数量的累进式固定优惠券折扣

Progressive fixed Coupon Discount based on specific product quantity in Woocommerce

我有一个小问题不知道如何解决。我只想将此逻辑用于我的 Woocommerce 商店中的一种产品。

我像这样使用 link 自动应用优惠券代码并添加到购物车:

https://testsite.com/checkout/?add-to-cart=Product_ID&quantity=1&coupon=Coupon_Code

这似乎有效,当数量为 1 时。但是我想要在点击直接 link 之前自动放置的折扣 (30%),以实现动态,例如:

我搜索了一下,找到了这个 usefull 帖子,但是情况和我的差不多。

如何才能获得特定的优惠券?一些建议或起点。谢谢

这两个步骤是可能的:

1) 添加独特的优惠券:

  • 在常规设置 > 类型 = 固定产品折扣
  • 在常规设置 > 金额 = 30
  • 在使用限制 > 产品 ==> 设置您想要的产品(s)

2) 添加此代码(您将在函数中设置优惠券代码的位置(小写)):

add_filter( 'woocommerce_coupon_get_discount_amount', 'custom_coupon_get_discount_amount', 10, 5 );
function custom_coupon_get_discount_amount( $rounded_discount, $discounting_amount, $cart_item, $single, $coupon ){

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

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

    ## ------ The code ------- ##

    if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) && $cart_item['quantity'] > 1 ) {
        if( in_array( $cart_item['product_id'], $coupon->get_product_ids() ) ){
            $discount = (float) $coupon->get_amount() * (int) $cart_item['quantity'];
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $rounded_discount;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。