2016-08-30 26 views
9

Şu anda bir web mağazası yapmakta olan WooCommerce ve bu sepete sizin istediğiniz zaman herhangi bir sayfada erişebilmeniz için yapılabilen bir ürünün sepeti ürün sepeti içindeki miktarını değiştirebilirsiniz. Sorun her ne zaman olursa olsun, bazı şeyler berbat olur. Örneğin, WC()->cart->total'u almayı denediğimde 0WooCommerce ajax güncellemesi bozuluyor

Ancak, ödeme sayfasına gittiğimde tüm doğru kart verilerini gösterir, bu yüzden bana bir şey ayarladıktan sonra çalıştırmam gerektiğini düşündüğümü düşünürüm action arabada. set_quantity() işlevlerini inceledim ve otomatik olarak toplamı $this->calculate_totals(); (elle de denedim) ile yeniliyor.

Ajax fonksiyonu:

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

Eğer bu prosedür $ denedin mi woocommerce-> sepeti-> get_total(); WC() -> cart-> get_total(); ? – Gopalakrishnan

+0

sağladığınız bu işlevi ne zaman ve nasıl çağırıyorsunuz? – Reigel

+0

@Gopalakrishnan Sanırım bunu hatırlayamadım, zamanım olduğunda test edeceğim, cevabınız için teşekkürler – LVDM

cevap

1

kullanın bu değiştirilmiş ajax fonksiyonu. Bunu test ettim. Çalışacak.

Modifiye Ajax Fonksiyonu:

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

Cevabınız için teşekkür ederiz, zamanı bulduktan sonra test edeceksiniz! – LVDM

+0

Sonunda test etmek için zamanım var ve benim için çalışmıyor. WC() -> cart-> get_total() '&' WC() -> cart-> total' bazı garip nedenlerden dolayı '0.00' dönüşü. – LVDM

+0

Aslında işe yaradı, benim tarafımda bir hata yaptı :-), çok arkadaşım teşekkürler. – LVDM