Disabling Magento Product via Code

More precisely, how to simulate the action taken when the user selects "Status: Disabled" on the product edit page in the backend - so that it does not show, does not sell, does not appear in different lists, etc.?

From what I have compiled, Magento sets the product status to 2 when disconnected, which is Mage_Catalog_Model_Product_Status::STATUS_DISABLED .

I tried the code below in Mage_Catalog_Model_Product to find out how it works, but it is not:

  public function getStatus() { return 2; // return $this->_getData('status'); } 

But I think this is not enough, because I assume that Magento uses events to notify listeners that the Product has been disconnected.

PS: Magento EE 1.11.0.2

+6
source share
2 answers

you can use

 Mage::getModel('catalog/product_status')->updateProductStatus($product->getId(), $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED); 

Which looks like this:

 Mage_Catalog_Model_Product_Status /** * Update status value for product * * @param int $productId * @param int $storeId * @param int $value * @return Mage_Catalog_Model_Product_Status */ public function updateProductStatus($productId, $storeId, $value) { Mage::getSingleton('catalog/product_action') ->updateAttributes(array($productId), array('status' => $value), $storeId); // add back compatibility event $status = $this->_getResource()->getProductAttribute('status'); if ($status->isScopeWebsite()) { $website = Mage::app()->getStore($storeId)->getWebsite(); $stores = $website->getStoreIds(); } else if ($status->isScopeStore()) { $stores = array($storeId); } else { $stores = array_keys(Mage::app()->getStores()); } foreach ($stores as $storeId) { Mage::dispatchEvent('catalog_product_status_update', array( 'product_id' => $productId, 'store_id' => $storeId, 'status' => $value )); } return $this; } 

Dispatch event based on

  <catalog_product_status_update> <observers> <sales_quote> <class>sales/observer</class> <method>catalogProductStatusUpdate</method> </sales_quote> </observers> </catalog_product_status_update> 

Here is the method

 Mage_Sales_Model_Observer /** * Catalog Mass Status update process * * @param Varien_Event_Observer $observer * @return Mage_Sales_Model_Observer */ public function catalogProductStatusUpdate(Varien_Event_Observer $observer) { $status = $observer->getEvent()->getStatus(); if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) { return $this; } $productId = $observer->getEvent()->getProductId(); Mage::getResourceSingleton('sales/quote')->markQuotesRecollect($productId); return $this; } 

Here is the resource model

 Mage_Catalog_Model_Resource_Product_Status /** * Update product status for store * * @param int $productId * @param int $storId * @param int $value * @return Mage_Catalog_Model_Resource_Product_Status */ public function updateProductStatus($productId, $storeId, $value) { $statusAttributeId = $this->_getProductAttribute('status')->getId(); $statusEntityTypeId = $this->_getProductAttribute('status')->getEntityTypeId(); $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable(); $refreshIndex = true; $adapter = $this->_getWriteAdapter(); $data = new Varien_Object(array( 'entity_type_id' => $statusEntityTypeId, 'attribute_id' => $statusAttributeId, 'store_id' => $storeId, 'entity_id' => $productId, 'value' => $value )); $data = $this->_prepareDataForTable($data, $statusTable); $select = $adapter->select() ->from($statusTable) ->where('attribute_id = :attribute_id') ->where('store_id = :store_id') ->where('entity_id = :product_id'); $binds = array( 'attribute_id' => $statusAttributeId, 'store_id' => $storeId, 'product_id' => $productId ); $row = $adapter->fetchRow($select); if ($row) { if ($row['value'] == $value) { $refreshIndex = false; } else { $condition = array('value_id = ?' => $row['value_id']); $adapter->update($statusTable, $data, $condition); } } else { $adapter->insert($statusTable, $data); } if ($refreshIndex) { $this->refreshEnabledIndex($productId, $storeId); } return $this; } 

There is no observer that directly performs this function, but the event is dispatched to Status.php.

+9
source

You might also need to modify Mage_Catalog_Model_Product_Status :

 public function getProductStatus($productIds, $storeId = null) { //return $this->getResource()->getProductStatus($productIds, $storeId); return 2; } 

This method is called by the CatalogInventory module to determine if a product is available.

0
source

Source: https://habr.com/ru/post/912605/


All Articles