Version Notes
Initial release
Download this release
Release Info
Developer | Manesh Sonah |
Extension | maurisou-instock |
Version | 1.0.0.0 |
Comparing to | |
See all releases |
Version 1.0.0.0
app/code/local/Maurisource/InStock/Model/Observer.php
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
class Maurisource_InStock_Model_Observer {
|
3 |
+
|
4 |
+
protected $_allowSetOutOfStock = true;
|
5 |
+
|
6 |
+
public function catalog_product_save_after($observer) {
|
7 |
+
|
8 |
+
$product = $observer->getProduct();
|
9 |
+
$simpleProductId = $product->getEntityId();
|
10 |
+
$simpleProductQty = $product->getStockData('qty');
|
11 |
+
|
12 |
+
if($product->getTypeId() == 'simple'){
|
13 |
+
|
14 |
+
//check if simple product has a parent
|
15 |
+
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($simpleProductId);
|
16 |
+
|
17 |
+
if(is_array($parentIds) && count($parentIds)> 0){
|
18 |
+
// simple product has parent(s)
|
19 |
+
foreach($parentIds as $parentId){
|
20 |
+
|
21 |
+
// now we check all the associated products
|
22 |
+
$this->_checkStockToOutOfStock($parentId);
|
23 |
+
|
24 |
+
if($this->_allowSetOutOfStock === true){
|
25 |
+
|
26 |
+
$this->_updateStockDataOutStock($parentId);
|
27 |
+
|
28 |
+
}else{
|
29 |
+
|
30 |
+
$this->_updateStockDataInStock($parentId);
|
31 |
+
|
32 |
+
}
|
33 |
+
|
34 |
+
}
|
35 |
+
|
36 |
+
}
|
37 |
+
|
38 |
+
if($simpleProductQty > 0){
|
39 |
+
|
40 |
+
$this->_updateSimpleProductStock($simpleProductId);
|
41 |
+
|
42 |
+
}
|
43 |
+
|
44 |
+
}elseif($product->getTypeId() == 'configurable' && $simpleProductQty > 0){
|
45 |
+
|
46 |
+
$this->_updateSimpleProductStock($simpleProductId);
|
47 |
+
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
private function _updateSimpleProductStock($simpleProductId){
|
54 |
+
|
55 |
+
$this->_updateStockDataInStock($simpleProductId);
|
56 |
+
|
57 |
+
}
|
58 |
+
|
59 |
+
private function _updateStockDataInStock($product_id){
|
60 |
+
|
61 |
+
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id); // Load the stock for this product
|
62 |
+
|
63 |
+
$stock->setData('is_in_stock', 1); // Set the Product to InStock
|
64 |
+
|
65 |
+
$stock->save(); // Save
|
66 |
+
}
|
67 |
+
|
68 |
+
private function _updateStockDataOutStock($product_id){
|
69 |
+
|
70 |
+
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id); // Load the stock for this product
|
71 |
+
|
72 |
+
$stock->setData('is_in_stock', 0); // Set the Product to InStock
|
73 |
+
|
74 |
+
$stock->save(); // Save
|
75 |
+
}
|
76 |
+
|
77 |
+
private function _checkStockToOutOfStock($configProductId){
|
78 |
+
|
79 |
+
// get child products of config. product
|
80 |
+
$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($configProductId);
|
81 |
+
|
82 |
+
foreach($childProducts[0] as $childProduct){
|
83 |
+
|
84 |
+
$product = Mage::getModel('catalog/product')->load($childProduct);
|
85 |
+
|
86 |
+
$stockQty = $product->getStockItem()->getQty();
|
87 |
+
|
88 |
+
if($stockQty > 0){
|
89 |
+
|
90 |
+
$this->_allowSetOutOfStock = false;
|
91 |
+
|
92 |
+
return;
|
93 |
+
|
94 |
+
}
|
95 |
+
|
96 |
+
}
|
97 |
+
}
|
98 |
+
|
99 |
+
}
|
100 |
+
?>
|
app/code/local/Maurisource/InStock/etc/config.xml
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<config>
|
3 |
+
<modules>
|
4 |
+
<Maurisource_InStock>
|
5 |
+
<version>1.0.0</version>
|
6 |
+
</Maurisource_InStock>
|
7 |
+
</modules>
|
8 |
+
<global>
|
9 |
+
<models>
|
10 |
+
<maurisource_instock>
|
11 |
+
<class>Maurisource_InStock_Model</class>
|
12 |
+
</maurisource_instock>
|
13 |
+
</models>
|
14 |
+
</global>
|
15 |
+
<adminhtml>
|
16 |
+
<events>
|
17 |
+
<catalog_product_save_after><!-- observe the event -->
|
18 |
+
<observers>
|
19 |
+
<maurisource_instock>
|
20 |
+
<class>maurisource_instock/observer</class>
|
21 |
+
<method>catalog_product_save_after</method>
|
22 |
+
</maurisource_instock>
|
23 |
+
</observers>
|
24 |
+
</catalog_product_save_after>
|
25 |
+
</events>
|
26 |
+
</adminhtml>
|
27 |
+
</config>
|
app/etc/modules/Maurisource_InStock.xml
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<config>
|
3 |
+
<modules>
|
4 |
+
<Maurisource_InStock>
|
5 |
+
<codePool>local</codePool>
|
6 |
+
<active>true</active>
|
7 |
+
<depends>
|
8 |
+
<Mage_Catalog />
|
9 |
+
</depends>
|
10 |
+
</Maurisource_InStock>
|
11 |
+
</modules>
|
12 |
+
</config>
|
package.xml
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<package>
|
3 |
+
<name>maurisou-instock</name>
|
4 |
+
<version>1.0.0.0</version>
|
5 |
+
<stability>stable</stability>
|
6 |
+
<license>ASL</license>
|
7 |
+
<channel>community</channel>
|
8 |
+
<extends/>
|
9 |
+
<summary>Put stock levels status back to in stock when quantity is greater than 0.</summary>
|
10 |
+
<description>Put stock levels status back to in stock when quantity is greater than 0.</description>
|
11 |
+
<notes>Initial release</notes>
|
12 |
+
<authors><author><name>Manesh Sonah</name><user>maurisource</user><email>info@maurisource.com</email></author></authors>
|
13 |
+
<date>2016-02-15</date>
|
14 |
+
<time>19:39:36</time>
|
15 |
+
<contents><target name="magelocal"><dir name="Maurisource"><dir name="InStock"><dir><dir name="etc"><file name="config.xml" hash="444218d145bb20c3ddb7ad67472d4198"/></dir><dir name="Model"><file name="Observer.php" hash="894bf582fbfae69a2921797b118121ff"/></dir></dir></dir></dir><dir name="Model"><file name="Observer.php" hash=""/></dir><dir name="etc"><file name="config.xml" hash=""/></dir></target><target name="mageetc"><dir name="modules"><file name="Maurisource_InStock.xml" hash="2b7a8fa6735c74fee518371e185fdd94"/></dir></target></contents>
|
16 |
+
<compatible/>
|
17 |
+
<dependencies><required><php><min>5.0.0</min><max>6.0.0</max></php></required></dependencies>
|
18 |
+
</package>
|