Issue
The below query correctly gives me a list of Enabled Configurable items, where ALL associated simple items are disabled. How can I amend this to update the 'status' attribute value to '2' for the configurable items in this list. (They have no child items so need to be disabled)
I appreciate I have mixed together approaches of table naming and referencing. I'm new to this and have combined elements of different solutions.
SELECT `mgic_catalog_product_entity`.`entity_id` FROM (((`mgic_eav_attribute`
join `mgic_catalog_product_entity_int` on ((`mgic_eav_attribute`.`attribute_id` = `mgic_catalog_product_entity_int`.`attribute_id`)))
join `mgic_catalog_product_entity` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_catalog_product_entity`.`entity_id`)))
join `mgic_cataloginventory_stock_item` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_cataloginventory_stock_item`.`product_id`)))
WHERE `mgic_catalog_product_entity`.`type_id` = 'configurable' AND ((`mgic_eav_attribute`.`attribute_code` = 'status') and
(`mgic_catalog_product_entity_int`.`value` = 2)) AND NOT EXISTS(
SELECT *
FROM mgic_catalog_product_super_link cpsl
INNER JOIN mgic_catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = `mgic_catalog_product_entity`.`entity_id`
AND cpei.attribute_id = 97
AND cpei.value = 1
);
Solution
Fetch IDs of configurables with disabled simples
As status
attribute will always have id 97 you could use the following SQL:
SELECT entity_id FROM `catalog_product_entity` cpe
WHERE `type_id` = 'configurable' AND NOT EXISTS(
SELECT *
FROM catalog_product_super_link cpsl
INNER JOIN catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = cpe.entity_id
AND cpei.attribute_id = 97
AND cpei.value = 1
);
Disable products by IDs
To answer your updated question I would suggest you fetching all entity ids and then just use Magento models to change status of each product like suggested here https://magento.stackexchange.com/questions/152263/how-to-disable-enable-a-product-programatically-in-magento2 . For example:
<?php
class Example
{
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $productRepository;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollectionFactory;
/**
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
$this->productRepository = $productRepository;
$this->productCollectionFactory = $productCollectionFactory;
}
/**
* Disable all products by IDs
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function disableProducts($productIds)
{
$productCollection = $this->productCollectionFactory()->create();
$productCollection->addAttributeToFilter('type_id', array('eq' => 'configurable'))
->setPageSize(999);
$productCollection->getSelect()
->where('NOT EXISTS(
SELECT *
FROM catalog_product_super_link cpsl
INNER JOIN catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = e.entity_id
AND cpei.attribute_id = 97
AND cpei.value = 1
)');
foreach ($productCollection as $p) {
$product = $this->productRepository->getById(
$p->getId(),
true /* edit mode */,
0 /* global store*/,
true/* force reload*/
);
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$this->productRepository->save($product);
}
}
}
Answered By - Konstantin Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.