0

I've created a page where I have a list of products. In each row (1 row = 1 product), I have a button "add to cart" :

<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="POST" id="form-<?php echo $numArticle; ?>">
    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
    <input type="hidden" name="qty" value="<?php  /* @escapeNotVerified */ echo $derniereQuantite; ?>" class="qty">
    <?php echo $block->getBlockHtml('formkey')?>
    <?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface'); ?>
    <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary cadencier">
        <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
    </button>
</form>

The product is indeed added to the cart. But I need to get a data (like the sku of the product). So I did :

if('POST' == $this->getRequest()->getMethod()){
    $post = $this->getRequest()->getParams();
    ...

But the POST is empty, even if the form is a POST form. Then I tried with GET.

if('GET' == $this->getRequest()->getMethod()){

An empty array, but this is giving me a white page (I'm already on debug mode), but the condition is always true (I believe that's because what's next).

Then I tried to print $_REQUEST... And I have a result. I have a success message saying that my product is indeed added to the cart. It's like there was an intermediary step between the post and the data send to the controller. So how can I get data from this intermediary step to the controller, and than passed is to the block phtml?

EDIT : or, if it's possible, remove/improuve this form by using ajax, it would be even better.

0

You can check the posted data to a controller like this:

public function execute()
{
    if ($this->getRequest()->isPost()) {
        $product = $this->getRequest()->getPost('product');
        $qty = $this->getRequest()->getPost('qty');
        if (!empty($product) && !empty($qty)) {
            //your logic here
        } else {
            $this->_messageManager->addError(__('You must fill all the fields !'));
            $this->_redirect('some/module/url');
        }
    } else {
        $this->_messageManager->addError(__('Error !'));
        $this->_redirect('some/module/url');
    }
}
|improve this answer|||||
0

I've added this line into my file :

<script type="text/x-magento-init">
        {
            "[data-role=tocart-form]": {
                "catalogAddToCart": {
                   "bindSubmit": true
                 }
            }
        }
 </script>

This is doing an ajax submit.Better with what I've done. Then I've been through the result and take the data that I wanted. That's really easier like this

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.