I have created a script that deletes duplicated option for custom attribute because this attribute product will generate duplicated value after every save or edit of the product so how to proceed to execute this xscript automatically every 1 minute and thanks in advance Here is my code according to what you have suggest :
class Test {
private $_objectManager;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectmanager
){
$this->_objectManager = $objectmanager;
}
public function execute()
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("Cron is working !");
$logger->info(__METHOD__);
return $this;
// You can write your code logic here
$attributeCode = 'choix_prix_product';
$attribute = $this->_objectManager->create('Magento\Eav\Model\Config')->getAttribute('catalog_product', $attributeCode);
$options = $attribute->getSource()->getAllOptions();
foreach ($options as $option) {
$products = $obj->create('Magento\Catalog\Model\Product')
->getCollection()
->addAttributeToFilter($attributeCode, $option['value']);
$productIds = [];
foreach ($products as $prod) {
$productIds[] = $prod->getId();
}
}
$duplicateOptions = [];
foreach ($options as $option) {
// because the first option can be blank
if(!empty(trim($option['label']))) {
$products = $obj->create('Magento\Catalog\Model\Product')
->getCollection()
->addAttributeToFilter($attributeCode, $option['value']);
if ($products->count()) {
$productsCount = $products->count();
} else {
$productsCount = 0;
}
$duplicateOptions[$option['label']][$option['value']] = $productsCount;
}
}
foreach ($duplicateOptions as $key => $value) {
if (count($value) > 1) {
foreach ($value as $k => $v) {
if ($v == 0) {
$duplicateOptions[$key]['delete'][] = $k;
}
}
if (isset($duplicateOptions[$key]['delete'])) {
/**
* if multiple options are in delete array,
* i.e. multiple option ids have zero product count
*/
if (count($duplicateOptions[$key]['delete']) > 1) {
sort($duplicateOptions[$key]['delete']); // sort the array
array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
/**
* if you assume that the older option id is the duplicate one
* then, you keep the older option id in the delete list
* and remove the latest/newly-added attribute option from the delete list
*/
// rsort($duplicateOptions[$key]['delete']); // reverse sort the array
// array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
}
// DELETE DUPLICATE ATTRIBUTE OPTIONS
foreach ($duplicateOptions[$key]['delete'] as $optionId) {
$optionModel = $obj->get('Magento\Eav\Model\Entity\Attribute\Option')->load($optionId);
try {
$optionModel->delete();
echo '<font color="green">"'.$key.' ('.$optionId.')" duplicate option deleted!</font><br />';
$data .= 'Option Value: ' . $key . ', ' . 'Option ID: ' . $optionId;
$data .= "\n";
} catch(Exception $e) {
echo '<font color="red">'. $e->getMessage() .'</font><br />';
$data .= $e->getMessage();
}
}
}
}
}
}
}
<schedule></schedule>