0

I am adding product into cart from a custom controller add wanted to add additional note/information to show in cart and order but it's only showing the on cart page not in order item or quote item, below is my code:

public function execute() {
        try {
            $post = $this->getRequest()->getPostValue();
            $selected_products = $post['selected_products'];
            $qty = $post['qty'];
            $time_period = $post['time_period'];
            $selected_products = array_values($selected_products);
            for($i=0;$i<count($selected_products);$i++) {
                $product_id = $selected_products[$i];
                $product_qty = $qty[$i];
                $params[$i] = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' => $product_id,
                    'qty' => $product_qty
                );
            }
            $additionalOptions['subscription'] = [
                'label' => 'Subscription',
                'value' => $time_period,
            ];
            foreach ($params as $product) {
                $productModel = $this->_product->create();
                $productModel->load($product['product']);
                $productModel->addCustomOption('additional_options', serialize($additionalOptions));
                $this->_cart->addProduct($productModel, $product);
            }
            $this->_cart->save();
            //$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            if($this->_cart->save()) { 
                echo 'Done';
            } else {
                echo 'No';
            }
        } catch(Exception $e) {
            echo 'No';
        }
        die;
    }

I wanted to achieve same functionality which is mentioned here but this solution work with observer and I wanted to do it within the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.