2017-08-08 11 views
1

私はecom Webチュートリアルに従っていますが、コードを何度も変更していますが、それでも正しいことを得られませんでした。プロダクトIDの代わりにプロダクトの数量に基づいてカートの横にエコーします。カートに追加された数量のカートをエコーカート

画像例: enter image description here

Shop.phpコード:

<a href="#tab2">CART <span class="badge"> 
<?php 
    if(isset($_SESSION["shopping_cart"])) { 
     echo count($_SESSION["shopping_cart"]); 
    } else { 
     echo '0';} 
?> 
</span></a> 

action.phpコード:

+0

これはMagentoカートですか?私はそれがあなたのタグに入っていて、人々はMagentoの回答で回答していますが、あなたがMagentoのチュートリアルからこれを得ているのなら、私はMagentoカートを扱う従来の方法ではない新しいチュートリアルを探すかもし​​れません。 Magentoカートではない場合は、そのタグを削除してMagento固有の回答を得ないようにしてください。問題には関係ありません。 – Rasclatt

答えて

0
<?php 
if (isset($_POST["product_id"])) { 
    $order_table = ''; 
    $message = ''; 
    if ($_POST["action"] == "add") { 
     if (isset($_SESSION["shopping_cart"])) { 
      $is_available = 0; 
      foreach ($_SESSION["shopping_cart"] as $keys => $values) { 
       if ($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"]) { 
        $is_available++; 
        $_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"]; 
       } 
      } 
      if ($is_available < 1) { 
       $item_array = array(
        'product_id'  => $_POST["product_id"], 
        'product_name'  => $_POST["product_name"], 
        'product_price'  => $_POST["product_price"], 
        'product_quantity' => $_POST["product_quantity"] 
       ); 
       $_SESSION["shopping_cart"][] = $item_array; 
      } 
     } else { 
      $item_array = array(
       'product_id'  => $_POST["product_id"], 
       'product_name'  => $_POST["product_name"], 
       'product_price'  => $_POST["product_price"], 
       'product_quantity' => $_POST["product_quantity"] 
      ); 
      $_SESSION["shopping_cart"][] = $item_array; 
     } 
    } 
} 
?> 

あなたから各項目の数量を格納しますループを使用しているカート:

function getCartQty() 
    { 
     # If there is nothing in the cart, return 0 
     if(empty($_SESSION['shopping_cart'])) 
      return 0; 
     # Store array 
     $qty = array(); 
     # Loop items in cart 
     foreach($_SESSION["shopping_cart"] as $item){ 
      # Store the quantity of each item 
      $qty[] = $item['product_quantity']; 
     } 
     # Return the sum 
     return array_sum($qty); 
    } 
?> 
<a href="#tab2">CART <span class="badge"><?php echo getCartQty() ?></span></a> 
0

count()は、配列内の要素の数を返します。 $ _SESSION配列に格納されたショッピングカート配列を実行すると、一意の商品の数がカウントされます。

各製品に数量値があるため、それらの合計を取得する必要があります。

function my_shopping_cart_total_product_count() { 
    $product_count = 0; 

    if (isset($_SESSION['shopping_cart'])) { 
     $product_count = array_sum(array_column($_SESSION['shopping_cart'], 'product_quantity')); 
    } 

    return $product_count; 
} 

の例では、私は、製品の数量のすべてを含む配列を取得するためにarray_columnを使用しています上:ここでは、その計算を実行するために使用できる機能です。私は合計を得るためにarray_sumまでそれを実行しています。もちろんforeachループを使うこともできます。

用途:

<a href="#tab2">CART <span class="badge"><?php echo my_shopping_cart_total_product_count(); ?></span></a> 

ドキュメント:

追加ノート:

action.phpのコードはかなりです。ユーザー入力の墨塗りが不足しているようです。セキュリティを向上させるためにデータのサニタイズを調べることをお勧めします。

0

代わりに異なる製品の数量の品目の合計数量取得するためにMagentoのバックエンドで設定を変更することができます。

  1. [システム]> [設定]> [販売]> [チェックアウトを。
  2. マイカートカートの中には何もありません。
  3. これを次のように設定します。カート内のアイテム数を表示します。

これはデフォルトのMagentoテーマで動作しますので、自分のテーマに合わせて動作させることができます。

0

ます。また、これを試すことができます。

# return shopping cart items count means how many sku add to shopping cart 
Mage::helper('checkout/cart')->getItemCount() 

# return shopping cart items summary (suppose you add sku1 6 qty and sku2 3 qty = total 9 qty return) 
Mage::helper('checkout/cart')->getSummaryCount() 

出典:https://magento.stackexchange.com/a/23496/46249

注:Magentoの中で$_SESSIONを使用しないでくださいを、代わりの方法があります。

関連する問題