我已经实现了这个自定义代码,以编程方式为登录用户提供折扣。在我选择变体后,折扣正确地适用于变体价格。
我稍微更改了代码,不考虑已经打折的产品,所以我的代码如下所示
编辑:我还添加了将礼品卡产品排除在折扣之外的条件。
add_filter( 'woocommerce_product_get_price', 'custom_discount_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_discount_price', 10, 2 );
function custom_discount_price( $price, $product ) {
// For logged in users
if ( is_user_logged_in() ) {
$discount_rate = 0.95; // 5% of discount
// Product is on sale or is a gift card
if ( $product->is_on_sale() || $product->get_id() == 5770 || $product->get_id() == 5766 || $product->get_id() == 5764 ) {
return $price;
}
// Product is not on sale
else {
// Returns the custom discounted price
return $price * $discount_rate;
}
}
return $price;
}
}
但是在类别页面,甚至在单变量产品页面上,都有这个范围告诉客户该产品的最小变化价格到最大变化价格是多少,并且这个价格范围不显示折扣价格,而是原始价格。
举个例子:
我以编程方式将所有产品打折 90%。
产品具有三种变体 -> X、Y、Z。
对于变体 X,我设定了 10 美元的价格。
对于 Y 版本,我设定了 20 美元的价格。
对于 Z 版本,我设定了 30 美元的价格。
在类别页面上,我可以看到产品及其图像和标题 -> 产品:$10 - 30$。
但它实际上应该显示价格 1 - 3 美元,因为我以编程方式对所有价格进行了折扣。
我到处寻找并尝试任何事情,但没有结果。有办法实现这一点吗?谢谢。
编辑:我将代码片段中的折扣率更改为 0.95 以匹配我提供的答案。
好的,这就是我现在得到的。
我之前的答案有严重缺陷,没有考虑到所有可能性,但我认为我涵盖了大多数情况。
在我的问题中的代码之后我添加了以下代码:
add_filter( 'woocommerce_variable_price_html', 'custom_discount_variable_price', 10, 2 );
function custom_discount_variable_price( $price, $product ) {
if ( is_user_logged_in() && ! is_admin() ) {
$discount_rate = 0.95; // 5% of discount
$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );
$max_var_reg_price = $product->get_variation_regular_price( 'max', true );
$max_var_sale_price = $product->get_variation_sale_price( 'max', true );
if ( ! ( $min_var_reg_price == $max_var_reg_price && $min_var_sale_price == $max_var_sale_price ) ) {
// regular price or sale price are not the same
if ( $min_var_sale_price < $min_var_reg_price * $discount_rate ) {
if ($max_var_sale_price < $max_var_reg_price * $discount_rate) {
// min_var_sale_price - max_var_sale_price
$price = sprintf( __( '%s - %s', 'woocommerce' ), wc_price( $min_var_sale_price ), wc_price( $max_var_sale_price ), $product->get_price_suffix() );
} else {
// min_var_sale_price - max_var_reg_price
$price = sprintf( __( '%s - %s', 'woocommerce' ), wc_price( $min_var_sale_price ), wc_price( $max_var_reg_price * $discount_rate ), $product->get_price_suffix() );
}
} else {
if ($max_var_sale_price < $max_var_reg_price * $discount_rate) {
// min_var_reg_price - max_var_sale_price
$price = sprintf( __( '%s - %s', 'woocommerce' ), wc_price( $min_var_reg_price * $discount_rate ), wc_price( $max_var_sale_price ), $product->get_price_suffix() );
} else {
// min_var_reg_price - max_var_reg_price
$price = sprintf( __( '%s - %s', 'woocommerce' ), wc_price( $min_var_reg_price * $discount_rate ), wc_price( $max_var_reg_price * $discount_rate ), $product->get_price_suffix() );
}
}
}
// regular price or sale price are the same
else {
if ( $min_var_sale_price < $min_var_reg_price * $discount_rate ) {
$price = $min_var_sale_price;
} else {
$price = $min_var_reg_price * $discount_rate;
}
}
return $price;
} else {
return $price;
}
}
我从出色的Business Bloomer中获得灵感并编辑了他的代码片段。然后我必须考虑不同的最小和最大变化价格及其组合。现在,无论是促销价还是正常价格,或者其中一些价格高于其他价格,可变产品价格范围都应该根据折扣率正确显示。
我怀疑有时它可能无法正常工作 - 当我将折扣率设置得太高时,某些价格不会按应有的方式显示。不过,虽然折扣率维持在 5%,但它看起来确实有效。
我绝对愿意接受有关如何使其工作得更好的建议。
编辑:我添加了 is_admin() 检查以不在管理环境中更改价格,并且还添加了最后的 else{return $price} 语句,因为我在管理产品页面中没有看到价格范围。