Magento 2: Add to cart button in category page with pre-selected attributes of configurable childadd to cart button is not workingGet javascript value without html or script tagsProblem inserting template below add to cart with rwd themeMagento 2 Grouped Product Add Product with Custom Options to CartAdd to Cart Button Not Working Magento 2I want to add parent product to cart using child product id, it's configurable options in magento 2.1Add to cart button added the same product issueAdditional Add to Cart Buttonhow to pass product page selected value to cart in magento2How to create custom form in Magento 2.2.3

Why did moving the mouse cursor cause Windows 95 to run more quickly?

What is the highest level of accuracy in motion control a Victorian society could achieve?

What happens if the limit of 4 billion files was exceeded in an ext4 partition?

How do I check that users don't write down their passwords?

Convert integer to full text string duration

What can a novel do that film and TV cannot?

In the Seventh Seal why does Death let the chess game happen?

What is the maximum amount of diamond in one Minecraft game?

Taking my Ph.D. advisor out for dinner after graduation

Does 5e have an equivalent of the Psychic Paper from Doctor Who?

n-level Ouroboros Quine

Why would "dead languages" be the only languages that spells could be written in?

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

Way to see all encrypted fields in Salesforce?

Has there ever been a cold war other than between the U.S. and the U.S.S.R.?

The Purpose of "Natu"

Taking advantage when the HR forgets to communicate the rules

How to play a D major chord lower than the open E major chord on guitar?

What is exact meaning of “ich wäre gern”?

What do you call the angle of the direction of an airplane?

Is it possible to spoof an IP address to an exact number?

Shipped package arrived - didn't order, possible scam?

Will Jimmy fall off his platform?

How come a desk dictionary be abridged?



Magento 2: Add to cart button in category page with pre-selected attributes of configurable child


add to cart button is not workingGet javascript value without html or script tagsProblem inserting template below add to cart with rwd themeMagento 2 Grouped Product Add Product with Custom Options to CartAdd to Cart Button Not Working Magento 2I want to add parent product to cart using child product id, it's configurable options in magento 2.1Add to cart button added the same product issueAdditional Add to Cart Buttonhow to pass product page selected value to cart in magento2How to create custom form in Magento 2.2.3






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to create an add to cart button for a child of a configurable product in the category page.



I'm trying with this code where I add the super_attribute hidden fields with the values of the attribute.



<?php $postParams = $block->getAddToCartPostParams($_product); ?>

<form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
<input type="hidden" name="product" value="<?php echo $_product->getId(); ?>">
<input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
<?php foreach ($attributeOptions as $attr) ?>
<input type="hidden" name="super_attribute[<?php echo $_child->getResource()->getAttribute($attr)->getData("attribute_id")?>]" value="<?php echo $super_attr[$attr] ?>">
<?php ?>
<?php echo $block->getBlockHtml('formkey')?>
<div class="fieldset">
<div class="actions">
<button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary">
<span><?php echo __('Add to Cart') ?></span>
</button>
</div>
</div>
</form>


The result is (for example)



<form data-role="tocart-form" action="baseurl/product-name?options=cart" method="post">
<input type="hidden" name="product" value="5858">
<input type="hidden" name="uenc" value="abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd">
<input type="hidden" name="super_attribute[185]" value="10">
<input type="hidden" name="super_attribute[186]" value="12">
<input name="form_key" type="hidden" value="DDAAkjkkkkkkj">
<div class="fieldset">
<div class="actions">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</div>
</div>
</form>


But when I try to add to cart, I'm redirected to the product page with the error "You need to choose options for your item."



I'm working on the v2.3.1.



Do you have suggestions on how to specify the options for the add to cart in the add to cart button? Thanks!










share|improve this question






























    0















    I want to create an add to cart button for a child of a configurable product in the category page.



    I'm trying with this code where I add the super_attribute hidden fields with the values of the attribute.



    <?php $postParams = $block->getAddToCartPostParams($_product); ?>

    <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
    <input type="hidden" name="product" value="<?php echo $_product->getId(); ?>">
    <input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
    <?php foreach ($attributeOptions as $attr) ?>
    <input type="hidden" name="super_attribute[<?php echo $_child->getResource()->getAttribute($attr)->getData("attribute_id")?>]" value="<?php echo $super_attr[$attr] ?>">
    <?php ?>
    <?php echo $block->getBlockHtml('formkey')?>
    <div class="fieldset">
    <div class="actions">
    <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary">
    <span><?php echo __('Add to Cart') ?></span>
    </button>
    </div>
    </div>
    </form>


    The result is (for example)



    <form data-role="tocart-form" action="baseurl/product-name?options=cart" method="post">
    <input type="hidden" name="product" value="5858">
    <input type="hidden" name="uenc" value="abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd">
    <input type="hidden" name="super_attribute[185]" value="10">
    <input type="hidden" name="super_attribute[186]" value="12">
    <input name="form_key" type="hidden" value="DDAAkjkkkkkkj">
    <div class="fieldset">
    <div class="actions">
    <button type="submit" title="Add to Cart" class="action tocart primary">
    <span>Add to Cart</span>
    </button>
    </div>
    </div>
    </form>


    But when I try to add to cart, I'm redirected to the product page with the error "You need to choose options for your item."



    I'm working on the v2.3.1.



    Do you have suggestions on how to specify the options for the add to cart in the add to cart button? Thanks!










    share|improve this question


























      0












      0








      0








      I want to create an add to cart button for a child of a configurable product in the category page.



      I'm trying with this code where I add the super_attribute hidden fields with the values of the attribute.



      <?php $postParams = $block->getAddToCartPostParams($_product); ?>

      <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
      <input type="hidden" name="product" value="<?php echo $_product->getId(); ?>">
      <input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
      <?php foreach ($attributeOptions as $attr) ?>
      <input type="hidden" name="super_attribute[<?php echo $_child->getResource()->getAttribute($attr)->getData("attribute_id")?>]" value="<?php echo $super_attr[$attr] ?>">
      <?php ?>
      <?php echo $block->getBlockHtml('formkey')?>
      <div class="fieldset">
      <div class="actions">
      <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary">
      <span><?php echo __('Add to Cart') ?></span>
      </button>
      </div>
      </div>
      </form>


      The result is (for example)



      <form data-role="tocart-form" action="baseurl/product-name?options=cart" method="post">
      <input type="hidden" name="product" value="5858">
      <input type="hidden" name="uenc" value="abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd">
      <input type="hidden" name="super_attribute[185]" value="10">
      <input type="hidden" name="super_attribute[186]" value="12">
      <input name="form_key" type="hidden" value="DDAAkjkkkkkkj">
      <div class="fieldset">
      <div class="actions">
      <button type="submit" title="Add to Cart" class="action tocart primary">
      <span>Add to Cart</span>
      </button>
      </div>
      </div>
      </form>


      But when I try to add to cart, I'm redirected to the product page with the error "You need to choose options for your item."



      I'm working on the v2.3.1.



      Do you have suggestions on how to specify the options for the add to cart in the add to cart button? Thanks!










      share|improve this question
















      I want to create an add to cart button for a child of a configurable product in the category page.



      I'm trying with this code where I add the super_attribute hidden fields with the values of the attribute.



      <?php $postParams = $block->getAddToCartPostParams($_product); ?>

      <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
      <input type="hidden" name="product" value="<?php echo $_product->getId(); ?>">
      <input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
      <?php foreach ($attributeOptions as $attr) ?>
      <input type="hidden" name="super_attribute[<?php echo $_child->getResource()->getAttribute($attr)->getData("attribute_id")?>]" value="<?php echo $super_attr[$attr] ?>">
      <?php ?>
      <?php echo $block->getBlockHtml('formkey')?>
      <div class="fieldset">
      <div class="actions">
      <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary">
      <span><?php echo __('Add to Cart') ?></span>
      </button>
      </div>
      </div>
      </form>


      The result is (for example)



      <form data-role="tocart-form" action="baseurl/product-name?options=cart" method="post">
      <input type="hidden" name="product" value="5858">
      <input type="hidden" name="uenc" value="abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd">
      <input type="hidden" name="super_attribute[185]" value="10">
      <input type="hidden" name="super_attribute[186]" value="12">
      <input name="form_key" type="hidden" value="DDAAkjkkkkkkj">
      <div class="fieldset">
      <div class="actions">
      <button type="submit" title="Add to Cart" class="action tocart primary">
      <span>Add to Cart</span>
      </button>
      </div>
      </div>
      </form>


      But when I try to add to cart, I'm redirected to the product page with the error "You need to choose options for your item."



      I'm working on the v2.3.1.



      Do you have suggestions on how to specify the options for the add to cart in the add to cart button? Thanks!







      magento2 php addtocart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 26 at 15:37







      Gianni

















      asked Jun 26 at 15:33









      GianniGianni

      63 bronze badges




      63 bronze badges




















          0






          active

          oldest

          votes














          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279755%2fmagento-2-add-to-cart-button-in-category-page-with-pre-selected-attributes-of-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279755%2fmagento-2-add-to-cart-button-in-category-page-with-pre-selected-attributes-of-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form