Mage_CatalogInventory_Model_Stock_Status Class Reference

Inheritance diagram for Mage_CatalogInventory_Model_Stock_Status:

Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 getProductTypeInstances ()
 getProductTypeInstance ($productType)
 getWebsites ($websiteId=null)
 getWebsiteDefaultStoreId ($websiteId)
 getProductStatusModel ()
 getStockItemModel ()
 getProductStatusEnabled ()
 changeItemStatus (Mage_CatalogInventory_Model_Stock_Item $item)
 assignProduct (Mage_Catalog_Model_Product $product, $stockId=1, $stockStatus=null)
 rebuild ($websiteId=null)
 updateStatus ($productId, $productType=null, $websiteId=null)
 saveProductStatus ($productId, $status, $qty=0, $stockId=1, $websiteId=null)
 getProductStatus ($productIds, $websiteId, $stockId=1)
 getProductData ($productIds, $websiteId, $stockId=1)
 getResource ()
 getProductType ($productId)
 getProductsType ($productIds)
 addStockStatusToProducts ($productCollection, $websiteId=null, $stockId=null)
 addStockStatusToSelect (Varien_Db_Select $select, Mage_Core_Model_Website $website)

Public Attributes

const STATUS_OUT_OF_STOCK = 0
const STATUS_IN_STOCK = 1

Protected Member Functions

 _construct ()
 _processChildren ($productId, $productType, $qty=0, $status=self::STATUS_IN_STOCK, $stockId=1, $websiteId=null)
 _processParents ($productId, $stockId=1, $websiteId=null)

Protected Attributes

 $_productTypes
 $_websites


Detailed Description

Definition at line 34 of file Status.php.


Member Function Documentation

_construct (  )  [protected]

Init resource model

Reimplemented from Varien_Object.

Definition at line 57 of file Status.php.

00058     {
00059         $this->_init('cataloginventory/stock_status');
00060     }

_processChildren ( productId,
productType,
qty = 0,
status = self::STATUS_IN_STOCK,
stockId = 1,
websiteId = null 
) [protected]

Process children stock status

Parameters:
int $productId
string $productType
float $qty
int $status
int $stockId
int $websiteId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 269 of file Status.php.

00270     {
00271         if ($status == self::STATUS_OUT_OF_STOCK) {
00272             $this->saveProductStatus($productId, $status, $qty, $stockId, $websiteId);
00273             return $this;
00274         }
00275 
00276         $statuses   = array();
00277         $websites   = $this->getWebsites($websiteId);
00278 
00279         foreach (array_keys($websites) as $websiteId) {
00280             /* @var $website Mage_Core_Model_Website */
00281             $statuses[$websiteId] = $status;
00282         }
00283 
00284         if (!$typeInstance = $this->getProductTypeInstance($productType)) {
00285             return $this;
00286         }
00287 
00288         $requiredChildrenIds = $typeInstance->getChildrenIds($productId, true);
00289         if ($requiredChildrenIds) {
00290             $childrenIds = array();
00291             foreach ($requiredChildrenIds as $groupedChildrenIds) {
00292                 $childrenIds = array_merge($childrenIds, $groupedChildrenIds);
00293             }
00294             $childrenWebsites = Mage::getSingleton('catalog/product_website')
00295                 ->getWebsites($childrenIds);
00296             foreach ($websites as $websiteId => $storeId) {
00297                 $childrenStatus = $this->getProductStatusModel()
00298                     ->getProductStatus($childrenIds, $storeId);
00299                 $childrenStock  = $this->getProductStatus($childrenIds, $websiteId, $stockId);
00300 
00301                 $websiteStatus = $statuses[$websiteId];
00302                 foreach ($requiredChildrenIds as $groupedChildrenIds) {
00303                     $optionStatus = false;
00304                     foreach ($groupedChildrenIds as $childId) {
00305                         if (isset($childrenStatus[$childId])
00306                             and isset($childrenWebsites[$childId])
00307                             and in_array($websiteId, $childrenWebsites[$childId])
00308                             and $childrenStatus[$childId] == $this->getProductStatusEnabled()
00309                             and isset($childrenStock[$childId])
00310                             and $childrenStock[$childId] == self::STATUS_IN_STOCK
00311                         ) {
00312                             $optionStatus = true;
00313                         }
00314                     }
00315                     $websiteStatus = $websiteStatus && $optionStatus;
00316                 }
00317                 $statuses[$websiteId] = (int)$websiteStatus;
00318             }
00319         }
00320 
00321         foreach ($statuses as $websiteId => $websiteStatus) {
00322             $this->saveProductStatus($productId, $websiteStatus, $qty, $stockId, $websiteId);
00323         }
00324 
00325         return $this;
00326     }

_processParents ( productId,
stockId = 1,
websiteId = null 
) [protected]

Process Parents by child

Parameters:
int $productId
int $stockId
int $websiteId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 336 of file Status.php.

00337     {
00338         $parentIds = array();
00339         foreach ($this->getProductTypeInstances() as $typeInstance) {
00340             /* @var $typeInstance Mage_Catalog_Model_Product_Type_Abstract */
00341             $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productId));
00342         }
00343 
00344         if (!$parentIds) {
00345             return $this;
00346         }
00347 
00348         $productTypes = $this->getProductsType($parentIds);
00349         $item         = $this->getStockItemModel();
00350 
00351         foreach ($parentIds as $parentId) {
00352             $parentType = isset($productTypes[$parentId]) ? $productTypes[$parentId] : null;
00353             $item->setData(array('stock_id' => $stockId))
00354                 ->setOrigData()
00355                 ->loadByProduct($parentId);
00356             $status  = self::STATUS_IN_STOCK;
00357             $qty     = 0;
00358             if ($item->getId()) {
00359                 $status = $item->getIsInStock();
00360                 $qty    = $item->getQty();
00361             }
00362 
00363             $this->_processChildren($parentId, $parentType, $qty, $status, $item->getStockId(), $websiteId);
00364         }
00365 
00366         return $this;
00367     }

addStockStatusToProducts ( productCollection,
websiteId = null,
stockId = null 
)

Add information about stock status to product collection

Parameters:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $productCollection
int|null $websiteId
int|null $stockId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 457 of file Status.php.

00458     {
00459         if ($stockId === null) {
00460             $stockId = Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID;
00461         }
00462         if ($websiteId === null) {
00463             $websiteId = Mage::app()->getStore()->getWebsiteId();
00464         }
00465         $productIds = array();
00466         foreach ($productCollection as $product) {
00467             $productIds[] = $product->getId();
00468         }
00469 
00470         if (!empty($productIds)) {
00471             $stockStatuses = $this->_getResource()->getProductStatus($productIds, $websiteId, $stockId);
00472             foreach ($stockStatuses as $productId => $status) {
00473                 if ($product = $productCollection->getItemById($productId)) {
00474                     $product->setIsSalable($status);
00475                 }
00476             }
00477         }
00478 
00479         /* back compatible stock item */
00480         foreach ($productCollection as $product) {
00481             $object = new Varien_Object(array('is_in_stock' => $product->getData('is_salable')));
00482             $product->setStockItem($object);
00483         }
00484 
00485         return $this;
00486     }

addStockStatusToSelect ( Varien_Db_Select select,
Mage_Core_Model_Website website 
)

Add stock status to prepare index select

Parameters:
Varien_Db_Select $select
Mage_Core_Model_Website $website
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 495 of file Status.php.

00496     {
00497         $this->_getResource()->addStockStatusToSelect($select, $website);
00498         return $this;
00499     }

assignProduct ( Mage_Catalog_Model_Product product,
stockId = 1,
stockStatus = null 
)

Assign Stock Status to Product

Parameters:
Mage_Catalog_Model_Product $product
int $stockId
int $stockStatus
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 192 of file Status.php.

00193     {
00194         if (is_null($stockStatus)) {
00195             $websiteId = $product->getStore()->getWebsiteId();
00196             $status = $this->getProductStatus($product->getId(), $websiteId, $stockId);
00197             $stockStatus = isset($status[$product->getId()]) ? $status[$product->getId()] : null;
00198         }
00199 
00200         $product->setIsSalable($stockStatus);
00201 
00202         return $this;
00203     }

changeItemStatus ( Mage_CatalogInventory_Model_Stock_Item item  ) 

Change Stock Item status process

Parameters:
Mage_CatalogInventory_Model_Stock_Item $item
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 168 of file Status.php.

00169     {
00170         $productId  = $item->getProductId();
00171         if (!$productType = $item->getProductTypeId()) {
00172             $productType    = $this->getProductType($productId);
00173         }
00174 
00175         $status     = (int)$item->getIsInStock();
00176         $qty        = (int)$item->getQty();
00177 
00178         $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId());
00179         $this->_processParents($productId, $item->getStockId());
00180 
00181         return $this;
00182     }

getProductData ( productIds,
websiteId,
stockId = 1 
)

Retrieve Product(s) Data array

Parameters:
int|array $productIds
int $websiteId
int $stockId
Returns:
array

Definition at line 407 of file Status.php.

00408     {
00409         return $this->getResource()->getProductData($productIds, $websiteId, $stockId);
00410     }

getProductStatus ( productIds,
websiteId,
stockId = 1 
)

Retrieve Product(s) status

Parameters:
int|array $productIds
int $websiteId
int $stockId
Returns:
array

Definition at line 394 of file Status.php.

00395     {
00396         return $this->getResource()->getProductStatus($productIds, $websiteId, $stockId);
00397     }

getProductStatusEnabled (  ) 

Retrieve Product Status Enabled Constant

Returns:
int

Definition at line 157 of file Status.php.

getProductStatusModel (  ) 

Retrieve Catalog Product Status Model

Returns:
Mage_Catalog_Model_Product_Status

Definition at line 137 of file Status.php.

00138     {
00139         return Mage::getSingleton('catalog/product_status');
00140     }

getProductsType ( productIds  ) 

Retrieve Products Type as array Return array as key product_id, value type

Parameters:
array|int $productIds
Returns:
array

Definition at line 444 of file Status.php.

00445     {
00446         return $this->getResource()->getProductsType($productIds);
00447     }

getProductType ( productId  ) 

Retrieve Product Type

Parameters:
int $productId
Returns:
string|false

Definition at line 428 of file Status.php.

00429     {
00430         $types = $this->getResource()->getProductsType($productId);
00431         if (isset($types[$productId])) {
00432             return $types[$productId];
00433         }
00434         return false;
00435     }

getProductTypeInstance ( productType  ) 

Retrieve Product Type Instance By Product Type

Parameters:
string $productType
Returns:
Mage_Catalog_Model_Product_Type_Abstract

Definition at line 89 of file Status.php.

00090     {
00091         $types = $this->getProductTypeInstances();
00092         if (isset($types[$productType])) {
00093             return $types[$productType];
00094         }
00095         return false;
00096     }

getProductTypeInstances (  ) 

Retrieve Product Type Instances as key - type code, value - instance model

Returns:
array

Definition at line 68 of file Status.php.

00069     {
00070         if (is_null($this->_productTypes)) {
00071             $this->_productTypes = array();
00072             $productEmulator     = new Varien_Object();
00073 
00074             foreach (array_keys(Mage_Catalog_Model_Product_Type::getTypes()) as $typeId) {
00075                 $productEmulator->setTypeId($typeId);
00076                 $this->_productTypes[$typeId] = Mage::getSingleton('catalog/product_type')
00077                     ->factory($productEmulator);
00078             }
00079         }
00080         return $this->_productTypes;
00081     }

getResource (  ) 

Retrieve resource model wraper

Returns:
Mage_CatalogInventory_Model_Mysql4_Stock_Status

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 417 of file Status.php.

00418     {
00419         return parent::getResource();
00420     }

getStockItemModel (  ) 

Retrieve CatalogInventory empty Stock Item model

Returns:
Mage_CatalogInventory_Model_Stock_Item

Definition at line 147 of file Status.php.

00148     {
00149         return Mage::getModel('cataloginventory/stock_item');
00150     }

getWebsiteDefaultStoreId ( websiteId  ) 

Retrieve Default website store Id

Parameters:
int $websiteId
Returns:
int

Definition at line 123 of file Status.php.

00124     {
00125         $websites = $this->getWebsites();
00126         if (isset($websites[$websiteId])) {
00127             return $websites[$websiteId];
00128         }
00129         return 0;
00130     }

getWebsites ( websiteId = null  ) 

Retrieve website models

Returns:
array

Definition at line 103 of file Status.php.

00104     {
00105         if (is_null($this->_websites)) {
00106             $this->_websites = $this->getResource()->getWebsiteStores();
00107         }
00108 
00109         $websites = $this->_websites;
00110         if (!is_null($websiteId) && isset($this->_websites[$websiteId])) {
00111             $websites = array($websiteId => $this->_websites[$websiteId]);
00112         }
00113 
00114         return $websites;
00115     }

rebuild ( websiteId = null  ) 

Rebuild stock status for all products

Parameters:
int $websiteId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 211 of file Status.php.

00212     {
00213         $lastProductId = 0;
00214         while (true) {
00215             $productCollection = $this->getResource()->getProductCollection($lastProductId);
00216             if (!$productCollection) {
00217                 break;
00218             }
00219 
00220             foreach ($productCollection as $productId => $productType) {
00221                 $lastProductId = $productId;
00222                 $this->updateStatus($productId, $productType, $websiteId);
00223             }
00224         }
00225 
00226         return $this;
00227     }

saveProductStatus ( productId,
status,
qty = 0,
stockId = 1,
websiteId = null 
)

Save product status per website if website is null, saved for all websites

Parameters:
int $productId
int $status
float $qty
int $stockId
int|null $websiteId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 380 of file Status.php.

00381     {
00382         $this->getResource()->saveProductStatus($this, $productId, $status, $qty, $stockId, $websiteId);
00383         return $this;
00384     }

updateStatus ( productId,
productType = null,
websiteId = null 
)

Update product status from stock item

Parameters:
int $productId
string $productType
int $websiteId
Returns:
Mage_CatalogInventory_Model_Stock_Status

Definition at line 237 of file Status.php.

00238     {
00239         if (is_null($productType)) {
00240             $productType = $this->getProductType($productId);
00241         }
00242 
00243         $item = $this->getStockItemModel()->loadByProduct($productId);
00244 
00245         $status  = self::STATUS_IN_STOCK;
00246         $qty     = 0;
00247         if ($item->getId()) {
00248             $status = $item->getIsInStock();
00249             $qty    = $item->getQty();
00250         }
00251 
00252         $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId(), $websiteId);
00253         $this->_processParents($productId, $item->getStockId(), $websiteId);
00254 
00255         return $this;
00256     }


Member Data Documentation

$_productTypes [protected]

Definition at line 44 of file Status.php.

$_websites [protected]

Definition at line 51 of file Status.php.

const STATUS_IN_STOCK = 1

Definition at line 37 of file Status.php.

Definition at line 36 of file Status.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:48 2009 for Magento by  doxygen 1.5.8