Magento - 如何在购买特定产品后触发事件(更改用户组)


Magento - How to trigger event (change user group) after purchasing a specific product

我的问题是如何在成功下单(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();
    }
}

我需要为此创建扩展名还是需要编辑核心文件?我应该在哪里创建观察器?

是的。您需要为此创建扩展。没什么大不了的。因此,您希望在订单成功后为客户更改用户组。右?。为此,您需要观察结帐过程并将代码放入其中。你是对的,checkout_onepage_controller_success_action是正确的观察者。

给你。。

app/code/local/Packagename/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是你的新方法。这里只有我们将为用户组更改内容的代码。所以

app/code/local/Packagename/Modulename/Model/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_idorder-id可用。因此,我们可以从会话中获取这些内容。就是这样。

如果您有任何疑问,请在此处发表评论。

编辑核心文件从来都不是一个好的做法。如果要更改默认Magento的功能,则应覆盖特定文件。这是一个关于覆盖的好教程

更好的方法是创建扩展并观察特定事件。在您的情况下,观察sales_order_place_after事件并检查订单是否包含 ID 为 11 的产品?如果是,则更改客户组。

有关观察者的更多信息,请查看此内容。

希望对您有所帮助。

无需为其创建扩展。

只需转到您的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();
    }
}