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
Related
I need to use the conditional tag is_product() from the WooCommerce plugin so my custom plugin only loads in the script if the user is at a product page on the front-end.
Additionally, I don't want the plugin to break if WooCommerce is not installed on the website.
My current code:
if ( !is_admin() && is_product() ) {
function add_script() {
wp_enqueue_script( 'main', plugin_dir_url( __FILE__ ) . '/js/main.js' );
}
}
add_action( 'wp_enqueue_scripts', 'add_script' );
This doesn't work at the moment since it can't find the conditional tag is_product().
Any advice?
"I don't want the plugin to break if Woocommerce is not installed on the website." Your main plugin file should start with:
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
// Check if WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) exit; // Exit if WC not active
Loads the script if the user is at a product page, use:
function add_script() {
// is_admin() - Determines whether the current request is for an administrative interface page.
// is_product() - Returns true on a single product page.
if ( !is_admin() && is_product() ) {
wp_enqueue_script( 'main', plugin_dir_url( __FILE__ ) . '/js/main.js' );
}
}
add_action( 'wp_enqueue_scripts', 'add_script' );
I am working on wordpress woocommerce site. I want to set logic user can not see product category or subcategory or product page without login,it will redirected to login page.
My code below
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && ( is_woocommerce() || is_shop() || is_cart() || is_checkout() ) ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')) );
exit();
}
}
But after logged in when go to product category page, it will redirected to login page. I also checked it by debug, product category page showing not logged in.I can not understand after logged in when go to product category page, why showing not logged in?
Please help me with possible solutions.
Thanks..
Maybe the problem is related to the woocommerce_myaccount_page_id option.Try to get it via slug (in case the page slug is "my-account"):
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && ( is_woocommerce() || is_shop() || is_cart() || is_checkout() ) ) {
wp_redirect( home_url( '/my-account/' ) );
exit();
}
}
or via the id of the My Account page:
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && ( is_woocommerce() || is_shop() || is_cart() || is_checkout() ) ) {
$my_account_page_id = 2;
wp_redirect( get_page_link( $my_account_page_id ) );
exit();
}
}
The code has been tested and works. Add it to your active theme's functions.php.
After Allow guest checkout for specific products only in WooCommerce answer to my previous question, the following code redirect users to login page:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
if( is_checkout() && !is_user_logged_in()){
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
But I have some products which allows guest checkout (see the linked question/answer above). So how could I fix my code for the products which allows guest checkout to disable that code redirection?
You can replace my previous answer code with the following:
// Custom conditional function that checks if checkout registration is required
function is_checkout_registration_required() {
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// Check if there is any item in cart that has not the option "Guest checkout allowed"
if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) !== 'yes' ) {
return true; // Found: Force checkout user registration and exit
}
}
}
return false;
}
add_filter( 'woocommerce_checkout_registration_required', 'change_tax_class_user_role', 900 );
function change_tax_class_user_role( $registration_required ) {
return is_checkout_registration_required();
}
Then your current question code will be instead:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
if( is_checkout() && !is_user_logged_in() && is_checkout_registration_required() ){
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
This is the code I am using:
if (!is_admin()):
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
//add_action('woocommerce_before_cart_table', 'apply_matched_coupons');
//add_action('woocommerce_before_checkout_form', 'apply_matched_coupons');
function apply_matched_coupons() {
global $woocommerce;
$coupon_code = 'somecodehere';
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
if ( $woocommerce->cart->cart_contents_total >= 1 ) {
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
}
endif;
The issue I am having is that when I go to the checkout page that the coupon still gets applied. It's not applied on the cart which is the desired result but I don't want it applied at all in this condition.
Any help?
Based on your explanation, it sounds like you should be using the woocommerce_add_to_cart hook, which is run when a product is successfully added to the cart. I also don't think you should be using is_admin(), since that just checks if you're on an admin page...not if the current user is an admin.
I would do something like the following:
add_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
// If the current user is a shop admin
if ( current_user_can( 'manage_woocommerce' ) ) return;
// If the user is on the cart or checkout page
if ( is_cart() || is_checkout() ) return;
$coupon_code = 'somecodehere';
if ( WC()->cart->has_discount( $coupon_code ) ) return;
WC()->cart->add_discount( $coupon_code );
}
In WooCommerce, I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page. Can anyone have the solution ?
Here is the code I have tried, but it does not work:
function my_empty_cart() {
global $woocommerce;
if (isset( $_GET['empty-cart'] ) ) {
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'product' ) ) );
}
}
add_action( 'init', 'my_empty_cart' );
// old woocommerce : use sizeof( $woocommerce->cart->cart_contents) to check cart content count
// In new woocommerce 2.1+ : WC()->cart->cart_contents_count to check cart content count
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
}
}
init hook will run everytime. use template_redirect
==============Updates=============
In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.
WC()->cart->cart_contents_count
2021 Update
Since WooCommerce version 3 use the following to redirect to shop page when trying to access cart page and cart is already empty:
add_action( 'template_redirect', 'empty_cart_redirect' );
function empty_cart_redirect(){
if( is_cart() && WC()->cart->is_empty() ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Notes - Obsolete and deprecated code:
global $woocommerce; use with $woocommerce->cart is replaced simply by WC()->cart
sizeof($woocommerce->cart->cart_contents) == 0 is replaced by simple WC()->cart->is_empty()
woocommerce_get_page_id() is replaced by wc_get_page_id()
When removing all cart items on cart page, to make the redirection active, some additional jQuery code is required, see: Redirect to shop if cart is emptied on cart page in WooCommerce 3+
Just tested this myself as I needed something similar.
function cart_empty_redirect_to_shop() {
global $woocommerce;
if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
}
}
add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
I tried #Pushpak's solution, but it doesn't work anymore. To check the cart content please use this code:
global $woocommerce;
if ( $woocommerce->cart->cart_contents_count != 0 ) {
// cart has content
} else {
// cart is empty
}
2018 - 25 - Sept
working for me today::
function cart_empty_redirect_to_shop() {
global $woocommerce, $woocommerce_errors;
if ( is_cart() && sizeof($woocommerce->cart->cart_contents) == 0) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
add_action( 'template_redirect', 'cart_empty_redirect_to_shop' );
Simply go to woocommerce folder and navigate to the CART folder.. in there is a cart-empty.php
then locate the following:
<p><a class="button1 btn btn-normal" href="<?php echo get_permalink.....
it would say getpermalink(woocommerce_.....
simply change that to
<p><a class="button1 btn btn-normal" href="<?php echo get_permalink('THE PAGE ID YOU WANT TO REDIRECT TO');
and bobs your uncle.. it will redirect to page with id you specify.
Let me know if you dont understand.