Search code examples
phpmagentomagento-1.9configurable-product

"Please specify the product's option(s)" in Magento 1.9.2


I've a Magento 1.9 server and the products is set on, the adding into cart for simple product is working smoothly and fine, but when I try to add configurable product this message return.

Please specify the product's option(s).

Now I was tracing the code and working on it, till I reach here.

app/code/core/Mage/Checkout/controllers/CartController

in this file this code for adding the product exists:

/**
 * Add product to shopping cart action
 *
 * @return Mage_Core_Controller_Varien_Action
 * @throws Exception
 */
public function addAction()
{
    if (!$this->_validateFormKey()) {
        $this->_goBack();
        return;
    }
    $cart   = $this->_getCart();
    $params = $this->getRequest()->getParams();

    try {
        if (isset($params['qty'])) {
            $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
            );
            $params['qty'] = $filter->filter($params['qty']);


        }

        $product = $this->_initProduct();
        $related = $this->getRequest()->getParam('related_product');


        /**
         * Check product availability
         */
        if (!$product) {
            $this->_goBack();
            return;
        }
        echo 'here';
        ?>
            <pre>
                <?php print_r($params); ?>
            </pre>
        <?php
            exit;
        ?>
        <?php

        $cart->addProduct($product, $params);
        if (!empty($related)) {
            $cart->addProductsByIds(explode(',', $related));
        }

        $cart->save();

        $this->_getSession()->setCartWasUpdated(true);

        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete',
            array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
        );

        if (!$this->_getSession()->getNoCartRedirect(true)) {
            if (!$cart->getQuote()->getHasError()) {
                $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                $this->_getSession()->addSuccess($message);
            }
            $this->_goBack();
        }
    } catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
            }
        }

        $url = $this->_getSession()->getRedirectUrl(true);
        if ($url) {
            $this->getResponse()->setRedirect($url);
        } else {
            $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
        }
    } catch (Exception $e) {
        $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
        Mage::logException($e);
        $this->_goBack();
    }
}

In this code I used this simple code to achieve where I am and what data I have.

echo 'here';
?>
    <pre>
        <?php print_r($params); ?>
    </pre>
<?php
    exit;
?>
<?php

This simple code return the arguments I have for my Configurable Product.

Array
(
    [uenc] => aHR0cDovL3N0YWdpbmcudmF0dHJlbmEuY29tL2luZGV4LnBocC9nZW5lcmFsLWplYW5zMTMuaHRtbA,,
    [product] => 65
    [form_key] => qIGyp6YDp7kQjibA
    [related_product] => 
    [super_attribute] => Array
        (
            [168] => M
        )

    [qty] => 1
)

That mean all attributes is chosen, but after this simple code when I reach the

$cart->addProduct($product, $params);

it suppose to go to this file

app/code/core/Mage/Checkout/Model/Cart.php

On this function

/**
 * Add product to shopping cart (quote)
 *
 * @param   int|Mage_Catalog_Model_Product $productInfo
 * @param   mixed $requestInfo
 * @return  Mage_Checkout_Model_Cart
 */
public function addProduct($productInfo, $requestInfo=null)
{
    $product = $this->_getProduct($productInfo);
    $request = $this->_getProductRequest($requestInfo);

    $productId = $product->getId();

    if ($product->getStockItem()) {
        $minimumQty = $product->getStockItem()->getMinSaleQty();
        //If product was not found in cart and there is set minimal qty for it
        if ($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty
            && !$this->getQuote()->hasProductId($productId)
        ){
            $request->setQty($minimumQty);
        }
    }

    if ($productId) {
        try {
            $result = $this->getQuote()->addProduct($product, $request);
        } catch (Mage_Core_Exception $e) {
            $this->getCheckoutSession()->setUseNotice(false);
            $result = $e->getMessage();
        }
        /**
         * String we can get if prepare process has error
         */
        if (is_string($result)) {
            $redirectUrl = ($product->hasOptionsValidationFail())
                ? $product->getUrlModel()->getUrl(
                    $product,
                    array('_query' => array('startcustomization' => 1))
                )
                : $product->getProductUrl();
            $this->getCheckoutSession()->setRedirectUrl($redirectUrl);
            if ($this->getCheckoutSession()->getUseNotice() === null) {
                $this->getCheckoutSession()->setUseNotice(true);
            }
            Mage::throwException($result);
        }
    } else {
        Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
    }

    Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
    $this->getCheckoutSession()->setLastAddedProductId($productId);
    return $this;
}

But when I try to echo anything in this function, it won't appear and redirected to the configurable product page with this message:

Please specify the product's option(s).

UPDATE 1:

$_helper = $this->helper('catalog/product');
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
<?php if (count($_attributes) > 0):?>
    <dl>
    <?php foreach($_attributes as $_attribute):?>
    <div class="row">
        <h2 class="gws_p_title" style="border-top:  none;"><?php echo $this->__($_attribute->getLabel()) ?><em>*</em></h2>
        <dd class="last">
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                  </select>
              </div>
        </dd>
        </div>
    <?php endforeach; ?>
    </dl>

Solution

  • This was issue for me and take a lot of time from me since i'm new to magento.

    adding this line in the front of the file

    app/design/frontend/yourtheme/default/template/catalog/product/view/type/options/cofigurable.phtml

    $attValConfig = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
    

    so after search and check every code i have, i found the error in these lines.

    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
        <option><?php echo $this->__('Choose an Option...') ?></option>
    </select>
    

    IT SHOULD BE LIKE THIS

    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
    <option><?php echo $this->__('Choose an Option...') ?></option>
    <?php
    foreach ($attValConfig as $values) {
        foreach ($values['values'] as $value) {
           ?>
           <option value="<?php echo $value['value_index']; ?>"><?php echo $this->__($value['label']); ?></option>
           <?php
    
        }
    }
    ?>
    </select>
    

    AND NOW EVERYTHING WORK VERY FINE