【发布时间】:2022-02-05 23:18:17
【问题描述】:
正在向网站添加内容,想要修改购物车中的价格。有以下代码:
function apd_product_custom_price($cart_item_data, $product_id)
{
if (isset($_POST['use_rewards']) && !empty($_POST['use_rewards']))
{
$cart_item_data['use_rewards'] = $_POST['use_rewards'];
}
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'apd_product_custom_price', 99, 2);
function apd_apply_custom_price_to_cart_item($cart_object)
{
if( !WC()->session->__isset( 'reload_checkout' )) {
foreach ($cart_object->cart_contents as $value) {
if(isset($value['use_rewards'])) {
$price = $value['data']->get_price() -
$value['use_rewards'];
$value['data']->set_price($price);
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'apd_apply_custom_price_to_cart_item',10);
由于某种原因,钩子 woocommerce_before_calculate_totals 触发了两次。如果我只用 echo 1 替换函数 apd_apply_custom_price_to_cart_item($cart_object) 中的代码;它在购物车页面中显示 11。有人可以帮忙吗?
【问题讨论】:
标签: php wordpress woocommerce