1

I'm using magento 1.9.1 and on a custom listing page I'm trying to add an add to basket form. This is supposed to add to the basket using a query string.

<?php $formKey = Mage::getSingleton('core/session')->getFormKey();?>
<form action="/shop/checkout/cart/add?product=4149&qty=1&super_attribute[136]=147?super_attribute[240]=1636" method="post">
<input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />

<input type="submit" value="Add to basket" />
</form>

I've tried a few variations on how the url is formatted and I will look at Un-hardcoding it once ,i have got a working format.

But for now it just keeps directing to the product page and asking me to select options.

I've read this, this and this about formatting a query string for this. I'm certain that I've done this on older versions of magento in the past with the format given in the code above.

FYI -

I need to add it as a config product and set the options rather than add the simple directly.

All help welcome.

  • are your want direct url query or form?? – Amit Bera Apr 23 '15 at 18:06
2

I you use form then It will good idea to use

  1. Dynamic cart url using function $this->helper('checkout/cart')->getAddUrl($product) in form action
  2. use the super attribute as input field.
<?php $formKey = Mage::getSingleton('core/session')->getFormKey();?>
    <form action="<?php echo $this->helper('checkout/cart')->getAddUrl($product) ?>" method="post">
    <input type="hidden" name="form_key" value="super_attribute[136]" />
    <input type="hidden" name="super_attribute[136]" value="147" />
    <input type="hidden" name="super_attribute[240]" value="11636" />
    <input type="hidden" name="qty" value="1" />
    <input type="submit" value="Add to basket" />
    </form>
0

I think @amitbera answer would work so I upvoted it but I had this working before I saw his so I'll post it also.

<?php
    $url = '/shop/checkout/cart/add?product='.$_product->getId().'&qty=1';
    $productAttributeOptions = $_config->getTypeInstance(true)->getConfigurableAttributesAsArray($_config);
    foreach ($productAttributeOptions as $productAttribute) {
        $url .= '&super_attribute['.$productAttribute['attribute_id'].']='.$_originalProduct->getData($productAttribute['attribute_code']);
    }

    if($_originalProduct->getData('is_in_stock')):
    $formKey = Mage::getSingleton('core/session')->getFormKey();

?>
    <form action="<?php echo $url; ?>" method="post">
    <input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />
    <input type="hidden" name="in_cart" value="1" />
    <input type="hidden" name="return_url" value="<?php echo $this->getUrl('checkout/cart') ?>" />
    <input type="submit" value="Re-order this shoe" />
    </form>
    <?php endif; ?>

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.