I have a problem with a plugin and WooCommerce.
So I have a plugin with an options page, and a custom checkbox on it.
When this checkbox is activated, I want to hide/remove the default WooCommerce related product container.
I can remove this container if I just add this code:
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
But the problem is that I need to call this function from inside another "add_filter" function.
In the moment I have something like this:
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
But when I change the option in the admin-settings, nothing changes. So I think the "init" hook is not right here.
I cant find the right hook to make this work. What hook must I use when I want it to fire when the plugin options get updated?
Thanks in advanced,
Mo
Thanks to Danijel and his answers.
I dont know why I didnt think of it this way.
Maybe this was to much "action" for me on that late evening ;)
I now placed the "add_action" outside of the "add_filter" function and just do the conditional-check there.
This is working:
add_action( 'init', 'hide_related');
function hide_related () {
if ( get_option( 'prfx_active', 'no' ) == 'yes' ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
};
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
...
Im quite certain that WP init action is fired before woocommerce_after_single_product_summary filter, and also if ( $active = 'yes' { ... expression will always be evaluated as true ( use == ). Try with this simple example:
add_action( 'init', function() {
if ( get_option( 'prfx_active', 'no' ) == 'yes' )
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
});
Try moving your add_action_funciton outside of add_filter_function
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
Related
Hi I have faced a problem on my code. I wanted to run add_action for the certain page. add_action is working individually when I Put this on the functions.php but when I tried to run this in wp_head action then its not working.
add_action( 'wp_head', 'remove_my_action', 0);
function remove_my_action($post) {
global $post;
$post_id = $post->ID;
if (512 == $post_id) {
echo "Page Found";
remove_action( 'woocommerce_add_to_cart', 'add_product_to_cart');
}else{
add_action('woocommerce_add_to_cart', 'add_product_to_cart');
}
}
In my functions.php file I have some remove_actions and add_filters that run for woocommerce but the problem is these functions run for all woocommerce product types.
I'd like to wrap a simple if statement around the hooks/filters I have to only run for grouped product pages the problem is I dont know how woocommerce refers to these pages heres what I have at the moment.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
Heres what i'd like to do but I dont know the correct reference for $grouped_product.
if ($grouped_product) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
I have two add_filter and one remove action i'd like to append in my functions.php to only execute on grouped product pages I just need the correct reference in the first set of brackets in the second code block above.
Tried this code but it doesn't work..
if (is_product() and $product->is_type('grouped')) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
functions php file
<?php
function theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
//Remove cart options from under short description.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
//Filter below adds new tab and returns cart options within tab.
add_filter( 'woocommerce_product_tabs', 'woo_paym_product_tab' );
function woo_paym_product_tab( $tabs ) {
// Adds the new tab
$tabs['paym-plans'] = array( 'title' => __( 'Contract Deals', 'woocommerce' ), 'priority' => 10, 'callback' => 'woo_paym_product_tab_content' );
return $tabs;
}
function woo_paym_product_tab_content() {
// The new tab content
woocommerce_template_single_add_to_cart();
}
//Filter below changes tab priority to display price plans first.
add_filter( 'woocommerce_product_tabs', 'sb_woo_move_description_tab', 98);
function sb_woo_move_description_tab($tabs) {
$tabs['paym-plans']['priority'] = 10;
$tabs['description']['priority'] = 20;
$tabs['additional_information']['priority'] = 30;
return $tabs;
}
?>
Solved the problem trick was to change it to a filter.
add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart');
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}
Checking $product->is_type('grouped') will not help here...
Please check if the code provides any help to you..
global $post;
if( $post->post_parent != 0 ){
echo 'is part of a group';
}
I'm switching to a new Wordpress plugin I've written myself that will run along side an old plugin on top of the Woocommerce cart. While I'm running the two plugins I want to remove the old plugin's action that calls it's table to display in the users my account page. I will be adding logic after to work out if it's needed, but for now I just need to remove it.
This is how the action is being called in the old plugin.
class WC_Subscriptions {
public static function init() {
// Display Subscriptions on a User's account page
add_action( 'woocommerce_before_my_account', __CLASS__ . '::get_my_subscriptions_template' );
}
}
WC_Subscriptions::init();
So far in my own plugin I have called the following and none of them work.
remove_action( 'woocommerce_before_my_account', array('WC_Subscriptions', 'get_my_subscriptions_template' ) );
// no error but still shows the table
and the last one
remove_action( 'woocommerce_before_my_account', array( WC_Subscriptions::init(), 'get_my_subscriptions_template' ) );
// Fatal error: Class 'WC_Subscriptions' not found in /var/sites/XXXXX on line 45
I have tried changing/adding the $priority from 1, 9, 10, 11 and 99 and that doesn't work either.
It's frustrating as I'm sure it would work if the old plugin was initiated with a new instance so I could do this
global $my_class;
remove_action( 'woocommerce_before_my_account', array( $my_class, 'get_my_subscriptions_template' ) );
Can anyone help?
This always happens, just worked it out.
I needed to hook the remove_action into another action that is called much later.
In the end I did this
class My_new_plugin {
public function __construct() {
add_action( 'wp_head', array($this, 'remove_action_woosubscription_table' ) );
}
public function remove_action_woosubscription_table() {
remove_action( 'woocommerce_before_my_account', array( 'WC_Subscriptions', 'get_my_subscriptions_template' ) );
}
}
You can set third attribute 'priority' as 999
<?php remove_action( $tag, $function_to_remove, $priority ); ?>
I want to hide the button on my shop pages, but I would like to show it on other posts and pages.
I've found this code to hide the add to cart button on my whole website:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
How can I tweak it, so it only removes the button on woocommerce shop and catagory pages?
You can use the Woocommerce conditional tags to check:
http://docs.woothemes.com/document/conditional-tags/
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
This could also be done with CSS by targeting the relevant classes:
.cart{display:none;}
.avia_cart_buttons{display:none;}
In my case there is that avia because i use Enfold Theme. With inspect element find out your class where the buton is located. and declare it invisible.
Another example is:
.woocommerce .products .shop-column.product-hover-style-2 .product-content
.product-add-to-cart-btn{
display:none !important;
}
add_action('wp','only_add_bierkoerier_in_cart', 'woocommerce_before_cart');
function only_add_bierkoerier_in_cart() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$bierkoerier_in_cart = false;
$categories = get_categories();
if ( has_term( 'bierkoerier', 'product_cat', $cart_item['product_id'] ) ) {
$bierkoerier_in_cart = true;
break;
}
}
if($bierkoerier_in_cart) {
wc_print_notice( 'omdat u bierkoerier producten in uw winkelwagen heeft
kunt u geen winkelitems toevoegen', 'notice' );
if(is_shop() || is_product() || is_product_category()) {
remove_action( 'woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}
To remove the "Add to cart" button
You need to use hook which not affect other code-
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_button', 1 );
function remove_loop_button()
{
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}
this will remove add to cart button from shop/category pages .
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html
Here is a plugin you can use to remove hide disable add to cart button https://wordpress.org/plugins/woo-options/
That's quite simple as i have gone through several tutorials when i was trying to fix it . You have to just put this code in woocommerce.php to hide add to cart button for shop page.
function WpBlog() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
return WooCommerce::instance();
}
Hope that would work for you, if not let me know i will guide you
To remove add to cart buttons from shop, product category, and single product pages, use below steps:
Locate the functions.php in child theme. Child theme prevents changes been overwritten by WP updates.
https://www.dreamhost.com/wordpress/create-woocommerce-child-theme/
Put below code in functions.php:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_before_cart' );
function woocommerce_before_cart() {
if( is_product()) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
}
I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.
function contributor_posts_action() {
if ($role == 'contributor_posts') { // contributor_posts - custom role
// echo 'here'; for testing purposes and WORKS, so it goes under the if condition
add_action( 'admin_menu', 'remove_menus_contrib' );
function remove_menus_contrib(){
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit.php?post_type=directory' );
remove_menu_page( 'edit.php?post_type=city' );
} // this function doesn't get hooked
add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
function remove_admin_bar_items( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-directory' );
$wp_admin_bar->remove_node( 'new-city' );
}// this one works properly. It's for removing for admin bar.
}
}
add_action( 'admin_init', 'contributor_posts_action' );
Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.
Some Wordpress hooks won't work inside other (custom) functions.