Edit product hook WooCommerce - php

On my product template, I execute this action :
<?php
/**
* woocommerce_single_product_summary hook.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
*/
do_action('woocommerce_single_product_summary');
?>
What I want is to edit woocommerce_template_single_excerpt so I wrote this code in functions.php :
add_action('woocommerce_template_single_excerpt', 'woocommerce_custom_single_excerpt', 20);
function woocommerce_custom_single_excerpt() {
global $post;
echo '<p class="product-description">';
echo $post->post_excerpt;
echo '</p>';
}
But it does not override at all.
Thanks for your help!

Please try this code snippet.
add_action('woocommerce_single_product_summary', 'woocommerce_custom_single_excerpt', 15);
function woocommerce_custom_single_excerpt() {
global $post;
echo '<p class="product-description">';
echo "hello";
echo '</p>';
}

Related

Woocommerce - How to put the short description beneath the product summary?

Using this as a visual reference for the hooks. I want to place the short summary at woocommerce_after_single_product_summary. I thought I could do that by simply doing something like this:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt',30);
This kind of works, but this places the short summary sort of on the product summary which messes up the whole layout. I've tried playing with priorities but nothing changes. I've also looked up the theme template file, but I can't figure out how to fix it. The theme template file:
<?php
defined( 'ABSPATH' ) || exit;
global $product;
/**
* Hook: woocommerce_before_single_product.
*
* #hooked wc_print_notices - 10
*/
do_action( 'woocommerce_before_single_product' );
if ( post_password_required() ) {
echo get_the_password_form(); // WPCS: XSS ok.
return;
}
?>
<div id="product-<?php the_ID(); ?>" <?php wc_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_single_product_summary.
*
* #hooked woocommerce_show_product_sale_flash - 10
* #hooked woocommerce_show_product_images - 20
*/
do_action( 'woocommerce_before_single_product_summary' );
?>
<div class="summary entry-summary ld-product-summary">
<?php
/**
* Hook: woocommerce_single_product_summary.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
* #hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
?>
</div>
<?php
/**
* Hook: woocommerce_after_single_product_summary.
*
* #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' );
?>
</div>
<?php do_action( 'woocommerce_after_single_product' ); ?>
<?php
add_action('woocommerce_after_single_product_summary', 'show_short_description', 25);
function show_short_description() {
global $post;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if( !empty($short_description) ) {
echo $short_description;
}
}

I am doing remove_action before do_action but action is still fired?

I don't understand why this doesn't work. My understanding is that you must remove the action after it is added but before it is fired. That is what I'm doing here? Is there something here that I'm not understanding? Debugging wordpress really confuses me because you can't really step through it?
<?php
/**
* Functions hooked into storefront_header action
*
* #hooked storefront_header_container - 0
* #hooked storefront_skip_links - 5
* #hooked storefront_social_icons - 10
* #hooked storefront_site_branding - 20
* #hooked storefront_secondary_navigation - 30
* #hooked storefront_product_search - 40
* #hooked storefront_header_container_close - 41
* #hooked storefront_primary_navigation_wrapper - 42
* #hooked storefront_primary_navigation - 50
* #hooked storefront_header_cart - 60
* #hooked storefront_primary_navigation_wrapper_close - 68
*/
remove_action('storefront_header', 'storefront_product_search', 40);
do_action( 'storefront_header' );
?>
Please see the contributed notes from the Wordpress documentation for remove_action:
If an action has been added from within a class, for example by a plugin, removing it will require accessing the class through a variable that holds the class instance.
...
To remove an action, the priority must match the priority with the function that was originally added.

Relocating product link in WooCommerce archives pages

The link I am trying to relocate is the link that wraps each product on the shop page which takes you to that products own page. As it is right now it wraps the image, product name, product sku and price. I only want it to wrap the image and product name.
Here's the code which is creating each product.
<li <?php post_class( $boot_classes ); ?>>
<?php
/**
* woocommerce_before_shop_loop_item hook.
*
* #hooked woocommerce_template_loop_product_link_open - 10
*/
do_action( 'woocommerce_before_shop_loop_item' );
/**
* woocommerce_before_shop_loop_item_title hook.
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* woocommerce_shop_loop_item_title hook.
*
* #hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* woocommerce_after_shop_loop_item_title hook.
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* woocommerce_after_shop_loop_item hook.
*
* #hooked woocommerce_template_loop_product_link_close - 5
* #hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
How is this possible?
You can change the location of the product link close as you expect, this way:
add_action('init', 'change_location_of_loop_product_link_close' );
function change_location_of_loop_product_link_close(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
add_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 20 );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.

How to remove title from woocommerce_single_product_summary hook?

On my product template, I execute this action :
<?php
/**
* woocommerce_single_product_summary hook.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
*/
do_action('woocommerce_single_product_summary');
?>
What I want is do remove the woocommerce_template_single_title hook so in my functions.php file I wrote this code :
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 1);
But it does not work and I don't know how.
Thanks for your help
!! EDIT !!
Nevermind I solved it by writing this:
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
The priority have to match
The author has added the one-liner to his question; as suggested by #Bugs, here it is as an answer.
// Remove title hook Woocommerce
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);

Display product price under the product image

In WooCommerce, I would like to display product varation price under image. I am using some code in my functions.php but not working please any help will be appreciated. Thanks
add_action( 'woocommerce_product_thumbnails','woocommerce_single_variation',30 );
For that you need to:
Remove the woocommerce_template_single_price in woocommerce_single_product_summary hook:
/**
* woocommerce_single_product_summary hook.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
*/
To add it in woocommerce_before_single_product_summary hook:
/**
* woocommerce_before_single_product_summary hook.
*
* #hooked woocommerce_show_product_sale_flash - 10
* #hooked woocommerce_show_product_images - 20
*/
So your code will be:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_product_thumbnails','woocommerce_template_single_price', 30 );
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories