【发布时间】:2018-05-14 12:15:46
【问题描述】:
我在网站上创建了一个自定义表单选项来更新价格并通过 ajax 发送该数据并尝试进入过滤器 woocommerce_before_calculate_totals 会话。
这是代码
add_action( 'woocommerce_before_calculate_totals', 'calculate_total_price', 99 );
function calculate_total_price( $cart_object )
{
global $woocommerce;
session_start();
$tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
$target_product_id = $_SESSION['wdm_user_product_id_data'].'<br/>.';
$_SESSION['productlist'][] =
[
'price' => $tac_dd_discounted_price,
'productid' => $target_product_id
];
$arrys = array_merge( $_SESSION[ "productlist" ]);
$_SESSION[ "productlist" ] = array_unique($arrys);
// This unique array created in seesion fro multi product which show correct data.
$price_blank="1";
foreach ( $cart_object->get_cart() as $cart_item ) {
$id= $cart_item['data']->get_id();
//$target_product_id=$arrys['productlist']['productid'];
//$tac_dd_discounted_price=$arrys['productlist']['price'];
if ( $id == $target_product_id ) {
$cart_item['data']->set_price($tac_dd_discounted_price);
}
else
{
$cart_item['data']->set_price($my_price['productlist']['price']);
}
}
}
但问题是购物车中的一种产品价格显示正确,但是当尝试添加两种产品时,Seession 变量在两个产品中附加相同的值
【问题讨论】:
-
请编辑您的问题并澄清问题,而不是在答案线程中添加代码和解释,这不是答案。同样,当您这样做时,没有人知道,因为没有通知。您需要在答案上添加评论以通知回答者。
标签: php wordpress session woocommerce cart