Magento - How do I use a shopping cart price rule to display a cms block?
I would like to use a shopping cart price rule to display a cross sell message rather than setting a discount.
For instance, if a certain item is in the cart, display the promotional message in the cross sell area.
Specifically, we have jacket and pants that can be purchased separately. But when purchased together, the customer can save $开发者_开发知识库50. So, if the jacket is in the cart, I would like a message that says "buy the pants and save $50" and vice versa. There is already a rule in place to give the discount when both are in the cart, so I would like to call their attention to the discount.
Create an observer for controller_action_layout_generate_blocks_after, the method should look something like this:
Note: this is the main idea, the difficult part is to parse the rule, probably it's much easier to add in the rule description something like: productSku: 123; productSku: 345 and to parse the rule description to see if one of the cart products matches the rule or not.
public function addPromotionBlock($observer)
{
$action = $observer->getEvent()->getAction();
$layout = $observer->getEvent()->getLayout();
if($action->getRequest()->getControllerName() == 'cart' && $action->getRequest()->getActionName() == 'index') {
$addPromotion = false;
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$items = Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
$rules = Mage::getResourceModel('salesrule/rule_collection')
->setValidationFilter(
Mage::app()->getStore($quote->getStoreId())->getWebsiteId(),
$quote->getCustomerGroupId(),
$quote->getCouponCode())
->load();
// see Mage_SalesRule_Model_Validator
foreach ($items as $item) {
//check if the rules applies
foreach ($rules as $rule) {
var_dump($rule->toArray());
}
//let's say we found a valid rule
$addPromotion = true;
}
if ($addPromotion) {
$crosssell = $layout->getBlock('checkout.cart.crosssell');
if ($crosssell) {
//set your template here
$crosssell->setTemplate('checkout/promotion.phtml');
}
}
}
}
精彩评论