magento 购物车礼品赠品插件,允许用户添加免费礼物到购物车
礼品
一:配置我们的config.xml文件:
<?xml version="1.0"?>
<config>
<modules>
<More_Gifts>
<version>0.1.5</version>
</More_Gifts>
</modules>
<frontend><!--前台-->
<layout>
<updates>
<gifts>
<file>gifts.xml</file>
</gifts>
</updates>
</layout>
<events>
<checkout_cart_update_items_after>
<observers>
<control_qty><!--监听中的数量控制方法-->
<class>gifts/observer</class>
<method>controlQty</method>
</control_qty>
</observers>
</checkout_cart_update_items_after>
<checkout_cart_product_add_after>
<observers>
<add_to_cart_after><!--添加购物车后触动的方法-->
<class>gifts/observer</class>
<method>make_gift</method>
</add_to_cart_after>
</observers>
</checkout_cart_product_add_after>
<controller_action_postdispatch>
<observers><!--控制器一经访问即触发-->
<controller_action_after>
<class>gifts/observer</class>
<method>hookToControllerActionPostDispatch</method>
</controller_action_after>
</observers>
</controller_action_postdispatch>
</events>
</frontend>
<global>
<models><!--数据库配置-->
<gifts>
<class>More_Gifts_Model</class>
<resourceModel>gifts_mysql4</resourceModel>
</gifts>
<gifts_mysql4>
<class>More_Gifts_Model_Mysql4</class>
<entities>
<gifts>
<table>gifts</table>
</gifts>
</entities>
</gifts_mysql4>
</models>
<resources><!-entity setup安装 有个entity文件夹-->
<gifts_setup>
<setup>
<module>More_Gifts</module>
<class>More_Gifts_Entity_Setup</class>
</setup>
<connection><!--数据库 链接读取-->
<use>core_setup</use>
</connection>
</gifts_setup>
<gifts_write>
<connection>
<use>core_write</use>
</connection>
</gifts_write>
<gifts_read>
<connection>
<use>core_read</use>
</connection>
</gifts_read>
</resources>
<blocks>
<gifts>
<class>More_Gifts_Block</class>
</gifts>
</blocks>
<helpers>
<gifts>
<class>More_Gifts_Helper</class>
</gifts>
</helpers>
</global>
</config>
二:observer 监听方法:
<?php
class More_Gifts_Model_Observer
{
public function __construct()
{
}
public function giftincart()
{
$items = Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
foreach($items as $item){
if ($item->getOriginalCustomPrice()==0 && (Mage::getModel('catalog/product')->load($item['product_id'])->getGiftvalue()>0))
return(true);
}
return(false);
}
public function hookToControllerActionPostDispatch($observer)
{
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_delete' || $observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_updatePost' ){
$items = Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
foreach($items as $item){
if ($item->getOriginalCustomPrice()==0 && Mage::getModel('catalog/product')->load($item['product_id'])->getGiftvalue()> Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()){
Mage::getSingleton('checkout/cart')->removeItem($item->getId())->save();
}
}
}
}
public function controlQty($observer)
{
$items = Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
foreach($items as $item){
if ($item->getPrice()==0 && Mage::getModel('catalog/product')->load($item['product_id'])->getGiftvalue()>Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()){
Mage::getSingleton('checkout/cart')->removeItem($item->getId())->save();
}
}
$event = $observer->getEvent();
$product = $event->getProduct();
$cart = $event->getCart();
$data = $event->getInfo();
foreach ($data as $itemId => $itemInfo) {
$item = $cart->getQuote()->getItemById($itemId);
if (!$item) {
continue;
}
if (Mage::getModel('catalog/product')->load($item['product_id'])->getGiftvalue()){
if ($item->getOriginalCustomPrice()!=null){
if ($item->getOriginalCustomPrice()==0.00){
$item->setQty(1);
$item->save();
}
}
}
}
}
public function make_gift($observer)
{
$items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$event = $observer->getEvent();
$product = $event->getProduct();
$quote_item = $event->getQuoteItem();
foreach($items as $item){
if (Mage::getModel('catalog/product')->load($item['product_id'])->getGiftvalue() && ($item->getOriginalCustomPrice())){
if ($item->getOriginalCustomPrice()==0.00){
if ($item['sku']==$quote_item['sku']){
$item->setQty(1);
$item->save();
}
}
}
}
if (Mage::app()->getRequest()->getParam('g')){
if (Mage::getModel('catalog/product')->load($quote_item['product_id'])->getGiftvalue()){
if (Mage::getModel('catalog/product')->load($quote_item['product_id'])->getGiftvalue()<Mage::getModel('checkout/session')->getQuote()->getGrandTotal()){
$quote_item->setOriginalCustomPrice(0.00);
$quote_item->calcRowTotal();
$quote_item->save();
}
}
}
return $this;
}
}
(责任编辑:最模板) |