Pokud potřebujete přidat v eshopu custom pole jen pro variantní produkty, ACF tuto podmínku standardně neumí. Ale není problém mu ji zadat. Nezbytné je ale ACF Pro. V ACF free tato možnost vůbec není.

Do functions.php stačí přidat toto:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action('acf/init', function() {
// Přidej nový typ podmínky
add_filter('acf/location/rule_types', function($choices) {
$choices['Product']['Product Type (custom)'] = 'product_type_custom';
return $choices;
});
// Přidej hodnoty podmínky
add_filter('acf/location/rule_values/product_type_custom', function($choices) {
error_log('Zaregistrováno pravidlo: product_type_custom');
$choices['variation'] = 'Variantní produkt';
return $choices;
});
// Vyhodnocení podmínky
add_filter('acf/location/rule_match/product_type_custom', function($match, $rule, $options) {
$post_id = $options['post_id'] ?? 0;
$post = get_post($post_id);
if (!$post) return false;
$is_variation = $post->post_type === 'product_variation';
if ($rule['operator'] === '==') {
$match = $is_variation;
} elseif ($rule['operator'] === '!=') {
$match = !$is_variation;
}
return $match;
}, 10, 3);
});
add_action('acf/init', function() { // Přidej nový typ podmínky add_filter('acf/location/rule_types', function($choices) { $choices['Product']['Product Type (custom)'] = 'product_type_custom'; return $choices; }); // Přidej hodnoty podmínky add_filter('acf/location/rule_values/product_type_custom', function($choices) { error_log('Zaregistrováno pravidlo: product_type_custom'); $choices['variation'] = 'Variantní produkt'; return $choices; }); // Vyhodnocení podmínky add_filter('acf/location/rule_match/product_type_custom', function($match, $rule, $options) { $post_id = $options['post_id'] ?? 0; $post = get_post($post_id); if (!$post) return false; $is_variation = $post->post_type === 'product_variation'; if ($rule['operator'] === '==') { $match = $is_variation; } elseif ($rule['operator'] === '!=') { $match = !$is_variation; } return $match; }, 10, 3); });
add_action('acf/init', function() {

    // Přidej nový typ podmínky
    add_filter('acf/location/rule_types', function($choices) {
        $choices['Product']['Product Type (custom)'] = 'product_type_custom';
        return $choices;
    });

    // Přidej hodnoty podmínky
    add_filter('acf/location/rule_values/product_type_custom', function($choices) {
        error_log('Zaregistrováno pravidlo: product_type_custom');
        $choices['variation'] = 'Variantní produkt';
        return $choices;
    });

    // Vyhodnocení podmínky
    add_filter('acf/location/rule_match/product_type_custom', function($match, $rule, $options) {
        $post_id = $options['post_id'] ?? 0;
        $post = get_post($post_id);

        if (!$post) return false;

        $is_variation = $post->post_type === 'product_variation';

        if ($rule['operator'] === '==') {
            $match = $is_variation;
        } elseif ($rule['operator'] === '!=') {
            $match = !$is_variation;
        }

        return $match;
    }, 10, 3);

});

A ve výberu podmínek zobrazení se přidá možnost pouze pro variantní produkty.

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *

three − 2 =