0

How can I get cart products in a custom module plugin in magento 2 on before-save shipping info hear already i'm taking the customer address its working fine like this i need to get the cart products also

/app/code/Sem/Shipment/Plugin/Checkout/Model/ShippingInformationManagement.php

         <?php

namespace Sem\Shipment\Plugin\Checkout\Model;
use Magento\Framework\Exception\StateException;
use Magento\Checkout\Model\Session;

class ShippingInformationManagement
{

    protected $_messageManager;
    protected $jsonResultFactory;
    protected $_quote;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Quote\Model\QuoteFactory $quoteFactory
    ) {
        $this->_messageManager = $messageManager;
        $this->jsonResultFactory = $jsonResultFactory;
          $this->_quote= $quoteFactory;
    }

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation

    )
    {

     $cart = $this->_quote->create()->get($cartId);
    $items = $cart->getAllVisibleItems();
    $resulta = [];
    if (count($items) > 0){
        foreach ($items as $item)
            $resulta[] = $item->getName();
    }
    //print_r($resulta);
    $address = $addressInformation->getShippingAddress();
    $postcode = $address->getData('postcode');
    $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $result = $this->jsonResultFactory->create();
    throw new StateException(__($resulta[0]));              
}
}

1 Answer 1

0

I just assume you need to echo the product name only. If you need other fields, please let me know.

In your custom module PHP function:

protected $_quote;

public function __construct(
    \Magento\Quote\Model\QuoteFactory $quoteFactory
    /*Other DIs if you have*/
)
{
    $this->_quote= $quoteFactory;
    /*Other DIs  if you have*/
}

public function beforeSaveAddressInformation(
    \Magento\Checkout\Api\ShippingInformationManagementInterface $subject,
    $cartId,
    \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
){
    //Inside 
    $cart = $this->_quote->create()->get($cartId);
    $items = $cart->getAllVisibleItems();
    $result = [];
    if (count($items) > 0){
        foreach ($items as $item)
            $result[] = $item->getName();
    }
    return $result;
}
17
  • i need full details of product inside the plugin i need to check some details hear
    – amith lal
    Jun 23, 2017 at 5:52
  • Can you specify what details you want to include?
    – PY Yick
    Jun 23, 2017 at 5:56
  • I am trying to check the delivery availability using the zip code for that i fetch the address information using "beforeSaveAddressInformation" method in plugin feature so there i get the delivery pin code now i need to get the seller ZIP(post code) and cart total price product weight etc.. based on the result from hear i need activate the nexr stem (Payment) hear i'm posing my plugin code
    – amith lal
    Jun 23, 2017 at 6:04
  • in $items i'm getting empty value
    – amith lal
    Jun 23, 2017 at 6:38
  • Try to use \Magento\Checkout\Model\Session instead, see my edited codes
    – PY Yick
    Jun 23, 2017 at 7:12

Your Answer

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

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