2

I have the opportunity to pick up goods from the store. It is necessary to make this delivery method only available if the basket contains data from certain categories, and if there is at least one product that is not included in these categories, then hide this method of accession? How can I check which item is in the basket and from which categories it is?Here is the method in which I define to show or not show this delivery method

public function isActive()
    {

        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $scope = ScopeInterface::SCOPE_STORE;
        if (
               empty($this->_scopeConfig->getValue('general/store_information/city', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/country_id', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/postcode', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/street_line1', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/phone', $scope))
        ) {
            return false;
        }
        return true;
    }

How do I get data from the cart now and compare product categories and return true of false ? How can I pull out categories of goods from the session?

I'm trying to pull the data with Quote but my page goes into error then.

$items = $this->_session->getQuote()->getAllVisibleItems();
var_dump($items);

I checked if the data is coming, everything has come in the logs, how can I see what data is inside, how do I get to the product category

 public function isActive()
        {
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/somelog.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $quote = $this->_session->getQuote();
            $items = $quote->getAllVisibleItems();
            $class = get_class($items);
            $logger->info($class); 

somelog.log

2019-03-22T08:51:10+00:00 INFO (6): Magento\Checkout\Model\Session\Interceptor
2019-03-22T08:52:11+00:00 INFO (6): Magento\Quote\Model\Quote\Interceptor

model

<?php

namespace Chester\ShippingCollect\Model\Carrier;

/* Parents, traits and interfaces */
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;

/* Parent dependencies */
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory as RateErrorFactory;
use \Psr\Log\LoggerInterface;

use \Magento\Checkout\Model\Session;

/* Class dependencies */
use Magento\Shipping\Model\Rate\ResultFactory as RateResultFactory;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory as RateMethodFactory;

/* Invokables */
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Store\Model\ScopeInterface;

class Collect extends AbstractCarrier implements CarrierInterface
{

    protected $_code = 'chester_collect';

    protected $_session;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        RateErrorFactory $rateErrorFactory,
        Session $session,
        LoggerInterface $logger,
        RateResultFactory $rateResultFactory,
        RateMethodFactory $rateMethodFactory,
        array $data = []
    ) {
        $this->_rateResultFactory = $rateResultFactory;
        $this->_session = $session;
        $this->_rateMethodFactory = $rateMethodFactory;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }


    public function isActive()
    {
//        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/somelog.log');
//        $logger = new \Zend\Log\Logger();
//        $logger->addWriter($writer);
//        $quote = $this->_session->getQuote();
//        $items = $quote->getAllVisibleItems();
//        $class = get_class($items);
//        $logger->info($class);
//        }



//        return $cartItems;

        //$items = $this->_session->getQuote()->getAllVisibleItems();
        //var_dump($items);

        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $scope = ScopeInterface::SCOPE_STORE;
        if (
               empty($this->_scopeConfig->getValue('general/store_information/city', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/country_id', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/postcode', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/street_line1', $scope))
            || empty($this->_scopeConfig->getValue('general/store_information/phone', $scope))
        ) {
            return false;
        }

        return true;
    }

    public function isTrackingAvailable()
    {
        return false;
    }

    public function getAllowedMethods()
    {
        return [$this->_code => $this->getConfigData('name')];
    }

    protected function getShippingPrice()
    {
        $configPrice = $this->getConfigData('price');
        $shippingPrice = $this->getFinalPriceWithHandlingFee($configPrice);
        return $shippingPrice;
    }

    public function collectRates(RateRequest $request)
    {
        if (!$this->isActive()) {
            return false;
        }

        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->_rateResultFactory->create();

        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->_rateMethodFactory->create();

        $method->setCarrier($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod($this->_code);
        $method->setMethodTitle($this->getConfigData('name'));

        $amount = $this->getShippingPrice();

        $method->setPrice($amount);
        $method->setCost($amount);

        $result->append($method);

        return $result;
    }

}

Then I received id goods, but how to get his category now?

 $items = $this->_session->getQuote()->getAllItems();
 foreach ($items as $item){
 $logger->info($item->getProductId());
0

Please try below method to get the cart items.

use Magento\Checkout\Model\Session;

public function __construct(
        ...
        Session $session
        ...
    ) {
        ...
        $this->_session = $session;
        ...
    }

public function yourFunction()
{
    $items = $this->_session->getQuote()->getAllVisibleItems();
}

Thanks

  • I try it, but I get an error – Рома Лытарь Mar 22 at 8:19
  • Can you please share the error? – Moin Malek Mar 22 at 8:22
  • When displaying data, the page just gives up "Something went wrong while desplaying this wedpage" – Рома Лытарь Mar 22 at 8:27
  • Edited question – Рома Лытарь Mar 22 at 8:30
  • Can you please check the log file or enable developer mode to check this? – Moin Malek Mar 22 at 8:38
0

Can you post your entire class code base. Need to check what are all the classes are you are passing in your delivery method constructor function. Are you passing the checkout session class in your delivery method file?

0

You can get the items information from below class.

\Magento\Quote\Model\Quote\Address\RateRequest $request

$quoteItems = $request->getAllItems();

/** @var $item \Magento\Quote\Model\Quote\Item */
foreach ($allItems as $item) {
    // Your checks on each product item against category here //
}

This will definitely give the items information which is present in your quote.

  • do not quite understand your code, where do I need to insert it? \Magento\Quote\Model\Quote\Address\RateRequest $request And where did the variable come from $allItems? – Рома Лытарь Mar 22 at 10:11
  • I tried to deduce so, all the same nothing was deduced $items = $this->_session->getQuote()->getAllItems(); /** @var $item \Magento\Quote\Model\Quote\Item */ foreach ($items as $item) { print_r($item->getName()); } – Рома Лытарь Mar 22 at 10:13

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.