dequeue (remove) woocommerce css stylesheets - php

I am trying to dequeue the woocommerce style sheets from all pages except checkout/cart/receipt pages.
The code form the woocommerce help page (http://docs.woothemes.com/document/disable-the-default-stylesheet/) works to remove the stylesheets entirely.
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
I tried to use this code but it does not work:
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
function child_manage_woocommerce_styles() {
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ));
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
}

It is because you are checking that the page is not all of it in once. You have to separate the if statements like:
if ( ! is_woocommerce()) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
if ( ! is_cart() ) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
if ( is_checkout() ) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
That should fix your problem.

Change && with ||
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() || ! is_cart() || ! is_checkout() ) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}

Deregister/Dequeue styles is best practice
https://codex.wordpress.org/Function_Reference/wp_deregister_style
https://codex.wordpress.org/Function_Reference/wp_dequeue_style
But you can use this filter too, to filter out woocommerce styles with any condition.
add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
}
return false;
});

Related

Disable WooCommerce payment gateway for guests and specific user roles

I have disabled the invoice payment mehtod for one of the user roles in my site ('customer') but now I need to add another user role ('business') to this rule and I can't figure out how to make it work. When I add the second role, the code stops working altogether and it ends up showing the gateway to all users.
Here's the code I'm using to disable the gateway:
I'm not very experienced with PHP so any help will be tremendously appreciated.
If there's any chance you can correct my code to fit the use case, I would be very grateful.
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
$user = wp_get_current_user();
if ( isset( $available_gateways['igfw_invoice_gateway'] ) && !is_user_logged_in() || isset( $available_gateways['igfw_invoice_gateway'] ) && in_array('customer', $user->roles) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
Thoughts?
There is a mistake in your if statement (also you can use current_user_can() function for user roles) like:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
if ( ( ! is_user_logged_in() || current_user_can('customer') || current_user_can('business') )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
or with the global $current_user; and array_intersect() function:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
global $current_user;
// Here define your user roles
$user_roles = array( 'customer', 'business' );
if ( ( ! is_user_logged_in() || array_intersect( $current_user->roles, $user_roles ) )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
It should better work now.

Remove WooCommerce noindex meta in myaccount page

Here is a solution to remove the meta "noindex" which causes an issue for the myaccount page to be indexed in google, because some people want it to appear for their clients to easy find the login page.
The function match the my-account page and then remove the meta
function remove_wc_page_noindex(){
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( false !== strpos( $url, 'my-account' ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'init', 'remove_wc_page_noindex' );
My question: is there is a way to directly locate the my account page instead of matching part of the url?
You can get more details about the conditional tags here.
/**
* Disable/Enable search engines indexing myaccount pages.
*
*/
function is_wc_page_noindex() {
if ( is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'template_redirect', 'is_wc_page_noindex' );
Since WP 5.7, Woocommerce uses the wp_robots filter. If remove_action( 'wp_head', 'wc_page_noindex' ) did not work for you, then you can try the following:
// Remove WooCommerce noindex meta in cart, checkout and myaccount pages
add_action( 'template_redirect', 'srj_woo_remove_noindex' );
function srj_woo_remove_noindex() {
if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_filter( 'wp_robots', 'wc_page_no_robots', 10 );
}
}

Clear Woocommerce Cart on Page Load Even for logged in users

I want to clear the cart page on page load if this page is not a cart or a checkout page Even for logged in users and admins, any page then it clears. This code was working but its not anymore
/**
 * Clears WC Cart on Page Load
 * (Only when not on cart/checkout page)
 */
 
add_action( 'wp_head', 'bryce_clear_cart' );
function bryce_clear_cart() {
if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
return;
}
WC()->cart->empty_cart( true );
}
Updated and enhanced.
Use Woocommerce conditional tags and try template_redirect hook instead (when cart is not empty):
add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
if ( ! ( is_cart() || is_checkout() ) && ! WC()->cart->is_empty() ) {
WC()->cart->empty_cart( true );
}
Code goes on function.php file of your active child theme (or active theme). It should work.
This works fine, as i said in the comment your code doesn't work i don't know why i just edited it like that it worked but i don't know if it now works fine as the condition to work only executed if cart is not empty on all other pages than cart and checkout)
add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
if ( ( ! ( is_cart() || is_checkout() ) ) && ! ( WC()->cart->is_empty() ) ) {
WC()->cart->empty_cart( true );
}
}
I think there is an issue with this ! ( WC()->cart->is_empty() )
I think this function maybe doesn't work with the operator "!", my code executes the same as yours but i don't know why yours no

WooCommerce does not redirect to internal link after purchase

I want to redirect to custom page after purchase on woocommerce code below:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_page_by_title( About )->ID );
exit;
}
}
It redirects to 'order-received' page which does not exist.
You have forgot th little "" around the title and you should need to use get_permalink() function too this way:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( get_permalink( get_page_by_title( "About" )->ID ) );
exit;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I have tested it and this should work now

Woocommerce exclude products by different users

I have differents shopmanager in my wocommerce, but for now i want that 1 shopmanager can see only all products created from him/her not all. For now they can only modify these products but i want hide different products from different shopmanager..
add_action( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! ( current_user_can( 'edit_product' ) ) && is_shop() ) {
$q->query_vars['author'] = get_current_user_id();
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Try this snippet

Categories