3

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 :)

asked Nov 16, 2017 at 8:31

2 Answers 2

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.

answered Nov 16, 2017 at 9:00
2
  • 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:50 Commented 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; Commented Nov 16, 2017 at 11:07
2

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();
answered Nov 16, 2017 at 8:56
3
  • Thanks for reply. I have tried with above code. It's still enabling/disabling product for default store. Commented 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 save Commented 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? Commented Nov 16, 2017 at 12:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.