如何在之前的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));
}
}
我正在接受
$ items = $ this-> _cart-> getAllVisibleItems();作为空值但产品在购物车中
答案 0 :(得分: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;
}