Magento 2 – How to Debug Variables Inside Plugin

magento2plugin

I tried die($some_var) and var_dump($some_var) but nothing happens.

DI:

<!-- di.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
  <type name="Magento\Quote\Model\Quote\Address">
    <plugin name="namespace.checkout.shipping" type="Namespace\Checkout\Plugin\Shipping" sortOrder="1" />
  </type>
</config>

Plugin:

<!-- Shipping.php -->
<?php
namespace Namespace\Checkout\Plugin;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductRepository;
use Namespace\Checkout\Helper\Data;

class Shipping
{
    private $productRepository;
    private $helper;

    public function __construct(ProductRepository $productRepository, Data $helper)
    {
        $this->productRepository = $productRepository;
        $this->helper = $helper;
    }

    public function afterGetShippingRatesCollection(Address $subject, AbstractCollection $rates)
    {
        $items = $subject->getQuote()->getItems();

        /* Something strange is happening here 
           0. If I just 'return $rates' without any conditions and custom code, It work perfectly. But I want to filter rates.
           1. When I add following code (if(!$items) { return $rates; }), it throws LocalizedException: "Please specify a shipping method" in vendor/magento/module-quote/Model/QuoteManagement.php(427)
           2. Without 'if(!$items){...}' I've got "Invalid argument supplied for foreach()" for second foreach in this function
         */
        if (!$items) {  // there is a some strange magic, and I want it to debug.
            return $rates;         
        }
        /* end strange */

        /** @var Rate $rate */
        foreach ($rates->getItems() as $key => $rate) {
        /** @var Item $item */
            foreach ($items as $item) {
                try {
                    /** @var Product $product */
                    $product = $this->productRepository->get($item->getProduct()->getSku());
                } catch (NoSuchEntityException $e) {
                    // product not found
                    continue;
                }
                if (!$this->helper->isShippingRateAvailable($rate->getCode(),$product)) {
                    $rates->removeItemByKey($key);
                    continue 2;
                }
            }
        }
        return $rates;
    }
}

Best Answer

Your plugin does not in list of interceptors. It does not work. To figure out why:

  1. Go to generated code - generated folder or var\generation. It depends on Magento version.

  2. Go to method \Magento\Quote\Model\Quote\Interceptor::getShippingRatesCollection() in the folder with generated code.

  3. var_dump $pluginInfo variable. Your plugin should be in this array. If not - rerun setup:di:compile

Related Topic