How to disable all products from specific category?
-
14-04-2021 - |
题
I am using CE 1.7.0.2, I want to disable all products from one specific category.
How I can disable all products from category? (SQL is fine)
解决方案
Create a new file in in your Magento root, call it products-disable.php
and paste in the following
<?php
require_once('app/Mage.php');
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categoryId = 123;
$category = Mage::getModel('catalog/category')->load($categoryId);
if ($category->getId()) {
$products = $category->getProductCollection();
foreach ($products as $product) {
$product->setData('status', false);
$product->save();
}
} else {
die("category failed to load");
}
Then browse to your site http://mymagesite.url/products-disable.php
and it should set the status of all the products in that category as false
DONT RUN THIS STRAIGHT ON YOUR PRODUCTION SITE. Test it out first :)
edit :
This magento code will update your database as a bulk action, so far quicker than the initial save on each row. I've batched it in pages of 500
, change that value in the setPageSize
call to whatever you please. I'd recommend this method FAR more than doing a direct sql change as it will trigger events and indexes and stuff.
I haven't tested this code, but believe it should work.
<?php
require_once('app/Mage.php');
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categoryId = 123;
$storeCode = 'default';
$storeId = Mage::getModel('core/store')->load($storeCode)->getId();
$category = Mage::getModel('catalog/category')->load($categoryId);
if ($storeId && $category->getId()) {
$collection = $category->getProductCollection();
$collection->getSelect()->reset(Zend_Db_Select::COLUMNS)->columns('entity_id');
$collection->setPageSize(500);
for ($curPage = 1; $curPage <= $collection->getLastPageNumber(); $curPage++) {
$collection->clear();
$collection->setCurPage($curPage);
$ids = array();
foreach ($collection as $_product) {
$ids[$_product->getId()] = $_product->getId();
}
if (!empty($ids)) {
Mage::getSingleton('catalog/product_action')->updateAttributes($ids, array('status' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED), $storeId);
}
}
} else {
die("category or store failed to load");
}
其他提示
Hi try this which is give you list of products specifiv cat
For getting this category products:
SELECT `e`.`value_id` FROM `catalog_product_entity_int` AS `e`
LEFT JOIN `catalog_category_product` AS `cp` ON e.entity_id = cp.product_id WHERE (cp.category_id IN(15)) and e.attribute_id = 96 )
and disable the product by
update `catalog_product_entity_int` set `catalog_product_entity_int`.value_id =0 where `catalog_product_entity_int`.`value_id` in (YOUR_UPPer_value_Ids)
Where 15 in category ids
FOR MAGENTO 2 Disable All products from a specific category.
UPDATE `catalog_category_product` JOIN `catalog_product_entity_int` ON catalog_category_product.product_id = catalog_product_entity_int.entity_id SET catalog_product_entity_int.value = 2 WHERE catalog_category_product.category_id=1754 AND catalog_product_entity_int.attribute_id = 94
Run This query.
- attribute id 94 taken from eav_attribute table. i.e enable/disable field of product
- Category id has taken static because I want to update from this category.
- value=2 means disable the product
So this query is taking all products from catalog_category_product.product_id WHERE category_id= 1754. And then matching product id and entity id in catalog_product_entity_int and updating value in catalog_product_entity_int.value column as disable i.e 2