2017-08-30 62 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

相關問題