Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_CatalogInventory_Block_Adminhtml_Form_Field_Customergroup
  • Mage_CatalogInventory_Block_Adminhtml_Form_Field_Minsaleqty
  • Mage_CatalogInventory_Block_Qtyincrements
  • Mage_CatalogInventory_Block_Stockqty_Abstract
  • Mage_CatalogInventory_Block_Stockqty_Composite
  • Mage_CatalogInventory_Block_Stockqty_Default
  • Mage_CatalogInventory_Block_Stockqty_Type_Configurable
  • Mage_CatalogInventory_Block_Stockqty_Type_Grouped
  • Mage_CatalogInventory_Helper_Data
  • Mage_CatalogInventory_Helper_Minsaleqty
  • Mage_CatalogInventory_Model_Api2_Stock_Item
  • Mage_CatalogInventory_Model_Api2_Stock_Item_Rest
  • Mage_CatalogInventory_Model_Api2_Stock_Item_Rest_Admin_V1
  • Mage_CatalogInventory_Model_Api2_Stock_Item_Validator_Item
  • Mage_CatalogInventory_Model_Indexer_Stock
  • Mage_CatalogInventory_Model_Mysql4_Indexer_Stock
  • Mage_CatalogInventory_Model_Mysql4_Indexer_Stock_Configurable
  • Mage_CatalogInventory_Model_Mysql4_Indexer_Stock_Default
  • Mage_CatalogInventory_Model_Mysql4_Indexer_Stock_Grouped
  • Mage_CatalogInventory_Model_Mysql4_Stock
  • Mage_CatalogInventory_Model_Mysql4_Stock_Item
  • Mage_CatalogInventory_Model_Mysql4_Stock_Item_Collection
  • Mage_CatalogInventory_Model_Mysql4_Stock_Status
  • Mage_CatalogInventory_Model_Observer
  • Mage_CatalogInventory_Model_Resource_Indexer_Stock
  • Mage_CatalogInventory_Model_Resource_Indexer_Stock_Configurable
  • Mage_CatalogInventory_Model_Resource_Indexer_Stock_Default
  • Mage_CatalogInventory_Model_Resource_Indexer_Stock_Grouped
  • Mage_CatalogInventory_Model_Resource_Stock
  • Mage_CatalogInventory_Model_Resource_Stock_Item
  • Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
  • Mage_CatalogInventory_Model_Resource_Stock_Status
  • Mage_CatalogInventory_Model_Source_Backorders
  • Mage_CatalogInventory_Model_Source_Stock
  • Mage_CatalogInventory_Model_Stock
  • Mage_CatalogInventory_Model_Stock_Item
  • Mage_CatalogInventory_Model_Stock_Item_Api
  • Mage_CatalogInventory_Model_Stock_Item_Api_V2
  • Mage_CatalogInventory_Model_Stock_Status
  • Mage_CatalogInventory_Model_System_Config_Backend_Minqty
  • Mage_CatalogInventory_Model_System_Config_Backend_Minsaleqty
  • Mage_CatalogInventory_Model_System_Config_Backend_Qtyincrements

Interfaces

  • Mage_CatalogInventory_Model_Mysql4_Indexer_Stock_Interface
  • Mage_CatalogInventory_Model_Resource_Indexer_Stock_Interface
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_CatalogInventory
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * CatalogInventory Stock Status per website Model
 29:  *
 30:  * @method Mage_CatalogInventory_Model_Resource_Stock_Status _getResource()
 31:  * @method Mage_CatalogInventory_Model_Resource_Stock_Status getResource()
 32:  * @method int getProductId()
 33:  * @method Mage_CatalogInventory_Model_Stock_Status setProductId(int $value)
 34:  * @method int getWebsiteId()
 35:  * @method Mage_CatalogInventory_Model_Stock_Status setWebsiteId(int $value)
 36:  * @method int getStockId()
 37:  * @method Mage_CatalogInventory_Model_Stock_Status setStockId(int $value)
 38:  * @method float getQty()
 39:  * @method Mage_CatalogInventory_Model_Stock_Status setQty(float $value)
 40:  * @method int getStockStatus()
 41:  * @method Mage_CatalogInventory_Model_Stock_Status setStockStatus(int $value)
 42:  *
 43:  * @category    Mage
 44:  * @package     Mage_CatalogInventory
 45:  * @author      Magento Core Team <core@magentocommerce.com>
 46:  */
 47: class Mage_CatalogInventory_Model_Stock_Status extends Mage_Core_Model_Abstract
 48: {
 49:     const STATUS_OUT_OF_STOCK       = 0;
 50:     const STATUS_IN_STOCK           = 1;
 51: 
 52:     /**
 53:      * Product Type Instances cache
 54:      *
 55:      * @var array
 56:      */
 57:     protected $_productTypes;
 58: 
 59:     /**
 60:      * Websites cache
 61:      *
 62:      * @var array
 63:      */
 64:     protected $_websites;
 65: 
 66:     /**
 67:      * Init resource model
 68:      *
 69:      */
 70:     protected function _construct()
 71:     {
 72:         $this->_init('cataloginventory/stock_status');
 73:     }
 74: 
 75:     /**
 76:      * Retrieve Product Type Instances
 77:      * as key - type code, value - instance model
 78:      *
 79:      * @return array
 80:      */
 81:     public function getProductTypeInstances()
 82:     {
 83:         if (is_null($this->_productTypes)) {
 84:             $this->_productTypes = array();
 85:             $productEmulator     = new Varien_Object();
 86: 
 87:             foreach (array_keys(Mage_Catalog_Model_Product_Type::getTypes()) as $typeId) {
 88:                 $productEmulator->setTypeId($typeId);
 89:                 $this->_productTypes[$typeId] = Mage::getSingleton('catalog/product_type')
 90:                     ->factory($productEmulator);
 91:             }
 92:         }
 93:         return $this->_productTypes;
 94:     }
 95: 
 96:     /**
 97:      * Retrieve Product Type Instance By Product Type
 98:      *
 99:      * @param string $productType
100:      * @return Mage_Catalog_Model_Product_Type_Abstract
101:      */
102:     public function getProductTypeInstance($productType)
103:     {
104:         $types = $this->getProductTypeInstances();
105:         if (isset($types[$productType])) {
106:             return $types[$productType];
107:         }
108:         return false;
109:     }
110: 
111:     /**
112:      * Retrieve website models
113:      *
114:      * @return array
115:      */
116:     public function getWebsites($websiteId = null)
117:     {
118:         if (is_null($this->_websites)) {
119:             $this->_websites = $this->getResource()->getWebsiteStores();
120:         }
121: 
122:         $websites = $this->_websites;
123:         if (!is_null($websiteId) && isset($this->_websites[$websiteId])) {
124:             $websites = array($websiteId => $this->_websites[$websiteId]);
125:         }
126: 
127:         return $websites;
128:     }
129: 
130:     /**
131:      * Retrieve Default website store Id
132:      *
133:      * @param int $websiteId
134:      * @return int
135:      */
136:     public function getWebsiteDefaultStoreId($websiteId)
137:     {
138:         $websites = $this->getWebsites();
139:         if (isset($websites[$websiteId])) {
140:             return $websites[$websiteId];
141:         }
142:         return 0;
143:     }
144: 
145:     /**
146:      * Retrieve Catalog Product Status Model
147:      *
148:      * @return Mage_Catalog_Model_Product_Status
149:      */
150:     public function getProductStatusModel()
151:     {
152:         return Mage::getSingleton('catalog/product_status');
153:     }
154: 
155:     /**
156:      * Retrieve CatalogInventory empty Stock Item model
157:      *
158:      * @return Mage_CatalogInventory_Model_Stock_Item
159:      */
160:     public function getStockItemModel()
161:     {
162:         return Mage::getModel('cataloginventory/stock_item');
163:     }
164: 
165:     /**
166:      * Retrieve Product Status Enabled Constant
167:      *
168:      * @return int
169:      */
170:     public function getProductStatusEnabled()
171:     {
172:         return Mage_Catalog_Model_Product_Status::STATUS_ENABLED;
173:     }
174: 
175:     /**
176:      * Change Stock Item status process
177:      *
178:      * @param Mage_CatalogInventory_Model_Stock_Item $item
179:      * @return Mage_CatalogInventory_Model_Stock_Status
180:      */
181:     public function changeItemStatus(Mage_CatalogInventory_Model_Stock_Item $item)
182:     {
183:         $productId  = $item->getProductId();
184:         if (!$productType = $item->getProductTypeId()) {
185:             $productType    = $this->getProductType($productId);
186:         }
187: 
188:         $status     = (int)$item->getIsInStock();
189:         $qty        = (int)$item->getQty();
190: 
191:         $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId());
192:         $this->_processParents($productId, $item->getStockId());
193: 
194:         return $this;
195:     }
196: 
197:     /**
198:      * Assign Stock Status to Product
199:      *
200:      * @param Mage_Catalog_Model_Product $product
201:      * @param int $stockId
202:      * @param int $stockStatus
203:      * @return Mage_CatalogInventory_Model_Stock_Status
204:      */
205:     public function assignProduct(Mage_Catalog_Model_Product $product, $stockId = 1, $stockStatus = null)
206:     {
207:         if (is_null($stockStatus)) {
208:             $websiteId = $product->getStore()->getWebsiteId();
209:             $status = $this->getProductStatus($product->getId(), $websiteId, $stockId);
210:             $stockStatus = isset($status[$product->getId()]) ? $status[$product->getId()] : null;
211:         }
212: 
213:         $product->setIsSalable($stockStatus);
214: 
215:         return $this;
216:     }
217: 
218:     /**
219:      * Rebuild stock status for all products
220:      *
221:      * @param int $websiteId
222:      * @return Mage_CatalogInventory_Model_Stock_Status
223:      */
224:     public function rebuild($websiteId = null)
225:     {
226:         $lastProductId = 0;
227:         while (true) {
228:             $productCollection = $this->getResource()->getProductCollection($lastProductId);
229:             if (!$productCollection) {
230:                 break;
231:             }
232: 
233:             foreach ($productCollection as $productId => $productType) {
234:                 $lastProductId = $productId;
235:                 $this->updateStatus($productId, $productType, $websiteId);
236:             }
237:         }
238: 
239:         return $this;
240:     }
241: 
242:     /**
243:      * Update product status from stock item
244:      *
245:      * @param int $productId
246:      * @param string $productType
247:      * @param int $websiteId
248:      * @return Mage_CatalogInventory_Model_Stock_Status
249:      */
250:     public function updateStatus($productId, $productType = null, $websiteId = null)
251:     {
252:         if (is_null($productType)) {
253:             $productType = $this->getProductType($productId);
254:         }
255: 
256:         $item = $this->getStockItemModel()->loadByProduct($productId);
257: 
258:         $status  = self::STATUS_IN_STOCK;
259:         $qty     = 0;
260:         if ($item->getId()) {
261:             $status = $item->getIsInStock();
262:             $qty    = $item->getQty();
263:         }
264: 
265:         $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId(), $websiteId);
266:         $this->_processParents($productId, $item->getStockId(), $websiteId);
267: 
268:         return $this;
269:     }
270: 
271:     /**
272:      * Process children stock status
273:      *
274:      * @param int $productId
275:      * @param string $productType
276:      * @param float $qty
277:      * @param int $status
278:      * @param int $stockId
279:      * @param int $websiteId
280:      * @return Mage_CatalogInventory_Model_Stock_Status
281:      */
282:     protected function _processChildren($productId, $productType, $qty = 0, $status = self::STATUS_IN_STOCK, $stockId = 1, $websiteId = null)
283:     {
284:         if ($status == self::STATUS_OUT_OF_STOCK) {
285:             $this->saveProductStatus($productId, $status, $qty, $stockId, $websiteId);
286:             return $this;
287:         }
288: 
289:         $statuses   = array();
290:         $websites   = $this->getWebsites($websiteId);
291: 
292:         foreach (array_keys($websites) as $websiteId) {
293:             /* @var $website Mage_Core_Model_Website */
294:             $statuses[$websiteId] = $status;
295:         }
296: 
297:         if (!$typeInstance = $this->getProductTypeInstance($productType)) {
298:             return $this;
299:         }
300: 
301:         $requiredChildrenIds = $typeInstance->getChildrenIds($productId, true);
302:         if ($requiredChildrenIds) {
303:             $childrenIds = array();
304:             foreach ($requiredChildrenIds as $groupedChildrenIds) {
305:                 $childrenIds = array_merge($childrenIds, $groupedChildrenIds);
306:             }
307:             $childrenWebsites = Mage::getSingleton('catalog/product_website')
308:                 ->getWebsites($childrenIds);
309:             foreach ($websites as $websiteId => $storeId) {
310:                 $childrenStatus = $this->getProductStatusModel()
311:                     ->getProductStatus($childrenIds, $storeId);
312:                 $childrenStock  = $this->getProductStatus($childrenIds, $websiteId, $stockId);
313: 
314:                 $websiteStatus = $statuses[$websiteId];
315:                 foreach ($requiredChildrenIds as $groupedChildrenIds) {
316:                     $optionStatus = false;
317:                     foreach ($groupedChildrenIds as $childId) {
318:                         if (isset($childrenStatus[$childId])
319:                             and isset($childrenWebsites[$childId])
320:                             and in_array($websiteId, $childrenWebsites[$childId])
321:                             and $childrenStatus[$childId] == $this->getProductStatusEnabled()
322:                             and isset($childrenStock[$childId])
323:                             and $childrenStock[$childId] == self::STATUS_IN_STOCK
324:                         ) {
325:                             $optionStatus = true;
326:                         }
327:                     }
328:                     $websiteStatus = $websiteStatus && $optionStatus;
329:                 }
330:                 $statuses[$websiteId] = (int)$websiteStatus;
331:             }
332:         }
333: 
334:         foreach ($statuses as $websiteId => $websiteStatus) {
335:             $this->saveProductStatus($productId, $websiteStatus, $qty, $stockId, $websiteId);
336:         }
337: 
338:         return $this;
339:     }
340: 
341:     /**
342:      * Process Parents by child
343:      *
344:      * @param int $productId
345:      * @param int $stockId
346:      * @param int $websiteId
347:      * @return Mage_CatalogInventory_Model_Stock_Status
348:      */
349:     protected function _processParents($productId, $stockId = 1, $websiteId = null)
350:     {
351:         $parentIds = array();
352:         foreach ($this->getProductTypeInstances() as $typeInstance) {
353:             /* @var $typeInstance Mage_Catalog_Model_Product_Type_Abstract */
354:             $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productId));
355:         }
356: 
357:         if (!$parentIds) {
358:             return $this;
359:         }
360: 
361:         $productTypes = $this->getProductsType($parentIds);
362:         $item         = $this->getStockItemModel();
363: 
364:         foreach ($parentIds as $parentId) {
365:             $parentType = isset($productTypes[$parentId]) ? $productTypes[$parentId] : null;
366:             $item->setData(array('stock_id' => $stockId))
367:                 ->setOrigData()
368:                 ->loadByProduct($parentId);
369:             $status  = self::STATUS_IN_STOCK;
370:             $qty     = 0;
371:             if ($item->getId()) {
372:                 $status = $item->getIsInStock();
373:                 $qty    = $item->getQty();
374:             }
375: 
376:             $this->_processChildren($parentId, $parentType, $qty, $status, $item->getStockId(), $websiteId);
377:         }
378: 
379:         return $this;
380:     }
381: 
382:     /**
383:      * Save product status per website
384:      * if website is null, saved for all websites
385:      *
386:      * @param int $productId
387:      * @param int $status
388:      * @param float $qty
389:      * @param int $stockId
390:      * @param int|null $websiteId
391:      * @return Mage_CatalogInventory_Model_Stock_Status
392:      */
393:     public function saveProductStatus($productId, $status, $qty = 0, $stockId = 1, $websiteId = null)
394:     {
395:         $this->getResource()->saveProductStatus($this, $productId, $status, $qty, $stockId, $websiteId);
396:         return $this;
397:     }
398: 
399:     /**
400:      * Retrieve Product(s) status
401:      *
402:      * @param int|array $productIds
403:      * @param int $websiteId
404:      * @param int $stockId
405:      * @return array
406:      */
407:     public function getProductStatus($productIds, $websiteId, $stockId = 1)
408:     {
409:         return $this->getResource()->getProductStatus($productIds, $websiteId, $stockId);
410:     }
411: 
412:     /**
413:      * Retrieve Product(s) Data array
414:      *
415:      * @param int|array $productIds
416:      * @param int $websiteId
417:      * @param int $stockId
418:      * @return array
419:      */
420:     public function getProductData($productIds, $websiteId, $stockId = 1)
421:     {
422:         return $this->getResource()->getProductData($productIds, $websiteId, $stockId);
423:     }
424: 
425:     /**
426:      * Retrieve Product Type
427:      *
428:      * @param int $productId
429:      * @return string|false
430:      */
431:     public function getProductType($productId)
432:     {
433:         $types = $this->getResource()->getProductsType($productId);
434:         if (isset($types[$productId])) {
435:             return $types[$productId];
436:         }
437:         return false;
438:     }
439: 
440:     /**
441:      * Retrieve Products Type as array
442:      * Return array as key product_id, value type
443:      *
444:      * @param array|int $productIds
445:      * @return array
446:      */
447:     public function getProductsType($productIds)
448:     {
449:         return $this->getResource()->getProductsType($productIds);
450:     }
451: 
452:     /**
453:      * Add information about stock status to product collection
454:      *
455:      * @param   Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $productCollection
456:      * @param   int|null $websiteId
457:      * @param   int|null $stockId
458:      * @return  Mage_CatalogInventory_Model_Stock_Status
459:      */
460:     public function addStockStatusToProducts($productCollection, $websiteId = null, $stockId = null)
461:     {
462:         if ($stockId === null) {
463:             $stockId = Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID;
464:         }
465:         if ($websiteId === null) {
466:             $websiteId = Mage::app()->getStore()->getWebsiteId();
467:             if ((int)$websiteId == 0 && $productCollection->getStoreId()) {
468:                 $websiteId = Mage::app()->getStore($productCollection->getStoreId())->getWebsiteId();
469:             }
470:         }
471:         $productIds = array();
472:         foreach ($productCollection as $product) {
473:             $productIds[] = $product->getId();
474:         }
475: 
476:         if (!empty($productIds)) {
477:             $stockStatuses = $this->_getResource()->getProductStatus($productIds, $websiteId, $stockId);
478:             foreach ($stockStatuses as $productId => $status) {
479:                 if ($product = $productCollection->getItemById($productId)) {
480:                     $product->setIsSalable($status);
481:                 }
482:             }
483:         }
484: 
485:         /* back compatible stock item */
486:         foreach ($productCollection as $product) {
487:             $object = new Varien_Object(array('is_in_stock' => $product->getData('is_salable')));
488:             $product->setStockItem($object);
489:         }
490: 
491:         return $this;
492:     }
493: 
494:     /**
495:      * Add stock status to prepare index select
496:      *
497:      * @param Varien_Db_Select $select
498:      * @param Mage_Core_Model_Website $website
499:      * @return Mage_CatalogInventory_Model_Stock_Status
500:      */
501:     public function addStockStatusToSelect(Varien_Db_Select $select, Mage_Core_Model_Website $website)
502:     {
503:         $this->_getResource()->addStockStatusToSelect($select, $website);
504:         return $this;
505:     }
506: 
507:     /**
508:      * Add stock status limitation to catalog product price index select object
509:      *
510:      * @param Varien_Db_Select $select
511:      * @param string|Zend_Db_Expr $entityField
512:      * @param string|Zend_Db_Expr $websiteField
513:      * @return Mage_CatalogInventory_Model_Stock_Status
514:      */
515:     public function prepareCatalogProductIndexSelect(Varien_Db_Select $select, $entityField, $websiteField)
516:     {
517:         if (Mage::helper('cataloginventory')->isShowOutOfStock()) {
518:             return $this;
519:         }
520: 
521:         $this->_getResource()->prepareCatalogProductIndexSelect($select, $entityField, $websiteField);
522: 
523:         return $this;
524:     }
525: 
526:     /**
527:      * Add only is in stock products filter to product collection
528:      *
529:      * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
530:      * @return Mage_CatalogInventory_Model_Stock_Status
531:      */
532:     public function addIsInStockFilterToCollection($collection)
533:     {
534:         $this->_getResource()->addIsInStockFilterToCollection($collection);
535:         return $this;
536:     }
537: }
538: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0