Skip to content

Instantly share code, notes, and snippets.

@PululuK
Last active January 22, 2021 20:31
Show Gist options
  • Save PululuK/9ccb44262eb06f8593f89dd7c146e0fc to your computer and use it in GitHub Desktop.
Save PululuK/9ccb44262eb06f8593f89dd7c146e0fc to your computer and use it in GitHub Desktop.
Combination genrator
<?php
require(dirname(__FILE__).'/config/config.inc.php');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
die('êtes vous sûre de réaliser cette action ?? Veuillez soovergarder votre base de données avant');
function getAllProductsID(){
return Db::getInstance()->executeS('
SELECT id_product
FROM `'._DB_PREFIX_.'product`
');
}
function generateProductCombinations($id_product, $logMessage = true, $deleteOlds = true){
$product = new Product((int)$id_product);
$options = [];
$id_lang = Context::getContext()->language->id;
$idAttributeGroupSkeep = 110;
$log = '';
$combinations = $product->getAttributeCombinations($id_lang);
if(!empty($combinations) && Validate::isLoadedObject($product)) {
$combinationsToDelete = [];
$attriubtesList = [];
foreach ($combinations as $k=>$v) {
$combinationObject = new Combination((int)$v['id_product_attribute']);
if(Validate::isLoadedObject($combinationObject) && $combinationObject->id) {
$combinationsToDelete[$combinationObject->id] = $combinationObject->id;
$attributes = Db::getInstance()->executeS('
SELECT id_attribute
FROM `'._DB_PREFIX_.'product_attribute_combination`
WHERE `id_product_attribute` = '.$combinationObject->id
);
foreach($attributes as $attribute) {
$attributesObject = new Attribute($attribute['id_attribute']);
$attributeGroupObject = new AttributeGroup((int)$attributesObject->id_attribute_group);
if ( Validate::isLoadedObject($attributesObject)
&& Validate::isLoadedObject($attributeGroupObject)
&& (int)$attributeGroupObject->id != $idAttributeGroupSkeep
) {
$attriubtesList[$attributeGroupObject->id][$attributesObject->id] = $attributesObject->id;
} else {
$log .= 'Attribuit '.$attribute['id_attribute'].' ou Group d\'attribuits invalide <br>';
}
}
}
}
// Delet old combinations from PS 1.6
if(!empty($combinationsToDelete) && $deleteOlds) {
foreach($combinationsToDelete as $k=>$id_product_attribute) {
$combinationObject = new Combination((int)$id_product_attribute);
if(!$combinationObject->delete()){
$log .= 'Cant delete combination ID : '.(int)$id_product_attribute.'<br>';
}
}
}
// Generate new combination for PS 1.7 (based on olds combinations attributes)
if(!empty($attriubtesList)) {
// Generate attibute process
SpecificPriceRule::disableAnyApplication();
//add combination if not already exists
$combinations = array_values(AdminAttributeGeneratorController::createCombinations(array_values($attriubtesList)));
$combinationsValues = array_values(array_map(function () use ($product) {
return array(
'id_product' => $product->id,
);
}, $combinations));
$product->generateMultipleCombinations($combinationsValues, $combinations, false);
Product::updateDefaultAttribute($product->id);
SpecificPriceRule::enableAnyApplication();
SpecificPriceRule::applyAllRules(array((int) $product->id));
}
} else {
$log .= 'Product ID : '.$id_product.' invalide ou pas de déclinaions <br>';
}
if($logMessage){
echo $log;
}
}
function bulkCombinationGenarator(){
$productsID = getAllProductsID();
if(!empty($productsID)) {
foreach($productsID as $k=>$v) {
generateProductCombinations($v['id_product']);
}
}
}
// Start
bulkCombinationGenarator();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment