2017-08-30 59 views
0

创建一个外部脚本来导入行情/购物车(其他CMS)。我的代码能够添​​加报价但不能创建购物车。当用户登录到他们的帐户时需要显示所有报价项目。我也启用了持久性购物车。导入报价并将产品添加到购物车中Magento 2

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 protected]; 
       $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

这里是答案我有它的工作:

更改$ - 经济通>保存();到$ quote-> collectTotals() - > save();

之后加载引用ID并更新updated_at字段日期与创建日期相同。现在登录并检查您的购物车。项目将在那里查看。

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(); 

将产品添加到购物车后,您可以将其保存到报价中。

+0

此代码是当他离线时从外部将购物车注入客户帐户,以便当客户登录时,他可以在购物车中看到该物品。例如:我们添加到购物车项目,然后注销并重新登录购物车。用其他方式从旧数据库或其他数据库导入magento2。你的代码不适合这种情况。 – Vikram

相关问题