2012-04-06 42 views
6

Chính xác hơn, cách mô phỏng hành động được thực hiện khi người dùng chọn "Trạng thái: Đã tắt" trong trang Chỉnh sửa sản phẩm trong phần phụ trợ - để nó không được hiển thị, bán, hiển thị trong các danh sách khác nhau, v.v ...?Vô hiệu hóa Sản phẩm Magento qua mã

Từ những gì tôi đã thu thập, Magento đặt trạng thái của sản phẩm thành 2 khi bị vô hiệu hóa, tức là Mage_Catalog_Model_Product_Status::STATUS_DISABLED.

Tôi đã thử đoạn code dưới đây trong Mage_Catalog_Model_Product để xem làm thế nào/nếu nó hoạt động, nhưng nó không:

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

Nhưng tôi nghĩ đó là không đủ như tôi đoán Magento sử dụng các sự kiện để thông báo cho người nghe mà Sản phẩm đã bị vô hiệu hóa.

PS: Magento EE 1.11.0.2

Trả lời

9

Bạn có thể sử dụng

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

nào trông như thế này

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

Sự kiện văn được dựa trên

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

Dưới đây là phương pháp

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

Dưới đây là mô hình tài nguyên

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

Không có quan sát viên trực tiếp thực hiện chức năng này, nhưng một sự kiện được gửi đi trong Status.php.

0

Có lẽ bạn cần phải sửa đổi Mage_Catalog_Model_Product_Status cũng như:

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

Phương pháp này được gọi bằng CatalogInventory module để xác định hoặc là một sản phẩm có sẵn.

Các vấn đề liên quan