2016-08-30 5 views
9

私は現在WooCommerceでWebショップを作っていますが、いつでもどのページからでもアクセスできるこのカートを持っています。カート内の製品数を更新できます。問題は私がこれを行うたびに発生し、いくつかの値が台無しになる。例えば、私はそれが0WooCommerce ajax update値が狂っています

を返しますが、私はチェックアウトのページに移動するとき、それはすべて正しいカートのデータを示しWC()->cart->totalを取得しようとするので、これは私が私が何かを調整した後で実行する必要があり、いくつかの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; 
} 
+0

は、この手順の$を試したwoocommerce-> cart-> get_total(); WC() - >カート - > get_total(); ? – Gopalakrishnan

+0

あなたが提供したこの機能をいつ呼び出すのですか? – Reigel

+0

@Gopalakrishnan私は、私もあなたの答えをお寄せいただきありがとうございます。 – LVDM

答えて

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; 
} 
+0

ありがとうございました。私が時間を見つけたらすぐに答えがテストされます! – LVDM

+0

最後にそれをテストする時間があり、それは私のためには機能しません。 'WC() - > cart-> get_total()'と 'WC() - > cart-> total'の両方が何らかの奇妙な理由で '0.00'を返します。 – LVDM

+0

実際にそれは動作し、私の側で間違いを犯しました:-)、おかげで多くの仲間。 – LVDM

関連する問題