I want to disable product for admin store means product should be disable for all store/website. I am trying to do something like this but product disable for default store only:
$productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
$productObj = $productRepository->get('sku');
$productObj->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$productRepository->save($productObj);
I have also tried to pass global store id i.e. 0 like this but it is also disable only default store.
$productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
$productObj = $productRepository->get('sku',true,0,true);
$productObj->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$productRepository->save($productObj);
Can anyone help? Thanks in advance :)
2 Answers 2
You can use product action class instead of using product repository and load for each product.
private $action;
private $storeManager;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Action $productAction,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->action = $productAction;
$this->storeManager = $storeManager;
}
public function execute() {
$productIds = [1,2,3];
$updateAttributes['status'] = \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED;
$storeIds = array_keys($this->storeManager->getStores());
foreach ($storeIds as $storeId) {
$this->action->updateAttributes($productIds, $updateAttributes, $storeId);
}
}
Refer what updateAttributes function does git
I have not tested this code but hope this works for you.
-
Thanks for reply. The above code is giving error.
Fatal error: Uncaught Error: Call to a member function getAttributeId() on boolean in public_html/vendor/magento/module-catalog/Model/ResourceModel/Product/Action.php:50Developer Kapil– Developer Kapil2017年11月16日 10:41:20 +00:00Commented Nov 16, 2017 at 10:41 -
Try my updated code I changed this line
$updateAttributes['status'] = \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED;Priyank– Priyank2017年11月16日 11:07:46 +00:00Commented Nov 16, 2017 at 11:07
You can use below code to disable or enable product from magento root
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$product = $obj->get('Magento\Catalog\Model\Product')->load(1);
$product->setStoreId(0) // you can set store id here
$product->setStatus(2); // 2 => Disable , 1 => Enable
$product->save();
-
Thanks for reply. I have tried with above code. It's still enabling/disabling product for default store.Developer Kapil– Developer Kapil2017年11月16日 10:46:01 +00:00Commented Nov 16, 2017 at 10:46
-
If you have multiple store and you want to save data by store then you have to set store id before saveVaibhav Ahalpara– Vaibhav Ahalpara2017年11月16日 11:22:44 +00:00Commented Nov 16, 2017 at 11:22
-
Yes I have multiple store and want to enable/disable to admin store only. I have also mention in my question. Where I can set store id in above code?Developer Kapil– Developer Kapil2017年11月16日 12:28:59 +00:00Commented Nov 16, 2017 at 12:28
Explore related questions
See similar questions with these tags.