创建外部脚本以导入Quotes / Cart(其他CMS)。我的代码能够添加引号但不能创建购物车。当用户登录其帐户时,需要显示所有引用的项目。我也启用了持久购物车。
class QuoteMove extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\Event\Manager $eventManager,
\Magento\Framework\App\AreaList $areaList,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\App\Response\Http $response,
\Magento\Framework\ObjectManager\ConfigLoaderInterface $config,
\Magento\Framework\App\State $state,
\Magento\Framework\Filesystem $fileSystem,
\Magento\Framework\Registry $registry,
\Magento\Store\Model\Store $store,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\File\Csv $csvProcessor,
\Magento\Quote\Model\QuoteFactory $quote,
\Magento\Catalog\Model\Product $product
)
$quotes = [];
$email = email@domain.com;
$qty = xxx ;
$customerId = xxx ;
$this->customer = $this->getCustomerByEmail($email);
$customerId = $this->customer->getId();
$quote = $this->quotes[$customerId];
$quote->setCustomerNote(_NOTES_);
$quote->setCouponCode(_COUPON_CODE_);
$product = $this->_product->load('PRODUCT_ID'); //PRODUCT_ID= xx
$params = [];
$params['product'] = $productId;
$params['qty'] = intval($qty);
$options = [];
$options[_ATTRIBUTE_] = _VALUE_] ;
$params['super_attribute'] = $options;
$config = new \Magento\Framework\DataObject();
$config->setItem($params);
$quote->addProduct($product,$config);
$quote->save();
How to Save items in cart now ??
因此,当用户登录帐户时能够查看购物车中的商品。
答案 0 :(得分:0)
这就是您需要将产品添加到购物车的方式。
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Checkout\Model\Cart $cart,
\Magento\Framework\Data\Form\FormKey $formKey){
$this->_productRepository = $productRepository;
$this->_cart = $cart;
$this->formKey = $formKey;
}
$params = array(
'product' => --productID--,
'qty' => $product->getQty()
);
$_product = $this->_productRepository->getById(--productID--);
$this->_cart->addProduct($_product,$params);
$this->_cart->save();
将产品添加到购物车后,您可以将其保存到报价中。
答案 1 :(得分:0)
以下是我的答案:
更改$ quote-> save(); to $ quote-> collectTotals() - > save();
之后加载引号id并将updated_at字段日期更新为与创建日期相同。现在登录并检查您的购物车。项目将在那里查看。