【问题标题】:WooCommerce ajax update messing up valuesWooCommerce ajax 更新弄乱了值
【发布时间】: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


【解决方案1】:

使用这个修改后的 ajax 函数。我已经测试过了。它会起作用的。

修改后的 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);
            }

            if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
                define( 'WOOCOMMERCE_CART', true );
            }
            WC()->cart->calculate_totals();

            $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;
}

【讨论】:

  • 谢谢你的回答我会尽快测试!
  • 终于有时间测试了,对我不起作用。 WC()->cart->get_total()WC()->cart->total 都出于某种奇怪的原因返回 '0.00'。
  • 实际上它确实有效,在我这边犯了一个错误:-),非常感谢。
猜你喜欢
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
相关资源
最近更新 更多