2012-04-06 168 views
6
禁用Magento的產品

更準確地說,如何模擬當用戶選擇「狀態:已停用」提出的行動後端的產品編輯頁面 - 因此,它不是表示,出售各種列表等表現?通過代碼

從我收集的信息來看,Magento在禁用時將產品的狀態設置爲2,即Mage_Catalog_Model_Product_Status::STATUS_DISABLED

我試過下面的代碼在Mage_Catalog_Model_Product怎麼看/如果它的工作原理,但它並不:

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

但我認爲這還不夠,因爲我相信Magento會用事件來通知偵聽該產品已被禁用。

PS:Magento的EE 1.11.0.2

回答

9

你可以使用

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

它看起來像這樣

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; 
} 

調度事件基於

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

這裏是方法

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; 
    } 

下面是資源模型

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; 
    } 

沒有觀察者直接執行該功能,但事件在Status.php調度。

0

也許你需要修改Mage_Catalog_Model_Product_Status還有:

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

此方法由CatalogInventory模塊調用,以確定任何一個產品是可用的。