Events 在Magento中实现事件观察程序
我开始了解Magento事件观察器是如何工作的,希望有人能帮我弄清楚我需要寻找什么样的事件才能让我的潜在模块工作。我的某些产品具有一个名为“子SKU”的属性,该属性是库存中其他产品的SKU编号数组 例如: 名称:非常棒的产品 库存单位:TP-1212 子SKU:GP-3232、FD-4332、VC-5332Events 在Magento中实现事件观察程序,events,magento,observers,Events,Magento,Observers,我开始了解Magento事件观察器是如何工作的,希望有人能帮我弄清楚我需要寻找什么样的事件才能让我的潜在模块工作。我的某些产品具有一个名为“子SKU”的属性,该属性是库存中其他产品的SKU编号数组 例如: 名称:非常棒的产品 库存单位:TP-1212 子SKU:GP-3232、FD-4332、VC-5332 每次有人购买TP-1212时,我也需要Magento从幕后的子SKU中扣除该数量。有人知道在购买完成后是否有一个事件调度器可以处理类似的事情吗???有几种最简单的方法,就是签出\u type
每次有人购买TP-1212时,我也需要Magento从幕后的子SKU中扣除该数量。有人知道在购买完成后是否有一个事件调度器可以处理类似的事情吗???有几种最简单的方法,就是
签出\u type\u onepage\u save\u order\u after
但是,如果订单取消,您是否也需要恢复库存,或者仅在付款或发货时执行此操作 这有点棘手,下面的代码中很可能没有涵盖一些边缘情况-答案假设如下:
代码: 因此,您显然需要首先创建一个模块。config.xml需要声明将在之后侦听
签出\u键入\u onepage\u保存\u订单\u的观察者。(注意:为了实现目标,您还可以收听其他活动)
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>
1.0.0
你的公司你的模块你的模型你的观察者
结帐\u键入\u一页\u保存\u订单\u之后
你的公司(你的模块)模式
然后在你的观察者中:
<?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()
;
}
}
}
}
}
不,如果订单被取消或有什么特殊情况,我就不必回复了。一旦有人提交订单,这将只是一个单向过程。谢谢真棒,谢谢!唯一的区别是,我必须根据母公司的销售数量扣除子产品的数量。我确实有一个工作脚本,每次我在一系列订单上运行它时都会这样做,但我想使用Observer使其更加无缝。结合我所拥有的和你所写的,我应该可以走了@Drew Hunter有没有办法减少订购物品的数量,假设我订购了2种不同数量的产品,我想使用相同的观察者从库存中减少相同的产品数量,有可能吗?