function fn_get_options_variants_by_option_variant_id($product_id, $variantsOptionsSelected = array())
{
    $condition = "";
    $fields = "?:product_options.option_id, c.variant_id";
    $condition .= db_quote(' (?:product_options.product_id = ?i OR (?:product_options.product_id=0 AND n.product_id = ?i))', $product_id, $product_id);
    $join = db_quote(' LEFT JOIN ?:product_global_option_links n ON ?:product_options.option_id = n.option_id ');
    $join .= db_quote(' JOIN ?:product_option_variants c ON ?:product_options.option_id = c.option_id');
    $opts = db_get_array("SELECT " . $fields . " FROM ?:product_options " . $join . " WHERE " . $condition . " GROUP BY c.variant_id, ?:product_options.option_id" . " ORDER BY ?:product_options.position, c.position");
    $initialOptionAndVariants = array();
    foreach ($opts as $opt) {
        $initialOptionAndVariants[$opt['option_id']][] = $opt['variant_id'];
    }
    $variantOptionsCombinationsArray = fn_get_linked_option_variants($product_id);
    //var_dump($variantOptionsCombinationsArray);
    $startArray = $variantOptionsCombinationsArray['option_variants'];
    $infoArray = $variantOptionsCombinationsArray['variant_option_ids'];
    $test_options_variants = fn_traverse_array_options($startArray, $infoArray);
    $followOptionsLinks = followOptionsLinks($test_options_variants, $variantsOptionsSelected);
    $mainOptions = getMainOptions($followOptionsLinks);
    $selectedOptionArray = createOptionsVariantsArrayBySelectedInfo($test_options_variants, $variantsOptionsSelected, $mainOptions);
    //var_dump($selectedOptionArray);echo"<br/>_________<br/>";
    foreach ($mainOptions as $mainOption) {
        unset($selectedOptionArray[$mainOption]);
    }
    foreach ($selectedOptionArray as $selectedOptionkey => $selectedOption) {
        if (count($selectedOption) > 1) {
            $arrayToIntersect = array();
            foreach ($selectedOption as $selectedOption1) {
                if ($selectedOption1) {
                    $arrayToIntersect[] = $selectedOption1;
                }
            }
            if (count($arrayToIntersect) > 1) {
                $selectedOptionArray[$selectedOptionkey] = call_user_func_array('array_intersect', $arrayToIntersect);
            } else {
                $selectedOptionArray[$selectedOptionkey] = $arrayToIntersect[0];
            }
        } elseif (count($selectedOption) == 1) {
            $selectedOptionArray[$selectedOptionkey] = $selectedOption[0];
        }
    }
    //    var_dump($selectedOptionArray);echo"<br/>________________<br/>";
    $selectedOptionArrayOrderedByVariantsOrders = array();
    foreach ($initialOptionAndVariants as $key1 => $value1) {
        foreach ($value1 as $key2 => $value2) {
            if ($selectedOptionArray[$key1] && in_array($value2, $selectedOptionArray[$key1])) {
                $selectedOptionArrayOrderedByVariantsOrders[$key1][] = $value2;
            }
        }
    }
    $selectedOptionArray = $selectedOptionArrayOrderedByVariantsOrders;
    return $selectedOptionArray;
}
function fn_get_options_variants_by_option_variant_id($product_id, $variantsOptionsSelected = array())
{
    $fields = "?:product_options.option_id, c.variant_id";
    $condition .= db_quote(' (?:product_options.product_id = ?i OR (?:product_options.product_id=0 AND n.product_id = ?i))', $product_id, $product_id);
    $join = db_quote(' LEFT JOIN ?:product_global_option_links n ON ?:product_options.option_id = n.option_id ');
    $join .= db_quote(' JOIN ?:product_option_variants c ON ?:product_options.option_id = c.option_id');
    $opts = db_get_array("SELECT " . $fields . " FROM ?:product_options " . $join . " WHERE " . $condition . " GROUP BY c.variant_id, ?:product_options.option_id" . " ORDER BY ?:product_options.position, c.position");
    $initialOptionAndVariants = array();
    foreach ($opts as $opt) {
        $initialOptionAndVariants[$opt['option_id']][] = $opt['variant_id'];
    }
    $variantOptionsCombinationsArray = fn_get_linked_option_variants($product_id);
    $startArray = $variantOptionsCombinationsArray['option_variants'];
    $infoArray = $variantOptionsCombinationsArray['variant_option_ids'];
    $test_options_variants = fn_traverse_array_options($startArray, $infoArray);
    $mainOptions = getMainOptions($test_options_variants);
    $selectedOptionArray = createOptionsVariantsArrayBySelectedInfo($test_options_variants, $variantsOptionsSelected);
    echo "<br/><br/><br/><br/>";
    var_dump($product_id);
    echo "<br/>";
    var_dump($variantsOptionsSelected);
    echo "<br/>";
    foreach ($mainOptions as $mainOption) {
        unset($selectedOptionArray[$mainOption]);
    }
    foreach ($selectedOptionArray as $selectedOptionkey => $selectedOption) {
        if (count($selectedOption) > 1) {
            $selectedOptionArray[$selectedOptionkey] = call_user_func_array('array_intersect', $selectedOption);
        }
    }
    return $selectedOptionArray;
}