Goal
Create a dynamic block that shows product data and works inside Product Collection.
Key concepts
@wordpress/create-block— a block scaffold with a buildblock.json:render: file:./render.php— server render, always fresh datausesContext: ["postId"]— the block gets the product ID from the template or the collectionrender.phphas$attributes,$content,$block;get_block_wrapper_attributes()- Block attributes — settings in the inspector (
InspectorControls)
Diagram
flowchart LR
ED["Editor<br/>edit.js — preview"] --> SAVE["save: null"]
SAVE --> DB[("post_content:<br/><!-- wp:my/stock-badge /-->")]
DB --> R["render.php<br/>wc_get_product( postId )"]
R --> HTML["Badge HTML<br/>for every product"]
Code
block.json:
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "my/stock-badge",
"title": "Stock badge",
"category": "woocommerce",
"usesContext": [ "postId" ],
"attributes": {
"threshold": { "type": "number", "default": 5 }
},
"supports": { "html": false, "color": { "background": true, "text": true } },
"textdomain": "my-plugin",
"editorScript": "file:./index.js",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
render.php:
<?php
$product_id = $block->context['postId'] ?? get_the_ID();
$product = $product_id ? wc_get_product( $product_id ) : null;
if ( ! $product || ! $product->managing_stock() ) {
return;
}
$qty = (int) $product->get_stock_quantity();
$threshold = (int) ( $attributes['threshold'] ?? 5 );
if ( $qty <= 0 || $qty > $threshold ) {
return;
}
?>
<p <?php echo get_block_wrapper_attributes( [ 'class' => 'stock-badge' ] ); ?>>
<?php
/* translators: %d: items left */
echo esc_html( sprintf( _n( 'Only %d left', 'Only %d left', $qty, 'my-plugin' ), $qty ) );
?>
</p>
Registration:
<?php
add_action( 'init', static function () {
register_block_type( __DIR__ . '/build/stock-badge' );
} );
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| In Product Collection the block shows the same product every time | It uses get_the_ID() instead of the context | usesContext and $block->context['postId'] |
| The badge is stale after the stock changes | A static block (save with HTML) | Dynamic render through render.php |
| The block "broke" in the editor after a markup change | The save() of a static block changed without deprecated | A dynamic block or deprecated |
Weak and strong answer
Question: A static or a dynamic block for a stock badge?
Weak answer. Static — it's simpler.
Strong answer. Dynamic. The stock changes without editing the page, so the HTML has to be generated at request time. A static block would store the number in
post_contentforever. I takepostIdfrom the context so the block works both in the product template and in Product Collection.
Why the second is stronger: The candidate picks the block type by the nature of the data and remembers the context.
Practice
In the terminal: generate the dynamic stock-badge block with create-block, look at what's in src and build, build it and activate the plugin.
On your own site: turn the block into a stock badge with the code above and add a threshold attribute with a slider in the inspector. Insert the block into the Product Template inside Product Collection.
Interview Questions
When the data changes independently of content editing: prices, stock, user data, post lists. Then render.php generates the HTML on every request.
A way to pass data from a parent block to its children: providesContext on the parent, usesContext on the child. Product Template passes the postId of every product.
It adds the classes and styles from supports (colors, spacing, alignment) to the block wrapper. Without it the inspector settings don't reach the markup.