1

How to get a list of product categories id in the basket? Here I get the name of the product.

use \Magento\Checkout\Model\Session;
class Collect extends AbstractCarrier implements CarrierInterface
{
    protected $_code = 'chester_collect';
    protected $_session;
    public function __construct(
        Session $session,
        array $data = []
    ) {
        $this->_session = $session;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }

public function isActive()
    {
        /** @var $item \Magento\Quote\Model\Quote\Item */
        $items = $this->_session->getQuote()->getAllItems();
        foreach ($items as $item){
          var_dump($item->getName());
        }          
    } 

But how to get all the categories of goods that are in the basket?

3

I have modified your code with below code:

use \Magento\Checkout\Model\Session;

class Collect extends AbstractCarrier implements CarrierInterface
{
    protected $_code = 'chester_collect';
    protected $_session;
    private $_objectManager;
    public function __construct(
        Session $session,
        \Magento\Framework\ObjectManagerInterface $objectmanager,
        array $data = []
    ) {
        $this->_session = $session;
        $this->_objectManager = $objectmanager;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }

    public function isActive()
    {
        /** @var $item \Magento\Quote\Model\Quote\Item */
        $items = $this->_session->getQuote()->getAllItems();
        foreach ($items as $item) {
            var_dump($item->getName());
            $productid = $item->getProductId();
            $product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($productid);
            $categoriesIds = $product->getCategoryIds();
            var_dump($categoriesIds);
        }
    }
}
1
  • 1
    Using objectManager is a bad solution. – Black Sep 8 '20 at 14:01
2

Code for Getting Cart Items and get Categories Ids and names by loading Products from Items

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// retrieve quote items array
$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
    $productid = $item->getProductId();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid);
    $categoriesIds = $product->getCategoryIds(); /*will return category ids array*/
    foreach($categoriesIds as $categoryId){
        $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
        echo $cat->getName();
    }
}

I hope this will help.

1
  • 1
    It is prohibited by magento to use objectManager – Black Sep 8 '20 at 14:01

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.