wordpress - WooCommerce 优惠券从正常价格计算
问题描述
我需要“朋友”客户的功能,他们可以以更优惠的价格获得产品。基于用户角色的解决方案不是我想要的,客户希望它与优惠券一起使用。他们向客户提供优惠券代码,例如,他们可以从某些产品的常规价格中获得 8 欧元的折扣。问题是,默认情况下 WooCommerce 优惠券从最低价格计算。如果设置了销售价格,那么这个计算是错误的,客户得到的价格太便宜了。基本上,我想要优惠券给某些产品固定的“朋友”价格。
我一直在谷歌搜索,找不到现成的解决方案。欢迎任何有关如何解决此问题的建议。
我检查了多个插件,但没有一个能满足我的要求。
解决方案
添加到functions.php。基于此代码https://brilliantinfo.net/apply-coupon-discount-on-regular-price-in-woocommerce/
针对固定产品的优惠券类型进行了修改。如果您需要它仅适用于特定优惠券,那么我可以为此进行修改。(就目前而言,这将适用于使用的任何固定产品优惠券)
add_action( 'woocommerce_before_calculate_totals', 'adjust_cart_coupon', 10, 1);
function adjust_cart_coupon( $cart_object) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
$coupon = False;
if ($coupons = WC()->cart->get_applied_coupons() == False ) {
$coupon = False;
} else {
foreach ( WC()->cart->get_applied_coupons() as $code ) {
$coupons1 = new WC_Coupon( $code );
if ($coupons1->type == 'fixed_product'){
$coupon = True;
}
}
}
if ($coupon == True){
foreach ( $cart_object->get_cart() as $cart_item ){
$price = $cart_item['data']->regular_price;
//sets cart item to use regular price and not sale price
$cart_item['data']->set_price( $price );
}
}
}
推荐阅读
- python - Archlinux:pip:ImportError:无法导入名称“请求”
- sql - 区分全额退款与部分退款
- business-process-management - 投资银行中后台典型IT项目
- angular - 预期的“样式”是一个字符串数组,角度为 5
- vba - 宏适用于我的 Excel,但不适用于其他人
- google-cloud-firestore - Firestore 更新文档对象字段中的多个属性
- python - 从 csv 文件导入权重:层权重形状 (672, 7) 与提供的权重形状 (1, 1, 672, 7) 不兼容
- laravel - 如何在 laravel-5.6 中安装 angular5
- java - 如何使用 LibGDX 生成透明的 PNG 图像?
- python - Tensorflow 代码不生成事件文件