2

I know that we can adding a product to cart via querystring.
but how do we add sku to cart via querystring in url without including product id like this :

domain/checkout/cart/add?sku=ABC
or
domain/checkout/cart/add/sku=BC

2

Andhi Irawan,you can not do this type of work at checkout Cart controllers. If want this type of work then you need to rewrite Checkout CartController.php and on this class magento is load the product object by id before going to cart,So you need load product by sku on this place .

Step 1: rewrite cartController.php and code is for xml rewrite

   <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <customcart before="Mage_Checkout">YourNameSpace_YourMOdule</customcart>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>  

Step2: and on your extended Cartcontorller _initProduct function you need product load product by sku

     protected function _initProduct()
  {
    $sku=$this->getRequest()->setParam('sku');
    $_catalog = Mage::getModel('catalog/product');
    $idBySku = $_catalog->getIdBySku($sku);

    if ($idBySku) { 
        $productId = $idBySku;
    $this->getRequest()->setParam('product', $productId );
    }else{
    $productId = (int) $this->getRequest()->getParam('product');
    }
    if ($productId) {
        $product = Mage::getModel('catalog/product')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->load($productId);
        if ($product->getId()) {
        return $product;
        }
    }

        return false;
    }

How to rewrite controller see more at : http://www.amitbera.com/how-to-override-a-controller-in-magento/

  • Amit Bera, thank you for your help. i had read your tutorial how to override controller. previous checkout module is apparently already override on the path code\local\ABC\Checkout\. so I do not change the file config.xml. I added a function _initProduct in code\local\ABC\Checkout\controllers\CartController.php.The following config.xml file contents:<br/><?xml version="1.0" ?><config><frontend><routers><checkout><args><modules><abc_checkout before="Mage_Checkout">ABC_Checkout</abc_checkout></modules></args></checkout></routers></frontend></config> – Andhi Irawan Nov 24 '14 at 8:03
  • my app\code\local\ABC\Checkout\controllers\CartController.php : pastebin.com/873rBFYm – Andhi Irawan Nov 24 '14 at 8:56
  • On magenot 1.9 magent is used form key which is validate your cart is proper or not if (!$this->_validateFormKey()) { $this->_goBack(); return; } – Amit Bera Nov 24 '14 at 9:04
  • hey,is it resolved or not – Amit Bera Nov 25 '14 at 7:51
  • hi Amit Bera, sorry I was sick yesterday. my Magento version 1.7. and yes, it resolved. thanks to you and the tutorial you made. i modify the config.xml, function _initProduct() in CartController.php and adding a helper file app\code\local\ABC\Checkout\Helper\Cart.php – Andhi Irawan Nov 27 '14 at 7:10
1

my function _initProduct() : pastebin.com/873rBFYm

protected function _initProduct()
{
    $sku = $this->getRequest()->getParam('sku'); 
    $productId = (int) $this->getRequest()->getParam('product');

    if($sku){
        $params = $this->getRequest()->getParams();
        $productId = Mage::helper('checkout/cart')->getProductIdByParams($params, false);
        if(! $productId){
            return false;
        }
    }

    if ($productId) {
        $product = Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($productId);
        if ($product->getId()) {
            return $product;
        }
    }
    return false;
}

my config.xml : pastebin.com/Ks85etyQ

<?xml version="1.0" ?><config>
<frontend>
    <routers>
        <checkout>
            <args>
                <modules>
                    <abc_checkout before="Mage_Checkout">ABC_Checkout</abc_checkout>
                </modules>
            </args>
        </checkout>
    </routers>
</frontend><global><helpers>
    <checkout>
        <rewrite>
            <cart>ABC_Checkout_Helper_Cart</cart>
        </rewrite>
    </checkout>
</helpers></global></config>

my helper : app\code\local\ABC\Checkout\Helper\Cart.php : pastebin.com/RZGSKJdr

<?php class ABC_Checkout_Helper_Cart extends Mage_Checkout_Helper_Cart{
public function getProductIdByParams($param, $outputFlag = true){
    $sku = trim($param['sku']);
    $_product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
                                                 ->loadByAttribute('sku', $sku);

    if($_product){
        $productTypeId = $_product->getTypeId();
        $productId = $_product->getId();
        if($productTypeId == 'simple'){
            $result = $outputFlag ? true : $productId;
        }
        elseif($productTypeId == 'configurable'){
            $color = trim($param['color']);
            $size = trim($param['size']);
            $config = trim($param['option']);

            $configurableProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
            $simpleCollections = $configurableProduct->getUsedProductCollection()
                                                     ->addAttributeToSelect('id');
            if(! empty($color)){
                $attrColor = $_product->getResource()->getAttribute("sp_color");
                $colorId = $attrColor->usesSource() ? $attrColor->getSource()->getOptionId($color) : '';
                $simpleCollections->addAttributeToFilter('sp_color', $colorId);
            }
            if(! empty($size)){
                $attrSize = $_product->getResource()->getAttribute("sp_size");
                $sizeId = $attrSize->usesSource() ? $attrSize->getSource()->getOptionId($size) : '';
                $simpleCollections->addAttributeToFilter('sp_size', $sizeId);
            }
            if(! empty($config)){
                $attrConfig = $_product->getResource()->getAttribute("sp_config");
                $configId = $attrConfig->usesSource() ? $attrConfig->getSource()->getOptionId($config) : '';
                $simpleCollections->addAttributeToFilter('sp_config', $configId);
            }
            $simpleCollections->addFilterByRequiredOptions();                                         

            if($simpleCollections->count() == 1){
                $productId = $simpleCollections->getFirstItem()->getId();
                $result = $outputFlag ? true : $productId;
            }
            else{
                $result = false;
            }

        }
        return $result;
    }
    return false;
}}

so sku can be adding via url querystring like this :

SIMPLE PRODUCT

Parameter:<br/>
~ sku<br/>
~ qty<br/>

a. Add product with qty = 1

domain/id/index.php/checkout/cart/add/sku/ABCD<br/>
or<br/>
domain/id/index.php/checkout/cart/add?sku=ABCD

b. Add product with qty more than 1

domain/id/index.php/checkout/cart/add/sku/ABCD/qty/3<br/>
or<br/>
domain/id/index.php/checkout/cart/add?sku=ABCD&qty=3<br/>

CONFIGURABLE PRODUCT<br/>
Parameter:<br/>
~ sku<br/>
~ color<br/>
~ size<br/>
~ option (ex: used by XYZ)<br/>
~ qty<br/>
~ __store (id or en)<br/>

a. Add product with qty = 1

domain/id/index.php/checkout/cart/add/sku/XYZ/color/merah tua/size/M/__store/id<br/>
domain/id/index.php/checkout/cart/add/sku/XYZ/color/maroon/size/M/__store/en<br/>
domain/id/index.php/checkout/cart/add/sku/XYZ/option/bilqis/__store/id<br/>
or<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&color=merah tua&size=M&__store=id<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&color=maroon&size=M&__store=en<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&option=bilqis&__store=id<br/>

b. Add product with qty more than 1

domain/id/index.php/checkout/cart/add/sku/XYZ/color/merah tua/size/M/qty/3/__store/id<br/>
domain/id/index.php/checkout/cart/add/sku/XYZ/color/maroon/size/M/qty/3/__store/en<br/>
domain/id/index.php/checkout/cart/add/sku/XYZ/option/bilqis/qty/3/__store/id<br/>
or<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&color=merah tua&size=M&qty=3&__store=id<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&color=maroon&size=M&qty=3&__store=en<br/>
domain/id/index.php/checkout/cart/add?sku=XYZ&option=bilqis&qty=3&__store=id

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.