Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Adding open and close div tags in content-single-product.php template

I want to add on templates/content-single-product.php:
An opening <div> before line 39
A close </div> after line 81.

Copying this template to my active theme and make the customization just works fine.

However is there a better way to do this without copying the template?

I thought I had seen some way to apply a filter to do_action( 'woocommerce_before_single_product_summary' ); and do_action( 'woocommerce_after_single_product_summary' ); to do something similar.

I just wanted to check the way I am doing, is the correct way.

Is the way I have chose, the right way?

Thanks.

like image 980
bigdaveygeorge Avatar asked Oct 28 '25 08:10

bigdaveygeorge


2 Answers

In template content-single-product.php, The only way to have your open <div>, is the template herself, as you want it before (line 39):

<div itemscope itemtype="<?php echo woocommerce_get_product_schema(); ?>" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>

Because if you use woocommerce_before_single_product_summary your open <div> is going to be after that existing <div itemscope itemtype="…"> and not before.

For the closing , as you are editing your template, the best way is to add it in your template (just as you made it.

But it should work too in woocommerce_after_single_product hook with any kind of priority (as nothing is already hooked in); this way for example:

function single_product_closing_div(){
    echo '</div>';
}
add_action( 'woocommerce_after_single_product', 'single_product_closing_div', 10, 0 );

Conclusion: You have done it the right way, in the template directly.

like image 149
LoicTheAztec Avatar answered Oct 29 '25 21:10

LoicTheAztec


You can probably do that by adding two hooks for woocommerce_before_single_product_summary and woocommerce_after_single_product_summary so they will look like this

add_action('woocommerce_before_single_product_summary','your_function_name',1); add_action('woocommerce_after_single_product_summary','another_function_name',99); function your_function_name(){echo ""; } function another_function_name(){echo ""; }

like image 38
m1tk00 Avatar answered Oct 29 '25 22:10

m1tk00