Question

I have created 5 Custom Input Fields (not Attributes) in the Product View Page.

The Customer can write his Values inside if he want to modify the Product Values.

I want to pass them to Cart, and later to Checkout. At the End I want to view the Values in the Order.

How can I achive this?

Was it helpful?

Solution 2

With the help of @AmitSaini i figured out.

I am currently using Magento 2.3.6

How to "add to cart" a product with custom input field and save it to Database?

Follow this link and check Renon Stewart answer.

If you get in Product View this error: {"message":"Unable to unserialize value. Error: Syntax error"}

or

if you get the jQuery error: Uncaught Error: [object Object]

Change in the inside of SalesModelServiceQuoteSubmitBeforeObserver.php and CheckoutCartProductAddAfterObserver.php :

serialize to json_encode

and

unserialize to json_decode

Now the next problem. If you tried before to go to the checkout with serialze and you were logged in with an user account, you need to delete the last quote item.

You need to delete in the database inside the quote table the last quote with the used e-mail.

Hint:

$post = $this->_request->getParam('cloudways');

This line, parameter defines the "name" field inside the addtocart.phtml responsible for this input field:

<input type="text" name="cloudways[remarks]" id="remarks" maxlength="255" placeholder="Remarks" />

"remarks" in this case is the json configuration name field.

And one more thing.

If you can't complete the checkout process, change the SalesModelServiceQuoteSubmitBeforeObserver.php file on line 50:

if(count($additionalOptions) > 0) 

to:

if(is_array($additionalOptions) && count($additionalOptions) > 0){

OTHER TIPS

I don't know what is your goal, but you try using checkoutSession to do this.

Here an untested example:

<?php
declare(strict_types = 1);

namespace Vendor\Module\Controller\Demo;


class Demo extends \Magento\Framework\App\Action\Action
{

    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->_checkoutSession = $checkoutSession;
    }


    public function execute()
    {
        $this->_checkoutSession->setMyValue('test');
        $myValue = $this->_checkoutSession->getMyValue();

        var_dump($myValue);
        var_dump($this->_checkoutSession->getData());
        exit;
    }
}

You can read more about the custom sessions of magento here.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top