Clearing Woocommerce cart on certain pages - php

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'

Related

Empty woocommerce cart when visiting page under specific parent page

How can I adjust this PHP code so that it empties the woocommerce cart when I visit a page under a specific parent page?
Example:
I visit www.domain.com/anyparentpage/page
nothing happens
I visit www.domain.com/mtm-user/page
empties cart
`
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$slug = $post->post_name;
if($slug == 'mtm-user') {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
`
I tried adding $post->post_parent but it didnt work
Try replacing slug with parent post id. here 31 is post parent id
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$parent_id = $post->post_parent;
if($parent_id == 31) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}

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 );

Disable WooCommerce shop page completely, but keep search page

I have an issue with WooCommerce. I want to disable the shop page completely, but I still want users to be able to search for products.
This is what I have now - it works, but it also disables the search page, as this uses the shop page.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Hope you can help.
I just modified code in your question & it is working. Please add below code in theme's functions.php file or custom plugin file.
function woocommerce_disable_shop_page() {
global $post;
if (is_shop() && !(is_search())):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Determine whether on shop page in woocommerce

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').

Categories