Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add product attribute to Woocommerce's blocks in Gutenberg

EDIT I: I have found the file where the old plugin Woocommerce Blocks sets the blocks: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/master/src/BlockTypes/FeaturedCategory.php But where is it in the Woocommerce library?

EDIT II: Question in short:

How do you customize the Woocommerce Blocks to show more data than the build in functionality?

------------- background ------------

If you search for adding custom attributes for Woocommerce Blocks you find a lot of WordPress examples for this.

For example, this, where the answer points out, that you can add attributes by using the blocks.registerBlockType. But how to do this for Woocommerce Blocks?

I want to be able to add a data field to the HTML output. The data field should then call a product attribute and show if it exists.

So when you use the Woocommerce Blocks on your front page - for example, the size will be shown underneath the add to cart button - as in the image.

enter image description here

As you might know the functionality of showing/hiding the price, add-to-cart-button, reviews are already there, when you choose a Woocommerce Block on the editing site.

But I haven't found the place where this functionality is created.

This would also be a great help actually - if you could show me where in the Woocommerce Github library the blocks are being created. Maybe I can figure out my self how to filter through them and add the functionality

I know - based on a Udemy course - how to create a custom plugin and create a new blog-type, save and edit.

But I need to figure out what Woocommerce namespace is, how they create their blocks, and what their data is called. The Woocommerce developer handbook is not saying anything about this - not what I've found.

I've been searching the internet for three days now, and I just don't understand that I can't seem to find ANYTHING on this. That nobody else wants to customize this functionality in Woocommerce. I know it is a new function (blocks) in the core, but still.

I just need to be pointed in the right direction.

like image 227
Peter Højlund Palluth Avatar asked Apr 07 '20 21:04

Peter Højlund Palluth


People also ask

How do I add a product attribute in WordPress?

Go to: Products > Add Product (or edit an existing one). Select the Attributes tab in the Product Data. There you can choose any of the attributes that you've created in the dropdown menu. Select Add.

Does WooCommerce blocks work with Elementor?

Both the Pro and Free versions of Elementor are 100% compatible with WooCommerce and EDD. Elementor Pro also has a special set of features that help you showcase your WC products anywhere on the site, in any way you choose.


1 Answers

I was dealing with the exact same problem as you and I found the answer by digging deeply on the WC blocks plugin repo.

I found that you have to apply a filter to this hook: woocommerce_blocks_product_grid_item_html

The original HTML is this:

<li class="wc-block-grid__product">
        <a href="{$data->permalink}" class="wc-block-grid__product-link">
            {$data->image}
            {$data->title}
        </a>
        {$data->badge}
        {$data->price}
        {$data->rating}
        {$data->button}
</li>

So that way you can filter the html code and modify it by adding this chunk of code to your functions.php and customizing it to fit your needs

function wc_add_date_to_gutenberg_block( $html, $data, $product ) {
    $dateStr = get_post_meta($product->get_id(), 'ticket_start_time', true);
    $date = new DateTime($dateStr);
    $data->date = "<p>Date: " . $date->format('d-m-Y H:i') . "</p>";
    $output = "
<li class=\"wc-block-grid__product\">
        <a href=\"{$data->permalink}\" class=\"wc-block-grid__product-link\">
            {$data->image}
            {$data->title}
        </a>
        {$data->date} <- I added this one
        {$data->badge}
        {$data->price}
        {$data->rating}
        {$data->button}
</li>
    ";

    return $output;
}

add_filter("woocommerce_blocks_product_grid_item_html", "wc_add_date_to_gutenberg_block", 10, 3);

like image 100
Charlie Ruiz Avatar answered Sep 29 '22 19:09

Charlie Ruiz