Adding a Product to the Cart via Querystring using SKUAdding a Product to the Cart via Querystring using SKU...
A Ri-diddley-iley Riddle
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
What can I do if I am asked to learn different programming languages very frequently?
Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?
If "dar" means "to give", what does "daros" mean?
What does "mu" mean as an interjection?
Help rendering a complicated sum/product formula
Practical application of matrices and determinants
Describing a chess game in a novel
Do I need to be arrogant to get ahead?
I got the following comment from a reputed math journal. What does it mean?
What is the term when voters “dishonestly” choose something that they do not want to choose?
Variable completely messes up echoed string
In what cases must I use 了 and in what cases not?
Hausdorff dimension of the boundary of fibres of Lipschitz maps
Do I need to consider instance restrictions when showing a language is in P?
Comment Box for Substitution Method of Integrals
How to terminate ping <dest> &
HP P840 HDD RAID 5 many strange drive failures
Optimising a list searching algorithm
World War I as a war of liberals against authoritarians?
PTIJ: Do Irish Jews have "the luck of the Irish"?
Relation between independence and correlation of uniform random variables
Loading the leaflet Map in Lightning Web Component
Adding a Product to the Cart via Querystring using SKU
Adding a Product to the Cart via Querystring using SKU in magento 1.9Adding to Cart with Querystring - Takes me to homepage?Adding product SKU into existing Cart Price rule Magento 1.7.0.2Unable to add product in cart with custom option via querystringMultiple Add to Cart Button from External php site using product idAdd product sku in URLHow can I get the product details (like SKU) on JS when “Add to Cart” button is clicked?Add a product to cart in checkout cart page?Why the product button aren't adding into the cart?Issue in adding the product to cart
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
addtocart query sku
add a comment |
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
addtocart query sku
add a comment |
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
addtocart query sku
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
addtocart query sku
addtocart query sku
asked Nov 24 '14 at 3:08
Andhi IrawanAndhi Irawan
3771720
3771720
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
add a comment |
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 : appcodelocalABCCheckoutHelperCart.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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f45132%2fadding-a-product-to-the-cart-via-querystring-using-sku%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
add a comment |
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
add a comment |
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/
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/
edited Nov 24 '14 at 5:22
answered Nov 24 '14 at 4:41
Amit Bera♦Amit Bera
59.2k1575176
59.2k1575176
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
add a comment |
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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
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 codelocalABCCheckout. so I do not change the file config.xml. I added a function _initProduct in codelocalABCCheckoutcontrollersCartController.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 appcodelocalABCCheckoutcontrollersCartController.php : pastebin.com/873rBFYm
– Andhi Irawan
Nov 24 '14 at 8:56
my appcodelocalABCCheckoutcontrollersCartController.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
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
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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
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 appcodelocalABCCheckoutHelperCart.php
– Andhi Irawan
Nov 27 '14 at 7:10
add a comment |
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 : appcodelocalABCCheckoutHelperCart.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
add a comment |
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 : appcodelocalABCCheckoutHelperCart.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
add a comment |
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 : appcodelocalABCCheckoutHelperCart.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
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 : appcodelocalABCCheckoutHelperCart.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
edited 3 mins ago
Teja Bhagavan Kollepara
3,00641949
3,00641949
answered Nov 27 '14 at 7:39
Andhi IrawanAndhi Irawan
3771720
3771720
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f45132%2fadding-a-product-to-the-cart-via-querystring-using-sku%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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