2017-06-23 29 views
0

如何在購買magento 2的自定義模塊插件的購物車產品之前保存運輸信息聽說我已將客戶地址的工作狀態這樣我需要讓車產品也有沒有任何方法可以在Magento 2的插件功能中獲取購物車的詳細信息

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

class ShippingInformationManagement 
{ 

    protected $_messageManager; 
    protected $jsonResultFactory; 
    protected $_cart;  

    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\Checkout\Model\Cart $cartModel 
    ) { 
     $this->_messageManager = $messageManager; 
     $this->jsonResultFactory = $jsonResultFactory; 
     $this->_cart = $cartModel; 
    } 

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

    ) 
    { 

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

Heare我得到

$ items = $ this - > _ cart-> getAllVisibleItems();作爲空值,但產品在購物車中

回答

0

我們假設您只需要產品名稱。

在您的自定義模塊PHP函數:

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(); 
      //If you need other fields, please let me know. 
      //$result[] = $item->getData(); 
    } 
    return $result; 
} 
相關問題