0

I have a custom module. I want to display category name of each product in the cart

Please see my code

checkout_index_index.xml

<body>
    <referenceBlock name="additional.product.info">
        <block class="Company\Module\Block\Showcategory" name="showcategory" template="Company_Module::checkout/cart/categoryname.phtml"/>
    </referenceBlock>
</body>

categoryname.phtml

<?php $quoteItems = $block->getCheckoutSession(); ?>
<?php echo count($quoteItems); ?>
<?php foreach ($quoteItems as $item): 
    $pid = $item->getProductId();
    $product = $block->getProductRepository()->getById($pid);
    $cats = $product->getCategoryIds();
    foreach ($cats as $category_id) {
        $_cat = $block->getCategoryRepository()->get($category_id, $block->getStoreId());
        echo $_cat->getName();
    }
?>

<?php endforeach; ?>

Showcategory.php

<?php 

namespace Company\Module\Block;  

use Magento\Framework\View\Element\Template\Context;
use Magento\Checkout\Model\Session;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

class Showcategory extends \Magento\Framework\View\Element\Template 
{
    /** @var CheckoutSession */
    private $checkoutSession;

    /** @var ProductRepository */
    private $productRepository;

    /** @var CategoryRepository */
    private $categoryRepository;

    /** @var StoreManager */
    private $storeManager;

    /**
     * @param CheckoutSession $checkoutSession
     * @param ProductRepository $productRepository
     * @param CategoryRepository $categoryRepository
     * @param StoreManager $storeManager
     */
    public function __construct(
        Context $context,
        Session $checkoutSession,
        ProductRepositoryInterface $productRepository,
        CategoryRepositoryInterface $categoryRepository,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->checkoutSession = $checkoutSession;
        $this->productRepository = $productRepository;
        $this->categoryRepository = $categoryRepository;
        $this->storeManager = $storeManager;
    }
    public function getCheckoutSession() {
        $quote = $this->checkoutSession->getQuote();
        $items = $quote->getAllVisibleItems();
        return $items;
    }

    public function getProductRepository() {
        $product = $this->productRepository;
        return $product;
    }

    public function getCategoryRepository() {
        $category = $this->categoryRepository;
        return $category;
    }

    public function getStoreId() {
        $storeId = $this->storeManager->getStore()->getId();
        return $storeId;
    }    
}

But it is showing all category names under each product. Please refer the image enter image description here

Please advice

  • I guess that you want show the category name from where you have do addtocart? – Amit Bera Feb 23 '18 at 11:32
  • I want to show the category name of the product for each product in the cart page. – E.B Feb 26 '18 at 4:11
1

I would suggest you to use categoryCollection instead of loading each category with a particular database query. This will result into a better performance when loading the cart page. Also, if $item is instance of quoteItem, you don't have to load the product again because you can find it inside quoteItem object. So, check the following code.

<?php $item = $block->getItem();
$product = $item->getProduct();
$cats = $block->getCategoryCollectionFactory()->create();
$cats->addAttributeToFilter('entity_id', $product->getCategoryIds());

foreach ($cats as $category) {
    echo $category->getName();
endforeach; ?> 

getCategoryCollectionFactory() should return an instance of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory

Also, i would suggest you to move the whole code inside your block and in phtml just call a method to take an array with category names.

  • What is function getItem(). Can you please add the code for function getItem() – E.B Feb 26 '18 at 4:14
  • You don't need to implement getItem() method as long as your block is child of additional.product.info block because, as you can see in it's template (module-checkout/view/frontend/templates/cart/additional/info.phtml), magento 2 set quote item on each child block. – George M Feb 26 '18 at 13:19
1

checkout_cart_index.xml

<referenceBlock name="additional.product.info">
        <block class="Gaurav\Companyname\Block\Showcategory" name="showcategory" template="Gaurav_Companyname::categoryname.phtml"/>
    </referenceBlock>

Gaurav\Companyname\Block\Showcategory.php

<?php 
namespace Gaurav\Companyname\Block;  
use Magento\Framework\View\Element\Template\Context;
use Magento\Checkout\Model\Session;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

class Showcategory extends \Magento\Framework\View\Element\Template 
{
    /** @var CheckoutSession */
    private $checkoutSession;

    /** @var ProductRepository */
    private $productRepository;

    /** @var CategoryRepository */
    private $categoryRepository;

    /** @var StoreManager */
    private $storeManager;

    /**
     * @param CheckoutSession $checkoutSession
     * @param ProductRepository $productRepository
     * @param CategoryRepository $categoryRepository
     * @param StoreManager $storeManager
     */
    public function __construct(
        Context $context,
        Session $checkoutSession,
        ProductRepositoryInterface $productRepository,
        CategoryRepositoryInterface $categoryRepository,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        //die('gaurav');
        parent::__construct($context, $data);
        $this->checkoutSession = $checkoutSession;
        $this->productRepository = $productRepository;
        $this->categoryRepository = $categoryRepository;
        $this->storeManager = $storeManager;
    }


    public function getProductRepository() {
        $product = $this->productRepository;
        return $product;
    }

    public function getCategoryRepository() {
        $category = $this->categoryRepository;
        return $category;
    }
    public function getStoreId() {
        $storeId = $this->storeManager->getStore()->getId();
        return $storeId;
    }  

  public function getCategoryName(){
    $item = $this->getItem(); 
    $pid = $item->getProductId();
    $product = $this->getProductRepository()->getById($pid);
    $cats = $product->getCategoryIds();
    $cat_name = array();
    foreach ($cats as $category_id) {
        $_cat = $this->getCategoryRepository()->get($category_id, $this->getStoreId());
        $cat_name[] = $_cat->getName();

    }
    return $cat_name;
  }

}

Gaurav\Companyname\view\frontend\templates\categoryname.phtml

<?php 
$category_names = $block->getCategoryName();
foreach($category_names as $cat_name){
    echo $cat_name;
}
?>
0
<?php $item = $block->getItem(); 
$pid = $item->getProductId();
$product = $block->getProductRepository()->getById($pid);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
    $_cat = $block->getCategoryRepository()->get($category_id, $block->getStoreId());
    echo $_cat->getName();
endforeach; ?>

please check above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.