Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harshvardhanmalpani/cebdef222eea721453d53e8ff6b3f4a2 to your computer and use it in GitHub Desktop.
Save harshvardhanmalpani/cebdef222eea721453d53e8ff6b3f4a2 to your computer and use it in GitHub Desktop.
This code removes duplicate custom options for all products. Use toggle for test and debug.
<?php
$debug=0;
$test=0;
/*
Code by Harshvardhan Malpani - https://harshmalpani.in
Tested for Magento CE 1.9.x
Date: 25/04/2017
Use $debug and $test toggles to display messages.
This script will remove all duplicate custom options in each product (duplicate logic = same title for multiple custom options)
*/
function array_dup($ar)
{
return array_unique(array_diff_assoc($ar,array_unique($ar)));
}
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$c=0;$d=0;
if($test==0)
{
$productsArr = Mage::getModel('catalog/product')->getCollection()->setPageSize(20)->setCurPage(1);
}
else
{
$productsArr = Mage::getModel('catalog/product')->getCollection();
}
foreach ($productsArr as $productss)
{
$i=0;
$product = Mage::getModel('catalog/product')->load($productss->getId());
if($debug)
{
echo $productss->getId().'<br>';
}
$arr=array();
foreach ($product->getOptions() as $k=>$value)
{
$arr['name'][]= $value->getTitle();
$arr['id'][]= $value->getId();
$i++;
}
$result=array_intersect_assoc($arr[0],$arr[1]);
if($test)
{
var_dump($arr);
var_dump(array_dup($arr['name']));
}
$dupli=array_dup($arr['name']);
foreach($dupli as $k=>$v)
{
foreach($product->getOptions() as $onelastride)
{
if($onelastride->getId()==$arr['id'][$k])
{
if($debug)
{
echo $onelastride->getTitle().' will be deleted.<br>';
}
$c++;
$onelastride->delete();
}
}
$product->save();
$d++;
}
}
echo $c .' options removed across '.$d.' products in store';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment