自定义优惠券类型woocommerce wordpress [英] Custom Coupon type woocommerce wordpress
问题描述
我需要创建自定义优惠券类型.因为我有自定义优惠券的自定义计算.任何人都可以帮助我.
I need to create custom coupon type. Because i had custom calculation for custom coupon. Anyone out there help me.
优惠券计算:购物车有一个产品,价值为 5000,然后应用自定义优惠券代码.购物车总数需要更改 2000.同样,购物车有 2 个产品,价值为 7000.如果应用了自定义优惠券代码,则购物车总数需要为 4000.
Coupon calculation: the cart has one product and value is 5000, then custom coupon code applied. The cart total need to change 2000. Similarly the cart has 2 product and value is 7000. If the custom coupon code applied, then cart total need to be 4000.
因此优惠券需要使购物车中的一件商品总计为 2000 元
So the coupon need to make the cart total as flat 2000 for one product
推荐答案
请在您的活动主题的 function.php 中使用以下代码
Please use below code in your active theme's function.php
function custom_discount_type( $discount_types ) {
$discount_types['custom_discount'] =__( 'custom discount', 'woocommerce' );
return $discount_types;
}
// add the hooks
add_filter( 'woocommerce_coupon_discount_types', 'custom_discount_type',10, 1);
//function to get coupon amount for "custom_discount"
function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) {
if ($coupon->code == 'custom'){
//echo "yes custom discount"; //if $coupon->type == 'fixed_cart' or 'percent' or 'fixed_product' or 'percent_product' The code Works
$discount = $cart_item['quantity'] * 2000;
return $discount;
} else {
return $discount;
}
}
//add hook to coupon amount hook
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5);
注意:请在此行的此处添加您添加的优惠券名称if ($coupon->code == 'your_added_coupon_here')
Note : Please add your added coupon name here on this line if ($coupon->code == 'your_added_coupon_here')
工作正常并经过测试.
这篇关于自定义优惠券类型woocommerce wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!