Magento API Add Promo Item To Cart

Hello,

Recently I was working on a project where we were working with MAgento API. There was a requirement to show promotions to user. I have published a post on this. You can check it from here

Now we had another requirement to add items of promo to cart when user purchase an item. Here in this blog I will explain how to do this. Please note this logic will work for Buy X and get Y product free and Buy X and get Y Free logic.

So in API we were passing rule_id. First we will get rule information.

Mage::app()->setCurrentStore($_storeId);
$rule = Mage::getModel(‘salesrule/rule’)->load($_POST[‘rule_id’]);

Now we will get all the rules conditions.

$conditions = $rule->getConditions()
$conditions = $rule->getConditions()->asArray();

Now we will get all the product SKUs  and qty involved in condition.

$conditionSkus = array();
$conditionQty = array();

foreach( $conditions[‘conditions’] as $_conditions ){
foreach( $_conditions[‘conditions’] as $_condition ){
if($_condition[‘attribute’] == ‘sku’){
$string = explode(‘,’, $_condition[‘value’]);
for ($i=0; $i $conditionSkus[] = trim($string[$i]);
}
}else if($_condition[‘attribute’] == ‘quote_item_qty’){
$string = explode(‘,’, $_condition[‘value’]);
for ($i=0; $i $conditionQty[] = trim($string[$i]);
}
}
}
}

As you can see above we have conditions as array and we get sku and number of qty. Number of qty will be useful if you have promos like Buy 2 get 1 Free.

$actions = $rule->getActions();
$actions = $rule->getActions()->asArray();
$actionSkus = array();
$actionQty = null;
if(isset($actions[‘conditions’])){
foreach( $actions[‘conditions’] as $_actions ){
$string = explode(‘,’, $_actions[‘value’]);
for ($i=0; $i $actionSkus[] = trim($string[$i]);
}
}
}
else{
$actionQty = $rule->discount_step;
}

In above code as you can see we are checking if there are any skus specified as free products. If there are not skus then it’s buy x and get y offers so we are getting free qty from discount_step.

That’s it now we just have to loop through array and add item to cart.

$arrProducts= array();
foreach($conditionSkus as $sku){
//load product from sku and get id
$qty = 1;
if(count($conditionQty) != 0){
$qty= $conditionQty[0];
}else if(isset($actionQty)){
$qty = $actionQty;
}
$product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$sku);
$product = Mage::getModel(‘catalog/product’)->load($product->getId());
$arrProducts[] =  array(
“product_id” => $product->getId(),
“qty” => $qty
);
}

foreach($actionSkus as $sku){
//load product from sku and get id
$qty = 1;
$product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$sku);
$product = Mage::getModel(‘catalog/product’)->load($product->getId());
$arrProducts[] =  array(
“product_id” => $product->getId(),
“qty” => $qty
);
}

$resultCartProductAdd = $client->call(
$session,
“cart_product.add”,
array(
$quote_id,
$arrProducts
)
);

That’s it and now promot items will be added to cart.

This entry was posted in Magento, Magento API, Magento Mobile on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

Leave a Reply

Your email address will not be published. Required fields are marked *