在Magento中实现事件观察器

jkp*_*phl 2 events observers magento

我开始明白Magento Event Observers是如何工作的,并希望有人可以帮我弄清楚我需要寻找什么样的事件才能让我的潜在模块发挥作用.我有某些产品具有名为"child skus"的属性,该属性是我库存中其他产品的SKU编号数组.

例:

名称:完全令人敬畏的产品

SKU:TP-1212

儿童SKUS:GP-3232,FD-4332,VC-5332

每当有人购买TP-1212时,我也需要Magento从幕后的那些儿童skus中扣除该数量.有没有人知道是否有一个事件调度员在购买完成后会处理类似的事情?

Dre*_*ter 13

这有点棘手,下面的代码中很可能没有涉及到一些边缘情况 - 答案假设如下:

  1. 您希望降低刚刚购买的父母的所有相关产品的库存水平
  2. 你希望减少1
  3. 没有必要满足/处理的其他并发症或其他条件

码:

所以,你显然需要先创建一个模块.config.xml需要声明将侦听的观察者checkout_type_onepage_save_order_after.(注意:您可以收听其他事件以实现目标).

config.xml至少包含以下代码:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCmpany_YourModule>
            <version>1.0.0</version>
        </YourCmpany_YourModule>
    </modules>
    <frontend>
        <events>
            <checkout_type_onepage_save_order_after>
                <observers>
                    <yourmodule_save_order_observer>
                        <class>YourCompany_YourModule_Model_Observer</class>
                        <method>checkout_type_onepage_save_order_after</method>
                    </yourmodule_save_order_observer>
                </observers>
            </checkout_type_onepage_save_order_after>
        </events>
    </frontend>
    <global>
        <models>
            <yourmodule>
                <class>YourCompany_YourModule_Model</class>
            </yourmodule>
        </models>
    </global>
</config>
Run Code Online (Sandbox Code Playgroud)

然后在你的观察者中:

<?php

class YourCompany_YourModule_Model_Observer
{
    public function checkout_type_onepage_save_order_after(Varien_Event_Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        /**
         * Grab all product ids from the order
         */
            $productIds = array();
        foreach ($order->getItemsCollection() as $item) {
            $productIds[] = $item->getProductId();
        }

        foreach ($productIds as $productId) {

            $product = Mage::getModel('catalog/product')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($productId);

            if (! $product->isConfigurable()) {
                continue;
            }

            /**
             * Grab all of the associated simple products
             */
            $childProducts = Mage::getModel('catalog/product_type_configurable')
                        ->getUsedProducts(null, $product);

            foreach($childProducts as $childProduct) {

                /**
                 * in_array check below, makes sure to exclude the simple product actually 
                 * being sold as we dont want its stock level decreased twice :)
                 */
                if (! in_array($childProduct->getId(), $productIds)) {

                    /**
                     * Finally, load up the stock item and decrease its qty by 1
                     */
                    $stockItem = Mage::getModel('cataloginventory/stock_item')
                                    ->loadByProduct($childProduct)
                                    ->subtractQty(1)
                                    ->save()
                                ;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)