Empty woocommerce cart when visiting page under specific parent page - php

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

Related

add_action run for the specific page

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

Add CSS to WooCommerce product pages only if product is not on sale

I need to add some CSS to hide some elements/classes on the page if the product is NOT on sale.
From another function I have, I am assuming I can check if $args['meta_key'] = '_sale_price'; is blank and if it then adds the CSS.
How can I use this in my functions.php?
add_action( 'wp', 'add_cssif_product_not_onsale' );
function add_cssif_product_not_onsale() {
if ( is_product() ) {
global $post;
$product = wc_get_product( $post );
if ( !$product->is_on_sale() )
echo "<style>body{display:none}</style>";
}
}

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

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

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