本文介绍了基于 Woocommerce 中特定产品数量的渐进式固定优惠券折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one little problem that dont know how to fix myself. I want to use this logic into my Woocommerce store just for one product.

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

I have use link like this to autmatically apply coupon code and add to cart:

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

这似乎有效,当数量为 1 时.但我想要折扣 (30%),当点击之前的直接链接时自动放置,以使其动态,例如:

and this seems to work, when quantity is 1. But i want discount (30%) that is automatically placed when click on direct link from before, to make dynamic, for ex:

  • 在购物车页面将数量增加到 2,并自动计算优惠券以计算 2 x 30$ = 60$ 折扣,
  • 从同一产品购买 3 件,并计算 3 X 30$ = 90$ 优惠券折扣等等..
  • Increase quantity to 2 in cart page, and coupon automatically to calculate 2 x 30$ = 60$ discount,
  • buy 3 from same product, and calculate 3 X 30$ = 90$ coupon discountand so on..

我搜索了一下,发现这个有用线程,但情况与我的略有不同.

I searched, and found this usefull thread, but there situation is little different then mine.

我怎样才能拥有一张特定的优惠券?一些建议或起点.谢谢

How can I make to have a specific coupon? Some advice or start point. Thanks

推荐答案

这可以通过 2 个步骤实现:

This is possible with that 2 steps:

1) 添加一个独特的优惠券:

1) Add a unique coupon with:

  • 在常规设置中 > 类型 = 固定产品折扣
  • 在一般设置中 > Amount = 30
  • 在使用限制 > 产品 ==> 设置您想要的产品

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

2) Add this code (where you will set your coupon code in the function (in lowercase)):

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 文件中.经测试有效.

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

这篇关于基于 Woocommerce 中特定产品数量的渐进式固定优惠券折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 12:56