Created
October 22, 2014 12:21
-
-
Save mklooss/1956811110039e5dd45d to your computer and use it in GitHub Desktop.
AutoUpselling Based on Orders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config> | |
<global> | |
<events> | |
<sales_order_place_after> | |
<observers> | |
<custom> | |
<type>singleton</type> | |
<class>custom/autoupsell_observer</class> | |
<method>autoUpsell</method> | |
</custom> | |
</observers> | |
</sales_order_place_after> | |
</events> | |
</global> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Custom_Modul_Model_Autoupsell_Observer | |
{ | |
/** | |
* @mageEvent sales_order_place_after | |
*/ | |
public function autoUpsell($event) | |
{ | |
$upsell = Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL; | |
$order = $event->getOrder(); | |
$ids = array(); | |
/* @var $order Mage_Sales_Model_Order */ | |
foreach($order->getAllItems() as $_product) | |
{ | |
/* @var $_product Mage_Sales_Model_Order_Item */ | |
$_id = $_product->getProductId(); | |
$ids[] = $_id; | |
} | |
if(count($ids) <= 1) | |
{ | |
return $this; | |
} | |
// | |
foreach($ids as $_id) | |
{ | |
foreach($ids as $_linked_id) | |
{ | |
if($_id != $_linked_id) | |
{ | |
$this->_connection()->insertOnDuplicate($this->getTableName('catalog/product_link'), array( | |
'product_id' => $_id, | |
'linked_product_id' => $_linked_id, | |
'link_type_id' => $upsell, | |
)); | |
} | |
} | |
} | |
return $this; | |
} | |
/** | |
* | |
* @return Mage_Core_Model_Resource | |
*/ | |
protected function _resoruce() | |
{ | |
return Mage::getSingleton('core/resource'); | |
} | |
/** | |
* | |
* @return Varien_Db_Adapter_Interface | |
*/ | |
protected function _connection($type = 'core_write') | |
{ | |
return $this->_resoruce() | |
->getConnection($type); | |
} | |
/** | |
* | |
* @param string $modelEntity | |
* @return string | |
*/ | |
protected function getTableName($modelEntity) | |
{ | |
return $this->_resoruce()->getTableName($modelEntity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment