Why is WooCommerce function not overriding the default function in child theme - php

I have a child theme that I am trying to override a WooCommerce function.
WooCommerce file in plugin location: woocommerce/includes/wc-template-hooks.php. I created a new file in the same hierarchy in my child theme and it's located here: my-child-theme/woocommerce/includes/wc-template-hooks.php
My edited section is I commented out the product title in child theme:
/**
* Product Summary Box.
*
* #see woocommerce_template_single_title()
* #see woocommerce_template_single_rating()
* #see woocommerce_template_single_price()
* #see woocommerce_template_single_excerpt()
* #see woocommerce_template_single_meta()
* #see woocommerce_template_single_sharing()
*/
//add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); //commented
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
However, when I goto my product page, I still see the title.
Any idea what I am doing wrong?

You need to remove action woocommerce_single_product_summary to this work properly.
add_action( 'woocommerce_before_single_product', 'remove_woocommerce_single_product_summary', 10 );
function remove_woocommerce_single_product_summary() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
Just add this to your functions.php and will do the magic.
You also can try to add remove_action() out of the add_action(). Just like this...
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 15 );
...directly to functions.php
Play with it.
TIPS & TRICKS
If you want to change your HTML in the title or do anything you want, here is example:
add_action( 'woocommerce_before_single_product', 'remove_woocommerce_single_product_summary', 10 );
function remove_woocommerce_single_product_summary() {
// First, we remove it
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
// Second, we add new title
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_new_title', 1 );
}
function woocommerce_my_new_title(){
echo '<h1 class="some-my-custom-classes">' . the_title() . '</h1>';
}
If your active theme does not support Woocommerce and you want to add
a Woocommerce template to your active theme, you must register
WooCommerce, or give Woocommerce features.
Check out these links for more details:
https://github.com/woocommerce/woocommerce/wiki/Declaring-WooCommerce-support-in-themes
https://docs.woocommerce.com/document/template-structure/
One important thing:
If you change the template folder within the Woocommerce plugin, there is a good chance that you will lose your settings after the plugin update. That is why it is better to work in an active theme.

function my_custom_action() {
echo '<p>Hello here</p>';
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );

Related

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_gzd_template_single_shipping_costs_info', 7 ) does not work

Unfortunately, despite remove_action on the desired. page plus shipping costs. Does anyone know what this can do?
This is shown on my website. I want to remove it with php.
<p class="wc-gzd-additional-info">
<span class="wc-gzd-additional-info tax-info">inkl. 19 % MwSt.</span>
</p>
I already removed the title with
//remove product title only //This worked
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
Now I am trying to do the same but it does not work
<?php
/**
* WooCommerce Custom Hook
* woocommerce-hooks.php
*/
//remove product title only //This worked
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_shipping_costs_info', 7 )
// include plugin functions
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

Remove WooCommerce messages from account content

I'm currently trying to remove all messages from the WooCommerce account with the following code line:
remove_action( 'woocommerce_account_content', 'woocommerce_output_all_notices', 10 );
Sadly, the wrapper for messages is still there:
<div class="woocommerce-notices-wrapper">lol</div>
I've added a lol to the function that displays the wrapper and it's the correct function I'm trying to remove. No idea why it's not working...
If your active theme uses the default woocommerce templates and hooks for myaccount pages, then add the follows code snippet to achieve the above -
function modify_wc_hooks() {
// remove all wc my account's notices wrapper
remove_action( 'woocommerce_account_content', 'woocommerce_output_all_notices', 5 );
remove_action( 'woocommerce_before_customer_login_form', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_before_lost_password_form', 'woocommerce_output_all_notices', 10 );
remove_action( 'before_woocommerce_pay', 'woocommerce_output_all_notices', 10 );
remove_action( 'woocommerce_before_reset_password_form', 'woocommerce_output_all_notices', 10 );
}
add_action( 'init', 'modify_wc_hooks', 99 );
Codes goes to your active theme's functions.php

Change Woocommerce single product templates order based on product tag

I have two templates, one for Panorama & Multi-Panel photographs, the other for Normal ones.
I'm trying to code a conditional statement that checks the product tag in order to select the relevant template to be displayed.
Panorama photos have the product tag 'panorama' or 'multi-panel'.
If the 'panorama' or 'multi-panel' tag is true >> perform some remove_action and add_action functionality on the Panorama product page...
...otherwise do nothing to the Normal product page...
This is what I have been dappling with to no affect!!
function robhemphill_panorama_layout () {
if ( has_tag( array( 'panorama', 'multi-panel'))) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
} else {
// Do nothing
}
}
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout' );
Any thoughts or ideas would be great, can't find anything online!
As you are targeting Woocommerce product tags (but not WordPress tags) try this instead:
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout', 1 );
function robhemphill_panorama_layout () {
global $product;
if ( has_term( array( 'panorama', 'multi-panel' ), 'product_tag', $product->get_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
}
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.

Remove Variations from "Cart Form"

I am trying to move add to cart button under button(cart form) under the product image and leave variations where they are.
But with below hook variations are also going under the product image
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
Any way to make only button move under the image and not variations form.
Here is the screenshot:
Thank You
Tried following solution but no Luck
Try 1:
remove_action( 'woocommerce_single_variation', 'remove_variation', 10 );
function remove_variation(){
woocommerce_single_variation();
}
Try 2:
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
Default Actions:
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
Remove Default Actions:
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
Add Custom Action:
add_action( 'woocommerce_single_variation', 'new_custom_action', 5 );
function new_custom_action() {
echo 'TEST';
}
If you look at the default variable template in woocommerce/templates/single-product/add-to-cart/variable.php you can see that for variable products, it combines both the variations drop-downs and the add to cart button together in the 'woocommerce_single_variation' hook:
/**
* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
You can either customise this file within your theme to change this behaviour, or remove the woocommerce_single_variation function from this hook and add it somewhere else.

Edit WooCommerce Shop page, Title before thumbnail

I've bitten off more than I can chew and need some help with WooCommerce.
I'm trying to edit the shop front page (i've managed to do the product single pages fine) but can't find the out put for the hooks anywhere.
Basically, all i'm trying to do is make the Title of the product appear before the thumbnail and add a"view" button after the thumbnail.
I'm literally pulling my hair out so if anyone could help i'd be extremely grateful!
Add below on function.php
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 90 );
Adding above code will duplicate you thumbnail, then add below CSS will do trick.
.product-images { display: none; }
You should to see in plugin/woocommerce/woocommerce-hook.php
Form Line 100 - 107
/**
* Before Single Products Summary Div
*
* #see woocommerce_show_product_images()
* #see woocommerce_show_product_thumbnails()
*/
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
/**
Form Line 120 - 132
/**
* Product Summary Box
*
* #see woocommerce_template_single_title()
* #see woocommerce_template_single_price()
* #see woocommerce_template_single_excerpt()
* #see woocommerce_template_single_meta()
* #see woocommerce_template_single_sharing()
*/
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
//add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
/**
Change order of add_action
Change value of the second arg
Good luck!

Categories