This will probably open a door for me as there's something I'm missing so a WooCommerce 101 please;
part of the WooCommerce template archive-product.php contains the code;
<?php
/**
* woocommerce_before_shop_loop hook.
*
* #hooked woocommerce_result_count - 20
* #hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>
From this, and reading the documentation, it implies that this;
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
should remove the result count from the product categories returned. Only it doesn't.
What's wrong?
I was going to say you should read the documentation but it is leaving an important part out.
remove_action() cannot be called directly and must, itself, be added to an action hook. The action hook needs to come before the action being removed. In this case I would just use the same hook, but an earlier priority (default is 10, I've used 1)
function so_38878702_remove_hook(){
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
add_action( 'woocommerce_before_shop_loop', 'so_38878702_remove_hook', 1 );
To remove result count action from Woocommerce -
add_action('woocommerce_before_shop_loop', 'remove_result_count' );
function remove_result_count()
{
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
}
use this code in your function.php file.
Important: To remove a hook, the $function_to_remove and $priority
arguments must match when the hook was added. This goes for both
filters and actions. No warning will be given on removal failure.
http://codex.wordpress.org/Function_Reference/remove_action
Here you can get WooCommerce Action and Filter Hook -
https://docs.woothemes.com/wc-apidocs/hook-docs.html
Related
I'm desperately trying to remove an action while the cart calculates the total.
Here is my code:
remove_action('woocommerce_cart_calculate_fees', array('WCS_Cart_Renewal', 'remove_non_recurring_fees'), 1000);
While the original action hook is taking place on the WooCommerce Subscriptions plugin:
// Remove non-recurring fees from renewal carts. Hooked in late (priority 1000), to ensure we handle all fees added by third-parties.
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'remove_non_recurring_fees' ), 1000 );
Unfortunately I could not remove the remove_non_recurring_fees hooked function.
Any idea why?
When you Look at WCS_Cart_Renewal Class and remove_non_recurring_fees() function, you will see that this function removes all fees first and re-add only recurring fees, when a subscription is involved. This function is hooked with a priority of 1000.
Instead of trying to remove the action hook that trigger this function, you have 2 other choices:
1). For custom fees added by you via your theme's functions.php file:
You will have just to use a greater priority like in this following example:
add_action( 'woocommerce_cart_calculate_fees', 'my_custom_fee', 2000 );
function my_custom_fee( $cart ) {
// Your code
}
2). Or better using woocommerce_subscriptions_is_recurring_fee available filter hook:
This filter hook that allows to re-add all desired fees that are not recurring with this simple code line:
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I have a plugin that uses woocommerce_cart_updated hook to do something when the cart contents change.
woocommerce_cart_updated seems to be fired on every page load.
Is there any way to restrict this hook in some pages like
my-account.?
Is there any single hook that I can call on any cart updated ( add , remove / update quantity/restore).?
By using different hooks - there is some workaround for this.
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this );
do_action( 'woocommerce_cart_item_restored', $cart_item_key, $this );
do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity );
Is there any option to get the pagename or slug in the callback
function of this hook. ( woocommerce_cart_updated ). So I can restrict this action in some pages.?
But as far as I can tell — it's being run even when the session variables haven't been updated. It's being fired all the time, no matter what.
In WooCommerce, there is a public static class WC_Emails, defined in /woocommerce/includes/class-wc-emails.php.
Within that class, there is a reference to a hook called woocommerce_email_customer_details. To that hook, an action called email_addresses is assigned, which is simply a PHP file that generates some HTML for billing and shipping addresses to be added to email notifications.
add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
I am trying to remove that action and can't seem to figure out how to do it.
Here's an example bit of code in my child theme's functions.php that does not work:
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details',
array( 'WC_Emails', 'email_addresses' ), 20);
}
Instead of the init hook there, I have tried wp-head. Since WC_Emails is a static function, the above code is the way the WordPress Codex suggests, as far as using an array() above, instead of just the action name. Regardless, this doesn't work, either (with just the action name):
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details', 'email_addresses', 20);
}
20 is the priority on the add_action Woo does, and I also understand that a remove_action() must have the same priority as what the original add_action() had.
No matter what I try, the billing and shipping addresses still appear on the email notifications.
Once that gets working, it will be great! However, there is an additional requirement: This needs to happen only inside code I have for the woocommerce_order_status_pending_to_processing_notification hook in Woo.
In other words, I have other code that fires and generates an email when an order status changes from Pending to Processing (and I have tried my code above with all its variations within that hook as well). I need the billing and shipping addresses to NOT show only in the custom email that is generated.
Any ideas? Thanks so much. :)
Here is the code:
add_action( 'woocommerce_email', function ( $email_class ) {
remove_action( 'woocommerce_email_customer_details', array( $email_class, 'email_addresses' ), 20, 3 );
});
Source
I have started to learn how to create templates with WooCommerce and I had faced with a little problem. For instance, in the php file content-single-product.php of Woocommerce plugin I have strings like that:
<?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' );
?>
And for example, when I want to edit this (delete some fields and change the structure) I try erase the string:
do_action( 'woocommerce_single_product_summary' );
and after that write like this:
<?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' );
do_action('woocommerce_template_single_title');
?>
Could you tell me please why this doesn't work?
What is the right way to edit like that?
Thanks
First in reference below you will find how to override properly woocommerce templates via a theme (avoiding editing the plugin templates).
In your first code snippet, as you can see for woocommerce_single_product_summary hook, you have in order all the different templates that are #hooked in this hook location with do_action() WordPress function:
do_action( 'woocommerce_single_product_summary' );
So in your customized code (the 2nd code snippet) you have just replaced the hook, by the hooked template slug (that is NOT a hook) and will NOT work as an entry point action hook. See the references at the bottom of this answer for the list of WooCommerce actions and filters existing hooks…
Consequences: All other hooked templates in the commented list code (beginning with #hooked) will be missing if you replace a hook by a template slug.
For the hooks used in the templates see this helpful WooCommerce Visual Hook Guide
Explanations (How to):
HOW TO - Concrete example:
You want to customize woocommerce_template_single_title hooked template in woocommerce_single_product_summary hook.
THE HOOK NAME: woocommerce_single_product_summary hook.
THE TEMPLATES HOOKED (+priority order number) => corresponding template file name:
— woocommerce_template_single_title (5) => single-product/title.php
— woocommerce_template_single_rating (10) => single-product/rating.php
— woocommerce_template_single_price (10) => single-product/price.php
— woocommerce_template_single_excerpt (20) => single-product/short-description.php
— woocommerce_template_single_add_to_cart(30) => single-product/add-to-cart/ (6 files depending on product type)
— woocommerce_template_single_meta (40) => single-product/review-meta.php
— woocommerce_template_single_sharing - (50) => single-product/share.php
Then you will need to edit the corresponding woocommerce_single_product_summary hook title.php located in single-product (sub folder)… Finally is not so complicated, once we understand the template structure files and the hooks in that templates.
The priority number, gives the order for the hooked templates: Smaller in first, bigger at the end…
See also: Hooks and their hooked functions execution queue in Wordpress and Woocommerce
Others ways:
You can also use all that existing templates hooks to target very specific changes or customizations, with custom functions located in the function.php file of your active child theme (or theme) or any plugin file too.
Example using add_action() WordPress function:
// define the woocommerce_single_product_summary callback function
function my_custom_action() {
echo '<p>This is my custom action function</p>';
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
This function has a priority number of 15 and will display
"This is my custom action function" string text, between the product price and the product short description…
Optional arguments of this hooked function for this hook:
• The template slug (string).
• The priority (int).
References:
Template structure & Overriding templates via a theme
WooCommerce Hooks: Actions and filters
WooCommerce Code - Action and Filter Hook Reference
WooCommerce Visual Hook Guide: Single Product Page
I'm new to WooCommerce and Storefront theme. I am trying to get a sense of the source code before I start modifying it. I'm just having a little difficulty finding out where all the necessary codes are located.
When I open the header.php, I got lost because every functions were hooked to some other files like this.
do_action( 'storefront_before_header' );
Where are these functions defined in Storefront theme? and how can I find where all these do_action functions are defined in the future other than just opening all the files are searching for the strings?
I've looked into files such as:
storefront-functions.php
storefront-template-functions.php
storefront-template-hooks.php
functions.php
For all woocommerce-related products, there's a #hooked tag in phpdoc block before each hook. If there're no #hooked tags, that hook is just a reserved hook which may be used in the future.
Let's see the storefront_header hook:
/**
* Functions hooked into storefront_header action
*
* #hooked storefront_skip_links - 0
* #hooked storefront_social_icons - 10
* #hooked storefront_site_branding - 20
* #hooked storefront_secondary_navigation - 30
* #hooked storefront_product_search - 40
* #hooked storefront_primary_navigation_wrapper - 42
* #hooked storefront_primary_navigation - 50
* #hooked storefront_header_cart - 60
* #hooked storefront_primary_navigation_wrapper_close - 68
*/
do_action( 'storefront_header' );
After the #hooked tag is a function name and priority in which the function is executed when the action is fired. Lower numbers correspond with earlier execution.
Most of the functions hooked to the hook is located inside storefront-template-functions.php and added inside storefront-template-hooks.php.
You can find those functions with simple IDE searchs inside theme folder.