Determine whether on shop page in woocommerce - php

My client wants the checkout to be "streamlined" to skip the cart page and go straight to checkout one product at a time. I got that covered but I also need to automatically empty the cart if the customer decides not to confirm checkout.
For this I wanted to check whether or not I'm on any other page than cart or checkout and do it there but all the commands I tried (is_shop(), is_front_page(), is_page('Shop'), is_product(), is_home()) always return false so I'm not sure what to do about it. This is how I'm trying to do it (in my themses functions.php):
function reset_cart_front() {
global $woocommerce;
echo "Attempting to empty<br>";
if (is_shop()) {
echo "is right page<br>";
$woocommerce->cart->empty_cart();
} else {
echo "is not right<br>";
}
}
add_action( 'init', 'reset_cart_front' );
what gives?

Nevermind, I figure it out!
function reset_cart_shop_loop() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action( 'woocommerce_before_shop_loop', 'reset_cart_shop_loop' );

Correct me if I'm wrong...but I think you should check if the current page is_checkout(), and empty the cart if it's not:
function reset_cart_front() {
global $woocommerce;
if ( !is_checkout() ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'template_redirect', 'reset_cart_front' );
I also think 'init' is too early to hook (and would recommend trying 'template_redirect').

Related

remove_submenu_page('woocommerce', 'wc-admin'); function not removing dashboard

I have created a new role in WordPress for WooCommerce. The only thing I require this role to see is:
WooCommerce --> Orders Only
Users - All Submenus
WP All Import - All Submenus except for Settings
WP All Export - All Submenus except for Settings
I successfully dwindled down to the list but am having issues isolating the WooCommerce Dashboard sub-menu.
I used this code to get a full list:
if (!function_exists('debug_admin_menus')):
function debug_admin_menus() {
if ( !is_admin())
return;
global $submenu, $menu, $pagenow;
if ( current_user_can('manage_options') ) { // ONLY DO THIS FOR ADMIN
if( $pagenow == 'index.php' ) { // PRINTS ON DASHBOARD
echo '<pre>'; print_r( $menu ); echo '</pre>'; // TOP LEVEL MENUS
echo '<pre>'; print_r( $submenu ); echo '</pre>'; // SUBMENUS
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
endif;
I found that component being: wc-admin where I added:
remove_submenu_page('woocommerce', 'wc-admin');
BUT it isn't removing it. Does anyone know why? I've also tried:
add_action( 'admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if($user_role == "custom_shop_admin") {
$remove_submenu = remove_submenu_page('woocommerce', 'wc-admin');
}
}
Everywhere I've looked up has ('woocommerce', 'wc-admin') as the dashboard. Not sure if I can't remove it, to redirect it to the orders page perhaps for that role only. I know there has been a recent WooCommerce Update so not sure if that has anything to do with it.
Thank you!
I know its been a long time no talk here. I had the same problem. I have figured it out and it may help people out in the future to solve the problem really quick. Here is the solution.
function remove_menus_custom(){
global $current_user;
if(is_admin(){
remove_submenu_page('woocommerce','wc-admin');
}
}
add_action( 'admin_menu', 'remove_menus_custom', 999 );

How do I make it so my customers cart is emptied upon leaving the checkout page?

On my e-commerce store upon clicking the add to cart button you are redirected straight to the checkout page. I want to make it so when you leave the checkout page your cart is emptied. Is there a php code that can make this work?
I tried this code but its very buggy:
add_action( 'wp_head', 'clear_cart' );
function clear_cart() {
if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
return;
}
WC()->cart->empty_cart();
}
Sometimes my session just ends while on the checkout page or my cart is emptied randomly.
I think the below may help you to empty the cart. Please add in your functions.php file.
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ){
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}

Reset previous chosen shipping method in Woocommerce checkout page

Currently, I am clearing the default shipping selection method using this filter:
add_filter( 'woocommerce_shipping_chosen_method', '__return_false', 99);
However, this only clears it during the initial session of the customer. Once the customer chooses an option even once, it remembers the selection for the future.
I am trying to get the checkout to force the customer to pick a shipping option every time they visit the checkout even in the same session. Is it possible to run this filter every time the checkout page is loaded?
You can reset the last chosen shipping method using in checkout page (for logged in customers):
delete_user_meta( get_current_user_id(), 'shipping_method' );
And also remove the chosen shipping method from session data:
WC()->session->__unset( 'chosen_shipping_methods' );
In a hooked function like:
add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in()
&& get_user_meta( get_current_user_id(), 'shipping_method', true ) ) {
delete_user_meta( get_current_user_id(), 'shipping_method' );
WC()->session->__unset( 'chosen_shipping_methods' );
}
}
Or you can also set a default shipping method for everybody in checkout page:
add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in() ) {
WC()->session->set( 'chosen_shipping_methods', array('flat_rate:14') );
}
}
To find out the shipping methods rate Ids to be used, you can inspect the shipping method radio buttons in cart or checkout pages, with your browser inspector like:
Code goes on function.php file of your active child theme (or active theme). It should works.
Found this solution, it might be a bit hacky.
Just unsets currently selected shipping method and then checks if it's present while validating the checkout.
add_action(
'woocommerce_before_checkout_form',
'reset_previous_chosen_shipping_method'
);
function reset_previous_chosen_shipping_method()
{
if (is_checkout() && !is_wc_endpoint_url()) {
unset(WC()->session->chosen_shipping_methods);
}
}
add_action('woocommerce_after_checkout_validation', 'validate', 10, 2);
function validate($data, $errors)
{
if (!$data['shipping_method']) {
$errors->add('validation', __('Please select shipping method'));
}
}

If cart is empty, the cart page will redirect to shop page in WooCommerce?

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.

Clearing Woocommerce cart on certain pages

So, found this snippet that clears the basket and it works well when added to functions.php:
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action('init', 'my_empty_cart');
How can I modify this and make it empty the cart only when certain pages are loaded? I played around with if ( is_page( 'pageID' ) but couldn't get it working properly!
Try this,
global $post;
if($post->ID == 'something'){
add_action('init', 'my_empty_cart');
}
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
for more readings.
check this link it have all the page / function codes in woocommerce
Hope its works..
WordPress conditional didnt worked for me either so i did something that dont relies on WordPress conditionals:
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}
if u want to add to certain pages u can edit the conditional exp: (didnt try it)
if ($_SERVER['REQUEST_URI'] === get_permalink('post_id'))
if the get_permalink doest work use the exact url exp: 'https://www.domain.name/post-75'

Categories