簡體   English   中英

Magento - 如何在 header.phtml 中獲取購物車項目的總數

[英]Magento - How to get cart items total in header.phtml

我正在使用 Magento 電子商務,並通過空白模板修改了我的 header.phtml。 代碼,這是我的代碼,但它顯示為空白。

 <?php $cartQty = $this->getSummaryCount() ?>
    <?php if ($cartQty>0): ?>

            <?php if ($cartQty==1): ?>
                <?php echo $this->__('<a class="cartgo" href="%s">(1 ITEM)</a>', $this->getUrl('checkout/cart')) ?>
            <?php else: ?>
                <?php echo $this->__('<a class="cartgo" href="%s">(%s ITEMS)</a>', $this->getUrl('checkout/cart')) ?>
            <?php endif ?>


    <?php endif ?>

之前有一個叫做SUHUR的人有一個鏈接的答案我認為,我會用答案獎勵他,但似乎他刪除了自己的帖子?

他與此相關: http//nothingtopost.wordpress.com/tag/how-to-get-total-cart-item-in-magento/

我修改了我的代碼,現在可以在.phtml文件上運行。

<?php
      $count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
      $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
      if($count==0)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(0 ITEMS)</a>',$count);
      }
      if($count==1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(1 ITEM)</a>',$count);
      }
      if($count>1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(%s ITMES)</a>',$count);
      }
      echo $this->__('', $this->helper('core')->formatPrice($total, false));
    ?>
<?php
    $cartTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
    $cartItemsCount = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    $cartSuffix = ($cartItemsCount != 1) ? 's' : '';

    echo '<a class="cartgo" href="'.$this->getUrl('checkout/cart').'">
              <strong>'.$this->__('Your basket').'</strong><br />'.
              $this->__('(%s) Item%s', $cartItemsCount, $cartSuffix).
              '<span>[$'.$this->helper('core')->formatPrice($cartTotal, false).']</span>
          </a>';
?>

輸出:

你的籃子
3件[$ 32.5]

<?php $_cartQty = Mage::getSingleton('checkout/cart')->getItemsCount(); echo $_cartQty; ?>

這就是你所需要的1.7如果你已經在運行法師:應用程序你沒有真正做任何事情。

此外,這僅顯示“項目”計數,而不是數量。

您可以在此處找到購物車模板:

YOURSITE/app/design/frontend/YOURTHEME/default/template/checkout/cart/minicart.phtml

.count類的范圍內,您將看到以下代碼段:

<span class="count"><?php echo $_cartQty; ?></span>

將其替換為此代碼段,您將獲得顯示的總計:

<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>

使用輔助對象獲取當前購物車對象,然后計算購物車對象中的項目數。

echo Mage::helper('checkout/cart')->getCart()->getItemsCount();

更多來自http://www.douglasradburn.co.uk/how-to-get-number-of-cart-items-in-magento/

當鏈接到購物Mage::helper('checkout/cart')->getCartUrl() ,你應該真正使用Mage::helper('checkout/cart')->getCartUrl() 如果您的站點托管在子域中,則給出的示例將不起作用。

<?php
      $count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
      $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
      if($count==0)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(0 ITEMS)</a>',$count);
      }
      if($count==1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(1 ITEM)</a>',$count);
      }
      if($count>1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(%s ITMES)</a>',$count);
      }
      echo $this->__('', $this->helper('core')->formatPrice($total, false));
    ?>

這對我有用...

使用 phtml 文件中的以下代碼獲取購物車中的商品數量。

<?php $helper = $this->helper('\Magento\Checkout\Helper\Cart');
      $noCartItems= $helper->getSummaryCount();
      echo $noCartItems;?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM