Arc*_*ana 5 php wordpress product cart woocommerce
我在 WooCommerce 上创建了一个产品,并使用 hook 在产品详细信息页面上添加了两个选项woocommerce_before_add_to_cart_button。现在,当客户从产品详细信息页面将产品添加到购物车时,他们有两个选项。他们可以从这两个选项中选择一个。
然后我使用 woocommerce_add_cart_item_data 将用户选择的值存储在购物车元数据中。
我正在使用此答案中的代码:在购物车中保存产品自定义字段单选按钮值并在购物车页面上显示它
这是我的代码:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的:
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 );
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想用最后一个片段得到的是:
但它没有按预期工作,因为更改后的产品价格是 maid before 并且在此更改后的价格上应用优惠券折扣。
我想要的是优惠券折扣将首先应用于产品正常价格,然后在更改此价格后使用我的自定义产品折扣。
这可能吗?我怎样才能做到这一点?
谢谢。
\n\n这实际上不可能\xe2\x80\xa6 为什么?\xe2\x80\xa6因为(逻辑):
\n\n
\n- 您有产品价格
\n- 然后优惠券折扣将应用于该价格(之后)
\n
\n ==> 如果您更改产品价格,优惠券将应用于更改后的价格
你可以做什么:
\nWC_cart add_fee()根据使用方法\xe2\x80\xa6后添加的产品价格应用自定义折扣(负费用)对于最后一种情况,您将必须微调您的额外折扣。
\n如果优惠券尚未使用或已被删除,则不会有额外折扣。
您的自定义函数将被挂接到woocommerce_cart_calculate_fees操作挂钩中:
add_action( \'woocommerce_cart_calculate_fees\', \'option2_additional_discount\', 10, 1 );\nfunction option2_additional_discount( $cart_obj ) {\n\n if ( is_admin() && ! defined( \'DOING_AJAX\' ) )\n return;\n\n $discount = 0;\n $applied_coupons = $cart_obj->get_applied_coupons();\n\n foreach ( $cart_obj->get_cart() as $item_values ) {\n if( \'option2\' == $item_values[\'custom_options\'] && !empty($applied_coupons) ){\n $product_id = $item_values[\'product_id\'];\n $percentage = get_post_meta( $product_id , \'percentage\', true );\n $quantity = $item_values[\'quantity\'];\n $product_reg_price = $item_values[\'data\']->regular_price;\n $line_total = $item_values[\'line_total\'];\n $line_subtotal = $item_values[\'line_subtotal\'];\n $percentage = 90;\n\n ## ----- CALCULATIONS (To Fine tune) ----- ##\n\n $item_discounted_price = ($percentage / 100) * $product_reg_price * $item_values[\'quantity\'];\n // Or Besed on line item subtotal\n $discounted_price = ($percentage / 100) * $line_subtotal;\n\n $discount += $product_reg_price - $item_discounted_price;\n }\n }\n if($discount != 0)\n $cart_obj->add_fee( __( \'Option2 discount\', \'woocommerce\' ) , - $discount );\n}\nRun Code Online (Sandbox Code Playgroud)\n代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
\n这段代码已经过测试并且可以工作。
\n