Search code examples
phpwordpresswoocommerceproducthook-woocommerce

Add custom content for a specific product on WooCommerce single product pages


In Woocommerce, How can I add a custom content for a specific product on single product pages?

Here is an explicit screen shot: How can i add a content for single product page


Solution

  • As your screenshot is not so clear on where you want this custom content you have 2 options:

    1) Under the product price

    With this custom function hooked in woocommerce_before_single_product_summary action hook you can add some custom content to a specific product ID (to be defined in the function) this way:

    add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
    function add_custom_content_for_specific_product() {
        global $product;
    
        // Limit to a specific product ID only (Set your product ID below )
        if( $product->get_id() != 37 ) return;
    
        // The content start below (with translatables texts)
        ?>
            <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
                <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
                <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
            </div>
        <?php
        // End of content
    }
    

    2) Under the product image:

    With this custom function hooked in woocommerce_before_single_product_summary action hook you can add some custom content to a specific product ID (to be defined in the function) this way:

    add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_for_specific_product', 25 );
    function add_custom_content_for_specific_product() {
        global $product;
    
        // Limit to a specific product ID only (Set your product ID below )
        if( $product->get_id() != 37 ) return;
    
        // The content start below (with translatables texts)
        ?>
            <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
                <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
                <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
            </div>
        <?php
        // End of content
    }
    

    If you want to remove the product short description you can add into the function just after the if statement:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
    

    Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works…