php – 以编程方式为Woocommerce 3添加有条件的折扣
发布时间:2020-12-13 16:11:03 所属栏目:PHP教程 来源:网络整理
导读:我正在寻找一种在结账时以编程方式创建优惠券的方法,并在结账时将其删除.这需要在奖金制度的基础上进行,我会检查是否允许客户获得奖金.重要的是,我不想把它作为普通优惠券,因为客户不应该通过自己知道代码来附加它. 我只找到了附加优惠券或以编程方式创建优
我正在寻找一种在结账时以编程方式创建优惠券的方法,并在结账时将其删除.这需要在奖金制度的基础上进行,我会检查是否允许客户获得奖金.重要的是,我不想把它作为普通优惠券,因为客户不应该通过自己知道代码来附加它.
我只找到了附加优惠券或以编程方式创建优惠券的解决方案.我在结账时没有发现临时优惠券. 同样重要的是,这张优惠券可以与其他优惠券相结合,而不是更多. 这是我的代码: 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 按折扣百分比计算 这甚至可能吗? 解决方法
为了简单起见,你可以使用负费用(每个步骤点增加折扣百分比),如:
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 ); } } 代码位于活动子主题(或活动主题)的function.php文件中.经过测试和工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |