Search code examples
phpwordpresswoocommerceproductwordpress-hook

Custom content issue in Woocommerce single product pages


I'm trying to add some informations on description.php page from Woocommerce, but I can't see the result.

For a specific product, I try to add specific information. I've done this way :

<?php

function my_function_name() {
    global $product;

    if ( $product->get_id() !== 96 ) {
        return;
    }

    echo ('Information I want to display');
}
add_action( 'woocommerce_after_single_product_summary', 'my_function_name', 10);
?>

Once I go back to my product page, I can't see the result, nothing happens. I'm not sure where I've failed.

Any help is appreciated.


Solution

  • Updated: The problem is in the hook priority. Priority 10 is already used by product tabs:

    <?php
        /**
         * woocommerce_after_single_product_summary hook.
         *
         * @hooked woocommerce_output_product_data_tabs - 10
         * @hooked woocommerce_upsell_display - 15
         * @hooked woocommerce_output_related_products - 20
         */
        do_action( 'woocommerce_after_single_product_summary' );
    ?>
    

    So if you need:

    • To display that before the product tabs, the priority needs to be between 1 and 9
    • To display that after the product tabs, the priority needs to be between 11 and 19

    So this example will display it before the product tabs (priority is 5 below):

    add_action( 'woocommerce_after_single_product_summary', 'my_function_name', 5 );
    function my_function_name() {
        global $product;
    
        if ( $product->get_id() != 96 )
            return;
    
        echo __( 'Information I want to display', 'woocommerce');
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    It should work