我的问题是如何在订单成功后触发操作(ID为11)?
我从先前的问题中读到,我需要为checkout_onepage_controller_success_action
创建一个观察者,但之后解释的不多。
以下是我用来更改产品的代码(来自同一个上一个问题):
$special_cat = 11; // Special product category
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$categoryIds = $product->getCategoryIds();
if (in_array($special_cat, $categoryIds)) {
$mem_group_id = $item->getSku(); // $item->getSku() is customer group name
$customer_detail = Mage::getSingleton('customer/session')->getCustomer();
$customer_detail->setGroupId($mem_group_id);
$customer_detail->save();
}
}
我是否需要为此创建扩展程序,还是需要编辑核心文件?我应该在哪里创建观察者?
答案 0 :(得分:3)
是。您需要为此创建扩展。没什么大不了的。因此,您希望在订单成功后为客户更改用户组。对 ?。为此,您需要观察结帐流程并将代码放入其中。你是对的,checkout_onepage_controller_success_action
是正确的观察者。
你去..
应用程序/代码/本地/包/ MODULENAME的/ etc / config.xml中
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
<global>
<models>
<modulename>
<class>Packagename_ModuleName_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<events>
<checkout_onepage_controller_success_action> <!-- identifier of the event we want to catch -->
<observers>
<checkout_onepage_controller_success_action_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>modulename/observer</class> <!-- observers class alias -->
<method>changeUserGroup</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</checkout_onepage_controller_success_action_handler>
</observers>
</checkout_onepage_controller_success_action>
</events>
</global>
</config>
这里Packagename_ModuleName_Model
是你的新班级。而changeUserGroup
是你的新方法。这里只有我们将代码用于用户组更改。
所以,
应用程序/代码/本地/包/ MODULENAME /型号/ Observer.php
<?php
class Packagename_ModuleName_Model_Observer
{
public function changeUserGroup(Varien_Event_Observer $observer)
{
//$customer = $observer->getCustomer();
$special_cat = 11; // Special product category
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$categoryIds = $product->getCategoryIds();
if (in_array($special_cat, $categoryIds)) {
$mem_group_id = $item->getSku(); // $item->getSku() is customer group name
$customer_detail = Mage::getSingleton('customer/session')->getCustomer();
$customer_detail->setGroupId($mem_group_id);
$customer_detail->save();
}
}
}
}
最后启用你的模块,
应用程序的/ etc /模块/ Packagename_ModuleName.xml
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
</config>
通常我们会在观察到某些内容时从$observer
获取详细信息。但在您的情况下,会话中可以使用customer_id
和order-id
。所以我们可以从会话中获取这些东西。就是这样。
如果您有任何疑问,请在此处发表评论。
答案 1 :(得分:1)
编辑核心文件绝不是一个好习惯。如果要更改默认Magento的功能,则应覆盖特定文件。 Here is a good tutorial on overriding
更好的方法是创建扩展并观察特定事件。在您的情况下,请观察 sales_order_place_after 事件并检查订单是否包含ID为11的产品?如果是,则更改客户组。
有关观察者check this out.
的更多信息希望它有所帮助。
答案 2 :(得分:0)
无需为其创建扩展程序。
只需转到你的onepage_controller_action并添加以下代码。
$mem_catid = 982; //CATEGORY ID products to filter
$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$categoryIds = $product->getCategoryIds();
if (in_array($mem_catid, $categoryIds)) {
$mem_group_id = $item->getSku(); // $item->getSku() is customer group name
$customer_detail = Mage::getSingleton('customer/session')->getCustomer();
$customer_detail->setGroupId(6);//add customer group id here which you want to set.
$customer_detail->save();
}
}