【发布时间】:2017-01-06 11:51:58
【问题描述】:
我目前正在使用 WooCommerce 建立一个网上商店,并且我制作了这个购物车,您可以随时在任何页面访问,您可以更新购物车中产品的数量。每当我这样做时,就会出现问题,有些值会搞砸。例如,当我尝试获取 WC()->cart->total 时,它返回 0。
但是当我转到结帐页面时,它会显示所有正确的购物车数据,所以这让我觉得我错过了一些 action 我必须在调整购物车中的某些内容后运行。我一直在查看set_quantity() 函数,它会自动刷新总数为$this->calculate_totals();(也手动尝试过)。
Ajax 函数:
public function set_quantity($direction = false, $product_id) {
$response = array();
$justOne = false;
if($_GET['data']['direction'] && $_GET['data']['product_id']) {
$direction = $_GET['data']['direction'];
$product_id = $_GET['data']['product_id'];
$justOne = true;
}
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($product_id == $_product->id) {
if($justOne && $direction == 'minus') {
WC()->cart->set_quantity($cart_item_key, $values['quantity'] - 1, true);
$response['success']['quantity'] = $values['quantity'] - 1;
} else if($justOne && $direction == 'plus') {
WC()->cart->set_quantity($cart_item_key, $values['quantity'] + 1, true);
$response['success']['quantity'] = $values['quantity'] + 1;
} else {
WC()->cart->set_quantity($cart_item_key, $values['quantity'] + $direction, true);
}
$response['success']['line_total'] = '€ '.number_format((float)$response['success']['quantity'] * $_product->price, 2, '.', '');
$response['success']['cart_count'] = WC()->cart->get_cart_contents_count();
$response['success']['total'] = number_format((float)WC()->cart->total, 2, '.', '');
die(json_encode($response));
}
}
return false;
}
【问题讨论】:
-
你试过这个程序了吗 $woocommerce->cart->get_total(); WC()->购物车->get_total(); ?
-
你何时以及如何调用你提供的这个函数?
-
@Gopalakrishnan 我想我也试过了
-
@Reigel 当有人调整我购物车中的数量时(可以是任何页面)。
-
@Gopalakrishnan 响应迟但 WC()->cart->get_total();也返回 0.00。
标签: php jquery ajax wordpress woocommerce