我有一个模块可以观察状态并将值写入数据库。 当产品简单时,它可以工作。但是当它是可配置的时,它会写出Pattern Product的值而不是Child的值。
if (($status !== $previousStatus) && ($status == Mage_Sales_Model_Order::STATE_PROCESSING)) {
$order = $observer->getEvent()->getOrder();
$items = $order->getAllVisibleItems();
mysql_connect('localhost', 'user', 'pass');
foreach($items as $item){
$cd = $item->getProduct()->getData('cd');
mysql_query("INSERT INTO `bd`.`table` (`id`, `cd`) VALUES (NULL, '$cd')");
}
return true;
}
如何识别它是否是可配置产品,并从子产品中获取数据?
答案 0 :(得分:1)
只是一个FYI但if (($status !== $previousStatus) && ($status == Mage_Sales_Model_Order::STATE_PROCESSING)) {
/** @var Mage_Sales_Model_Order $order */
$order = $observer->getData('order');
$items = $order->getAllVisibleItems();
/** @var Mage_Sales_Model_Order_Item $item */
foreach($items as $item){
$cd = $item->getProduct()->getData('cd');
//If configurable get the first child item and the cd value from it
if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$childrenItems = $item->getChildrenItems();
$childItem = reset($childrenItems);
if ($childItem instanceof Mage_Sales_Model_Order_Item) {
$cd = $childItem->getProduct()->getData('cd');
}
}
/** @var Mage_Core_Model_Resource $resource */
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$query = "INSERT INTO `table` (`cd`) VALUES ('{$cd}')";
$writeConnection->query($query);
}
return true;
}
返回配置,如果你想在一个集合中看到配置及其简单,你可以使用getAllItems
{{1}}
答案 1 :(得分:0)
忽略您的SQL连接 - 您应该修改它 - 看看这里:http://fishpig.co.uk/magento/tutorials/direct-sql-queries/
要获得可配置产品的所选子产品,请执行此操作;
foreach($items as $item){
// Establish product type and if configurable, load the chosen product
if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$simple = reset($item->getChildren());
$_product = $simple->getProduct();
} else {
$_product = $item->getProduct();
}
$cd = $_product->getData('cd');
mysql_query("INSERT INTO `bd`.`table` (`id`, `cd`) VALUES (NULL, '$cd')");
}