In my home page use gutenberg blocks (WooCommerce Products Recents), works fine. But I need add some text itens on title of products.
When using WooCommerce shortcodes, works fine but not work when using blocks.
How hook/filter blocks?
function my_info(){
// some logic
echo "(sold out)";
}
add_action('woocommerce_after_shop_loop_item_title', 'my_info', 999);
Looking for a way to use an action or filter to interact with blocks.
2
Answers
It’s not that easy with Gutenberg blocks. To extend blocks you need to have at least basic knowledge in React. Maybe the following link from WooCommerce might help you:
https://developer.woocommerce.com/2023/08/07/extending-the-woocommerce-checkout-block-to-add-custom-shipping-options/
Can You Use Hooks?
In the Gutenberg block system, PHP hooks like those you’re used to in WooCommerce won’t directly apply. Gutenberg blocks are primarily constructed using JavaScript, particularly ReactJS. Therefore, the PHP-based WordPress hooks don’t interact with these blocks in the way they would with PHP-generated content.
How to Achieve Similar Functionality?
Custom Block: Create a custom Gutenberg block that incorporates your specific modifications.
Dynamic Blocks: Use a Dynamic Block that renders with PHP. In the PHP function that renders your block, you can add your custom hook.
In
your_render_function
, include:JavaScript Hooks: If the block exposes JavaScript actions or filters, you can use those to modify its behavior.
What about WooCommerce Blocks?
WooCommerce-specific blocks may expose their own hooks or methods for customization. Consult the WooCommerce Blocks documentation for those specifics.
While traditional PHP WordPress hooks won’t directly work with Gutenberg blocks, you can use a combination of custom blocks, dynamic blocks, and JavaScript hooks to achieve similar outcomes.