Php 以编程方式有条件地向Woocommerce 3添加折扣
我正在寻找一种方法,在结帐期间以编程方式创建优惠券,并在结帐完成时将其删除。这需要在奖金制度的基础上完成,在奖金制度中,我检查是否允许客户获得奖金。重要的是,我不想让它成为普通优惠券,因为客户不应该在知道代码的情况下附加它 我只找到了附加优惠券或以编程方式创建优惠券的解决方案。在一次结账时,我没有发现任何关于临时优惠券的信息 同样重要的是,此优惠券可以与其他优惠券组合,而不是更多 这是我的代码:Php 以编程方式有条件地向Woocommerce 3添加折扣,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在寻找一种方法,在结帐期间以编程方式创建优惠券,并在结帐完成时将其删除。这需要在奖金制度的基础上完成,在奖金制度中,我检查是否允许客户获得奖金。重要的是,我不想让它成为普通优惠券,因为客户不应该在知道代码的情况下附加它 我只找到了附加优惠券或以编程方式创建优惠券的解决方案。在一次结账时,我没有发现任何关于临时优惠券的信息 同样重要的是,此优惠券可以与其他优惠券组合,而不是更多 这是我的代码: if ( get_discount_points() < 100 ) { //Cust
if ( get_discount_points() < 100 ) {
//Customer has bonus status 1
} elseif ( get_discount_points() < 200 ) {
//Customer has bonus status 2
} else {
//Customer has bonus status x
if(获得折扣积分()<100){
//客户的奖金状态为1
}其他(获得折扣积分()<200){
//客户有奖金状态2
}否则{
//客户具有奖金状态x
按折扣率
}
那么这是可能的吗?要想得到简单的东西,你可以使用负费用(每一步点数都会增加折扣百分比),比如:
函数获取客户折扣(){
如果($points=获得折扣积分()){
如果($100分以下){
返回1;//1%折扣
}elseif($200分以下){
返回2;//2.5%折扣
}否则{
返回4;//5%折扣
}
}否则{
返回false;
}
}
添加操作('woocommerce\u cart\u calculate\u fees'、'custom\u折扣',10,1);
功能自定义折扣($cart){
if(定义了('DOING'uajax'))
返回;
//仅适用于2项或2项以上
如果($percentage=获得客户折扣()){
$discount=WC()->cart->get_subtotal()*$percentage/100;
//对购物车中的非销售商品向第二个商品应用折扣
如果($折扣>0)
$cart->add_费用(sprintf(uuu(“折扣%s%%”),$percentage),-$折扣);
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
function get_customer_discount(){
if( $points = get_discount_points() ){
if ( $points < 100 ) {
return 1; // 1 % discount
} elseif ( $points < 200 ) {
return 2; // 2.5 % discount
} else {
return 4; // 5 % discount
}
} else {
return false;
}
}
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount', 10, 1 );
function custom_discount( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if( $percentage = get_customer_discount() ){
$discount = WC()->cart->get_subtotal() * $percentage / 100;
// Apply discount to 2nd item for non on sale items in cart
if( $discount > 0 )
$cart->add_fee( sprintf( __("Discount %s%%"), $percentage), -$discount );
}
}