我在 WooCommerce 上创建了一个产品,并使用 Hook woocommerce_before_add_to_cart_button
在产品详细信息页面上添加了两个选项。现在,当客户从产品详细信息页面将产品添加到购物车时,他们有两个选择。他们可以从这两个选项中选择一个选项。
然后我使用 woocommerce Hook woocommerce_add_cart_item_data 将用户选择的值存储在购物车元数据中。
我正在使用此答案中的代码:Save product custom field radio button value in cart and display it on Cart page
这是我的代码:
// single Product Page options
add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");
function options_on_single_product(){
$dp_product_id = get_the_ID();
$product_url = get_permalink($dp_product_id);
?>
<input type="radio" name="custom_options" checked="checked" value="option1"> option1<br />
<input type="radio" name="custom_options" value="option2"> option2
<?php
}
//Store the custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_with_add_to_cart', 10, 2 );
function save_custom_data_with_add_to_cart( $cart_item_meta, $product_id ) {
global $woocommerce;
$cart_item_meta['custom_options'] = $_POST['custom_options'];
return $cart_item_meta;
}
这是我尝试过的:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
$product_id = $value['product_id'];
$custom_options = $value['custom_options'];
$coupon_code = $value['coupon_code'];
if($custom_options == 'option2')
{
if($coupon_code !='')
{
global $woocommerce;
if ( WC()->cart->has_discount( $coupon_code ) ) return;
(WC()->cart->add_discount( $coupon_code ))
//code for second discount
}
else{
$percentage = get_post_meta( $product_id , 'percentage', true );
//print_r($value);
$old_price = $value['data']->regular_price;
$new_price = ($percentage / 100) * $old_price;
$value['data']->set_price( $new_price );
}
}
}
}
现在我想用最后一个片段得到的是:
- 如果客户选择选项 1,则运行 woocommerce 常规流程。
- 如果选择选项 2,则首先将优惠券代码应用到购物车(如果代码由客户输入),然后将价格除以某个百分比(存储在产品元数据中)随后应用。。
但它没有按预期工作,因为更改后的产品价格是之前的佣金,并且在更改后的价格上应用了优惠券折扣。
我想要的是优惠券折扣将首先应用于产品正常价格,然后在使用我的自定义产品折扣更改此价格之后。
这可能吗?我怎样才能做到这一点?
谢谢。
最佳答案
This is not really possible … Why? … Because (the logic):
- You have the product price
- Then the coupon discount is applied to that price (afterwards)
==> if you change the product price, the coupon is will be applied to that changed price
您可以做什么:
- 您不更改产品价格
- 如果输入了优惠券,并且……
- 如果将“option2”产品添加到购物车:
- 根据使用后添加的产品价格应用自定义折扣(负费用)
WC_cart add_fee()
方法……
对于最后一种情况,您必须微调您的额外折扣。
如果优惠券尚未应用或已被移除,则没有额外折扣。
您的自定义函数将挂接到 woocommerce_cart_calculate_fees
操作 Hook 中:
add_action( 'woocommerce_cart_calculate_fees', 'option2_additional_discount', 10, 1 );
function option2_additional_discount( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount = 0;
$applied_coupons = $cart_obj->get_applied_coupons();
foreach ( $cart_obj->get_cart() as $item_values ) {
if( 'option2' == $item_values['custom_options'] && !empty($applied_coupons) ){
$product_id = $item_values['product_id'];
$percentage = get_post_meta( $product_id , 'percentage', true );
$quantity = $item_values['quantity'];
$product_reg_price = $item_values['data']->regular_price;
$line_total = $item_values['line_total'];
$line_subtotal = $item_values['line_subtotal'];
$percentage = 90;
## ----- CALCULATIONS (To Fine tune) ----- ##
$item_discounted_price = ($percentage / 100) * $product_reg_price * $item_values['quantity'];
// Or Besed on line item subtotal
$discounted_price = ($percentage / 100) * $line_subtotal;
$discount += $product_reg_price - $item_discounted_price;
}
}
if($discount != 0)
$cart_obj->add_fee( __( 'Option2 discount', 'woocommerce' ) , - $discount );
}
代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码已经过测试并且可以工作。
关于php - 应用优惠券代码后更改 WooCommerce 购物车价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43491931/