2017-06-23 27 views
0

magento 2のカスタムモジュールプラグインでカート製品を入手するにはどうすればよいですか?このように私はMagento 2のプラグイン機能でカートの詳細を取得する方法はありますか

を取得しています
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 /app/code/Sem/Shipment/Plugin/Checkout/Model/ShippingInformationManagement.php

カートの製品を取得する必要があります

$ 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; 
} 
関連する問題