3
我已经建立了使用woocommerce_before_add_to_cart_button
钩一个隐藏的输入项WooCommerce价格中重写不工作
function add_gift_wrap_field() {
?>`<input type="hidden" id="price_val" name="added_price" value="100.34">`<?php
}
add_action('woocommerce_before_add_to_cart_button', 'add_gift_wrap_field');
保存对产品领域:
function save_gift_wrap_fee($cart_item_data, $product_id) {
if(isset($_POST['added_price'])) {
$cart_item_data = array();
$cart_item_data[ "gift_wrap_fee" ] = "YES";
$cart_item_data[ "gift_wrap_price" ] = 100;
}
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2);
现在,我可以附和了这里面的$_POST['added_price']
woocommerce_before_calculate_totals
钩。
我编写的代码如下所示:
function calculate_gift_wrap_fee($cart_object) {
if(!WC()->session->__isset("reload_checkout")) {
/* Gift wrap price */
$additionalPrice = number_format($_POST['added_price'], 2);
$additionalPrice = floatval($additionalPrice);
//echo $additionalPrice; exit(); shows the value
foreach ($cart_object->cart_contents as $key => $value) {
if(isset($value["gift_wrap_fee"])) {
/* Woocommerce 3.0 + */
$orgPrice = floatval($value['data']->get_price());
$value['data']->set_price($orgPrice + $additionalPrice); //not adding the $additionalPrice here.
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99);
什么我错在这里做什么?
我刚刚加入:) –
谢谢:) ...为什么你不添加到购物车对象''added_price''在同一时间? – LoicTheAztec
试过但实际上没有工作 –