2

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;
 }
}
asked Jun 19, 2017 at 14:29
1
  • Should try with XDebug with PHPStorm or Sublime Text. Commented Jun 19, 2017 at 14:32

2 Answers 2

1

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

answered Jun 17, 2019 at 14:33
1
  • actually DI have not been compiled due to errors Commented Jun 17, 2019 at 14:39
0

You try logging your variables

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($yourvariable);
answered Jun 20, 2017 at 7:29
1
  • For some reason it doesn't work Commented Jun 20, 2017 at 8:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.