Custom Shortcode WooCommerce
August 7, 2025 · AI Conversations
User
add custom shortcode before relaated product in wocomerce single page by hook
Assistant
To add a **custom shortcode before the Related Products** section on a WooCommerce single product page, you can hook into `woocommerce_after_single_product_summary`, which is where related products are usually rendered.
Here’s how to do it:
—
### ✅ Step-by-Step Code
Add the following code to your theme’s `functions.php` file or a custom plugin:
// Register your custom shortcode
function my_custom_woo_shortcode() {
return '<div class="my-custom-shortcode">This is my custom content or shortcode output</div>';
}
add_shortcode('my_custom_shortcode', 'my_custom_woo_shortcode');
// Display the shortcode before related products
function insert_shortcode_before_related_products() {
if (is_product()) {
echo do_shortcode('[my_custom_shortcode]');
}
}
add_action('woocommerce_after_single_product_summary', 'insert_shortcode_before_related_products', 19);
—
### 📌 Notes:
– `woocommerce_after_single_product_summary` is the main hook that contains tabs, upsell, and related products.
– Related products are added at **priority 20**, so we hook into it at **priority 19** to show content *before*.
– You can replace the `[my_custom_shortcode]` with any shortcode you’ve defined.
—
Let me know if you want to show this only for specific product categories or customize the layout further.