skip to Main Content

Creating an external script to import Quotes/Cart (other CMS). My code able to add quote but not creating cart. Need all quoted items to show when user login to their account. I have enabled persistent cart also.

   class QuoteMove extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface 


  public function __construct(
    MagentoFrameworkObjectManagerInterface $objectManager,
    MagentoFrameworkEventManager $eventManager,
    MagentoFrameworkAppAreaList $areaList,
    MagentoFrameworkAppRequestHttp $request,
    MagentoFrameworkAppResponseHttp $response,
    MagentoFrameworkObjectManagerConfigLoaderInterface $config,
    MagentoFrameworkAppState $state,
    MagentoFrameworkFilesystem $fileSystem,
    MagentoFrameworkRegistry $registry,
    MagentoStoreModelStore $store,
    PsrLogLoggerInterface $logger,
    MagentoFrameworkFileCsv $csvProcessor,
    MagentoQuoteModelQuoteFactory $quote,
    MagentoCatalogModelProduct $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 MagentoFrameworkDataObject();
                    $config->setItem($params);
                    $quote->addProduct($product,$config);
                    $quote->save();      

                How to Save items in cart now ??

So when user login into account able to view items in cart.

2

Answers


  1. Chosen as BEST ANSWER

    Here is answer I have got it working :

    Change $quote->save(); to $quote->collectTotals()->save();

    after that load quote id and update updated_at field date to same as created date. Now login and check your cart. Item will be viewed there.


  2. This is how you need to add product to cart.

    public function __construct(
     MagentoCatalogModelProductRepository $productRepository, 
    MagentoCheckoutModelCart $cart, 
    MagentoFrameworkDataFormFormKey $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();
    

    Once product is added to the cart, the you can save it to the quote.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search