Skip to content

Instantly share code, notes, and snippets.

@edavydova
Last active December 24, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edavydova/a8ed674528a065a71094fe438aac6663 to your computer and use it in GitHub Desktop.
Save edavydova/a8ed674528a065a71094fe438aac6663 to your computer and use it in GitHub Desktop.
<?php
define('AREA', 'A');
define('ACCOUNT_TYPE', 'admin');
require(dirname(__FILE__) . '/init.php');
\Tygh\Registry::set('runtime.company_id', 0);
$update_product_ids = [];
$check_created_features = false;
$products = db_get_hash_multi_array('SELECT product_id, combination, combination_hash FROM ?:product_options_inventory', ['product_id', 'combination_hash']);
foreach ($products as $product_id => $combinations) {
fn_echo("Starting check product {$product_id}<br>");
foreach ($combinations as $combination) {
$variation_options = [];
$comb = explode('_', $combination['combination']);
if (!empty($comb) && is_array($comb)) {
$iterations = count($comb);
for ($i = 0; $i < $iterations; $i += 2) {
$variation_options[$comb[$i]] = isset($comb[$i + 1]) ? $comb[$i + 1] : '';
}
}
foreach ($variation_options as $option_id => $variant_id) {
$option = db_get_hash_array(
'SELECT po.option_id, po.value, po.product_id, pod.option_name FROM ?:product_options AS po'
. ' LEFT JOIN ?:product_options_descriptions AS pod ON pod.option_id = po.option_id AND pod.lang_code = ?s'
. ' WHERE po.option_id = ?i',
'option_id',
CART_LANGUAGE,
$option_id
);
$variant_exists = (bool) db_get_field('SELECT variant_id, option_id FROM ?:product_option_variants WHERE option_id = ?i AND variant_id = ?i', $option_id, $variant_id);
$option_exists = (bool) $option;
if (!$variant_exists || !$option_exists) {
if (!$option_exists) {
fn_echo("Option {$option_id} not found<br>");
}
if (!$variant_exists) {
fn_echo("Option variant {$variant_id} not found<br>");
}
fn_echo("<strong>Product type of product {$product_id} will be updated from combination to simple product!</strong><br>");
$update_product_ids[$product_id] = $product_id;
break(2);
}
if ($check_created_features) {
$option = $option[$option_id];
$value = $option['value'];
if (strpos($value, 'pvc_') === 0) {
$feature_key = $value;
} elseif (empty($option['product_id'])) {
$feature_key = md5(sprintf('%s_%s_global', $option['option_id'], $option['option_name']));
} else {
$category_id = db_get_field(
'SELECT category_id FROM ?:products_categories'
. ' WHERE product_id = ?i AND link_type = ?s',
$option['product_id'],
'M'
);
$feature_key = md5(sprintf('%s_%s_local', $category_id, $option['option_name']));
}
if (empty($feature_key)) {
$update_product_ids[$product_id] = $product_id;
fn_echo("<strong>Feature key by option {$option['option_id']} : {$option['option_name']} was not generate!</strong><br>");
continue;
}
$feature_id = db_get_field(
'SELECT feature_id FROM ?:product_features WHERE feature_code = ?s AND purpose IN (?a)',
$feature_key, ['group_catalog_item', 'group_variation_catalog_item']
);
if (empty($feature_id)) {
$update_product_ids[$product_id] = $product_id;
fn_echo("<strong>Feature was not created by key: $feature_key for option {$option['option_id']} : {$option['option_name']}!</strong><br>");
}
}
}
}
fn_echo("Checking product {$product_id} finished<br>");
}
foreach ($update_product_ids as $product_id) {
$url = fn_url('products.update?product_id=' . $product_id);
fn_echo("<a href=\"{$url}\" target=\"_blank\">{$url}</a><br>");
}
fn_echo("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment