Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

QueryLoop WooCommerce Variation Product

  • Hey

    Would it be possible to show in query loop variations of woocommerce products? Like different colors?

  • Hello. Are your attributes global ones or product specific ones?

  • Product specific. Like in the screenshot showing two products with there different color options

  • Use this code to create a new custom dynamic tag for the Color variations you have created:

    /**
     * Register Product Colors Dynamic Tag
     */
    function register_product_colors_dynamic_tag() {
        if (!class_exists('GenerateBlocks_Register_Dynamic_Tag')) {
            return;
        }
    
        new GenerateBlocks_Register_Dynamic_Tag(
            [
                'title'    => __('Product Colors', 'generateblocks'),
                'tag'      => 'product_colors',
                'type'     => 'post',
                'supports' => ['source'],
                'return'   => 'product_colors_callback',
            ]
        );
    }
    add_action('init', 'register_product_colors_dynamic_tag');
    
    /**
     * Product Colors Callback
     *
     * @param array  $options  The options.
     * @param object $block    The block.
     * @param object $instance The block instance.
     * @return string
     */
    function product_colors_callback($options, $block, $instance) {
        if (!function_exists('wc_get_product')) {
            return '';
        }
        
        $product = wc_get_product(get_the_ID());
        
        if (!$product || !$product->is_type('variable')) {
            return '';
        }
        
        $colors = $product->get_attribute('Color');
        
        if (!$colors) {
            return '';
        }
        
        // Replace pipes or commas with slashes
        $colors = str_replace(' | ', ' / ', $colors);
        $colors = str_replace(', ', ' / ', $colors);
        
        return GenerateBlocks_Dynamic_Tag_Callbacks::output($colors, $options, $instance);
    }

    In your query loop, insert a GB text block and open dynamic tags. The last entry from the Post is called Product Colors.

    Product colors dynamic tag

    Select that and check in the front end.

  • Thank you. Will that also show then the Color Variation Picture, if set?

  • No, it will also show Red / Blue for example, as you stated in your image.

  • Ok, but is there an way that images and price also gets shown then for the right variation? Also the correct link to that variation?

  • Sorry but that would require custom development which is out of scope of the current support forum. Hope you understand!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.