Woocommerce Disable Persistent Cart for Specific User - woocommerce

I have a woocommerce site setup where I have once specific user created for an answering service. Because multiple 'customers' will be using this one account, I need to disable woocommerce's persistent shopping cart for one specific user ID so that if two operators are talking to two different customers at the same time on different computers, their shopping carts won't be linked. I have yet to see anywhere how I can turn off a persistent cart for woocommerce, let alone for specific users. Can anyone help? Thanks!
EDIT:
After a lot of searching I found this great tutorial https://jhtechservices.com/woocommerce-persistent-cart-issue/ which I followed but doesn't seem to be working. Does anyone have any suggestions?

Woocommerce since 3.4 has special filter:
add_filter( 'woocommerce_persistent_cart_enabled', '__return_false' );
Source: https://github.com/woocommerce/woocommerce/pull/19027

Finally got everything working. Doing so required following https://jhtechservices.com/woocommerce-persistent-cart-issue/ and then clearing the cookies and cache everywhere. Hopefully this'll help someone else!

You can just override the user meta value with a filter - much better than deleting the value every time.
function disable_persistent_cart( $null, $object_id, $meta_key, $single ) {
if ( '_woocommerce_persistent_cart_' . get_current_blog_id() == $meta_key ) {
if (get_current_user_id() === $the_user_id)
return '';
}
return $null;
}
add_filter( 'get_user_metadata', 'disable_persistent_cart', 10, 4 );

Related

Endpoints not working on WooCommerce checkout page

I developing a custom theme. I have an issue on checkout page. When I click on 'Place order' button the URL of checkout page changing from http://localhost/sitename/checkout to look like http://localhost/sitename/checkout/order-received/390/?key=wc_order_DvIkeeaIUoNFI if payment-method is 'Cash on delivery' or http://localhost/sitename/checkout/order-pay/391/?key=wc_order_2TbWibkoOZcxz&order=391 if I choose Internet acquiring payment-method.
But on that pages displayed content of checkout page. I think that endpoints don't work.
Here is what I did to fix that issue:
Checked endpoints in WooCommerce -> Settings -> Advanced
Created new checkout page and deleted old
Checked Chrome DevTools Console for JS errors
Turned off all plugins except for WooCommerce. Issue still exists.
Checked it on another test-site with Storefront theme. Everything works.
Checked all default Woocommerce hooks in my custom checkout templates. There are available.
Create web.configfile in site's directory with code which provides on https://woocommerce.com/document/woocommerce-endpoints-2-1/
Trying to redirect with this code:
<?php
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( home_url('/thank-you') );
exit;
}
if ( is_checkout() && !empty( $wp->query_vars['order-pay'] ) ) {
wp_redirect( 'https://secure.wayforpay.com/pay' );
exit;
}
}
?>
As for me it's bad solution. Because if payment-method is 'Cash on delivery' on thank you page it is not possible to get order data, and if method is 'Internet acquiring' I need to get and transfer order data to acquiring system, but I have a plugin which should do it without my participation. And plugin working on another test-site.
This issue is very popular among junior Wordpress-developers, but there is few information about solving this problem.
I think that endpoints works incorrect, but I don't know how to fix it.
I will be very grateful if you share your own experience in solving the problem or tell me what to look for.
Updated
In addition I compared requests and responds in Chrome->DevTools->Network between site with Storefront theme and my site. They are the same, but on my site the redirect is not happening.
I fixed it.
Main issue consist in that my woocommerce doesn't do shortcodes from Console->Pages, so I activated checkout template via page-checkout.php where I get template part form-checkout.php (get_template_part( 'woocommerce/checkout/form-checkout' );).
To fix a bug I replaced this string with echo do_shortcode(['woocommerce_checkout']);.
Very simple solution that I spent almost 3 days searching for, but even better I learned how wordpress works

Woocommerce: Display 'Downloaded' Marker on Products Logged-In User Has Already Downloaded

I have a Woocommerce store with lots of different digital products across lots of different categories. I also have an annual membership offer that allows members to pay one price and download anything they like from the store. Because I have a lot of products, I'd like to add a little marker/badge/hover icon over any products they have previously downloaded, so they don't waste time/data/HD storage space downloading those products again. I still want them to be able to re-download any of these products if they need to, so I still want them to show up in the store. I'm using the Yith Membership plugin to handle the membership side of things, and Divi theme.
I'm OK with tinkering with a bit of code, and can edit bits and bobs using code snippets I find online, but I'm not knowledgeable enough with the language or the Woocommerce plugins to be able to figure out how to do this myself from scratch and haven't found any ways online, so could someone please suggest a way of achieving this please, and explain how as if I am 5 ;)
Many thanks :)
You can add this to your functions.php file but I would strongly suggest backing up before you make changes to the functions.php file.
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_user_logged_in_product_already_bought', 30 );
function bbloomer_user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
echo '<div>You purchased this in the past. Buy again?</div>';
}
}
For reference this was taken from: https://www.businessbloomer.com/woocommerce-check-current-user-already-purchased-product/

Clear out some woocommerce cart meta on logout

I have some personal information that I'm building during the checkout process and saving on the cart object.
If the user logs out then I want to leave the product selection in the cart intact, but remove the pii that I've added to the cart.
I've tested this code elsewhere, in a normal template and it clears out the cart:
public function clear_pii_from_cart()
{
if (WC()->cart == null) {
return;
}
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
unset(WC()->cart->cart_contents[$cart_item_key]['personalinfo']);
}
// save to cart
WC()->cart->set_session();
}
But when I call it from the wp_logout action the info is still in the cart after logout.
I can't figure out how to get any logging into it to be able to see what data structures I'm dealing with.
It just logs out and seems to do nothing.
I know the hook is working because if I put a die(); in then I get a blank page.
I've combed the WooCommerce source code on github looking for a hint, and not found anything, nor by general searches.
I know this is super niche but does anybody have an idea?
False alarm, I set the priority of wp_logout to 5 and it suddenly started working.
I was imagining that the session had been destroyed just as I was trying to use it or something like that.

Can not turn off wordpress update emails

I am currently receiving 2 e-mails a day asking me to update a Wordpress multisite I once worked on...and I am starting to get desperate!
Things I have tried:
I have changed my email adress both for the admin user and in the general settings.
I have tried adding the following to the functions.php file:
apply_filters( 'auto_core_update_send_email', false);
add_filter( 'auto_core_update_send_email', '__return_false');
apply_filters( 'send_core_update_notification_email', false);
..without any luck.
An obvious solution would be to just update :), but I am hoping to solve this problem once and for all since I am no longer taking this kind of work and have lots of other sites I have worked on in the past.
Any suggestions would be very welcome!
You almost have the correct answer. Change apply_filters to add_filter. Also you need to use '__return_false' which is a hook that just returns false. Using false directly won't do anything. Here's the complete version:
// This stops emails being sent to you after an automatic update.
add_filter( 'auto_core_update_send_email', '__return_false' );
// This stops emails being sent to you to notify you of a new core update.
add_filter( 'send_core_update_notification_email', '__return_false' );

WooCommerce Show Payment Gateways for Logged In Customers Only

I am setting up an ecommerce site using Wordpress and WooCommerce. We are using the wordpress member accounts to track customer information, and we need a way for logged in members only to be able to choose to purchase their cart "on credit", meaning no payment is required to place the order. Basically what I have done is hi-jacked the "Check" option (since we don't need it anywhere else) and renamed it "Credit" since it allows for the functionality we need.
However, I need a way for the "Credit" (check) option to only display if the user is logged in. Is there any way I can just "unhook" this option if the user isn't logged in? This seems like something that would be easy to do, but I couldn't find anything about it. Any help is appreciated.
The original answer to this question (from BWDesign) no longer works due to changes in WooCommerce (at least from WooCommerce 2.1.7 onwards, possibly before). This does the trick now (tested on 2.1.7 and 2.1.8), add it to e.g. your functions.php file:
add_filter( "woocommerce_available_payment_gateways", "rp_filter_gateways", 9999 );
function rp_filter_gateways($args) {
if(!is_user_logged_in() && isset($args['cheque'])) {
unset($args['cheque']);
}
return $args;
}
I just found the solution. In the class-wc-cheque.php file, the check or "cheque" (crazy brits) option is hooked using add_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );. So the solution was simply to add this code to my functions.php file:
if(!is_user_logged_in()){
remove_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );
}
Hope this helps!

Resources