Refreshing a WordPress Page using JavaScript - wordpress

I am creating a custom cart for a website, and i am still a beginner.
I am using Wordpress/woocommerce functions to add or delete from the cart, but after submiting my changes i want the page to be refreshed so i used javascript to do that.
location.reload();
however this is taking me back to the home page but with a url having the item id that i deleted from the cart.
is there a better way to refresh wordpress pages, or should location.reload() make no issues?
this is a php snippet of how i am deleting an item, i am also concerned that this snippet is responsible for taking me back to the homepage
$prod_to_remove = $values[product_id];
// Cycle through each product in the cart
foreach ( $WC->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get the Variation or Product ID
$prod_id = $cart_item['product_id'];
// Check to see if IDs match
if( $prod_to_remove == $prod_id ) {
$WC->cart->set_quantity($cart_item_key,$cart_item['quantity'] -1,true);
break;
}
}
any insight over the issue is appreciated, thanks in advance
Link not clear in the comments but this is how the URL is being redirected
http://localhost/wordpress/index.php/cart/basket.php?r=1&id=2838023a778dfaecdc212708f721b788

Related

Hide & refresh WooCommerce checkout after add coupon

We want to hide "Have a coupon? Add one..." on WooCommerce checkout, if a coupon is already added OR when a customer add a coupon on the checkout page.
Currently we habe this code below and it works when a customer enters a coupon on the cart page and then navigate to the checkout page. In this case, the "Have a coupon? Add one..." message is not visible. If no coupon in added on the cart page the message is visible.
This is working fine! But it does not work when a customer adds a coupon on the checkout page.
1.) We get the message "Coupon added" But the coupon message to add one is still visible AND also the coupon is not calculated in the order table. => After a page refresh is everything correctly.
2.) When a coupon is removed by the customer on checkout, then we get the message that the coupon is removed, but the discount is still visible in the order table. => After a page refresh it displays everything right again.
So now I'm trying to refresh the page after a coupon was added or removed. But I have problems to get the right event. I guess we must do this via js? Or is there a PHP approach?
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
return false;
}
return $coupons_enabled;
}
Your code should be like this
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
if(is_checkout()){
global $woocommerce;
if ( ! empty( $woocommerce->cart->get_applied_coupons() ) ) {
$coupons_enabled = false;
}
}
return $coupons_enabled;
}
Edit: okay you need to check if page is checkout or cart then run the script. I have added condition in code.

Woocommerce how to click a button to add to cart and checkout?

Objective:
I would like the customer to click on a button, add an item with quantity = 1 to cart, and route to checkout page automatically.
What I did:
I'm using Elementor to add a button with a href value of:
https://fakeurl.com/checkout/?add-to-cart=59
Problem:
Once I click the button, it will route to the checkout page, however it will add 2 quantity instead of one to the cart.
What I've tried:
Explicitly specify the quantity count in the href:
https://fakeurl.com/checkout?add-to-cart=59&quantity=1
But I'm getting the same results.
My checkout page is just simple page with 2 shortcodes namely woocommerce_cart & woocommerce_checkout:
Any idea why? Do I need to empty the cart before the aforementioned button is pressed?
Use your link structure as you already do > ?add-to-cart=59&quantity=1 and add below code in functions.php in your theme to just do checking
the only thing this peace of code do is to loop your cart to see if this product is already there .. and if it is - it sets $valid var on false
function is_product_in_cart( $valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0) return true;
foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
$_product = $values['data'];
$id = $_product->id ;
if( $product_id == $id ) $valid = false;
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_in_cart', 10, 3 );
Woocommerce default flow is that it will add the quantity to the cart whenever you add an item that is already in the cart.
Empty your cart whenever a new product is added to the cart so that only one remains in the cart.
In most cases the quantity is doubled because you are being redirected.
So the quantity is added and after the redirect the quantity is added again.
The reasons for this could be a couple of the things.
Theme or wordpress permalinks setting are adding or removing // in the links
Your checkout page is not set in woocommerce settings (woocommerce->advanced settings->Page settings)
How to check if you are being redirected (chrome)
At the top of Chrome's inspector (in the Network tab) is a checkbox which says Preserve log. Enable this option. Now it doesn't matter at all how the page navigates, the inspector will keep all log history -- including the redirect response.
(found here: See full redirect path and HTTP status code in Chrome)
Possible solutions
“Enable AJAX add to cart buttons on archives” (WooCommerce –> Settings –> Products -> General)
found here (https://www.businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/)
This sometimes help when having custom pages for cart and checkout.
Disable "Redirect to the cart page after successful addition" under WooCommerce > Settings > Products.Because you are linking to the checkout page, this wil redirect you again to the cart page.

How to disable Woocommerce login redirect if on Checkout Page

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

Woocommerce clear cart after payment

I'm having trouble clearing my cart after payment but I'm using Woocommerce.
I used codes and plugin from an older site (before the major update in the beginning of the year) and that doesn't seem to work now on the new updated site.
This is the functions.php
// check for clear-cart get param to clear the cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
In the thankyou.php that appears after succesfull payment this code is in there.
<!-- clear cart after successfull payment -->
jQuery(function($) {
$.post("http://ihavetakenoutmydomain.org?clear-cart",{},function(response){
var NewCart = $(response).find('#header-cart-inner');
$('#header-cart').html(NewCart);
var NewCartItems = $(response).find('.cart-items-inner');
$('.cart-items').html(NewCartItems);
});
});
I'm using the same theme as in the older version so the header-cart-inner and header-cart are the same.
But for some reason this doesn't work with the updated Wordpress and Woocommerce.
Does anyone know what the problem might be or have another solution for clearing the cart after succesfull payment? The thankyou page should only appear after successfull payment.
I forgot one thing
If I load the link "xxx://ihavetakenoutmydomain.org?clear-cart" on my browser the cart empties. So I guess the problem would be how to activate the link on the thankyou page without redirecting away from the thankyou page.
Best regards

Only logged in users should be able to view product in WooCommerce

I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.

Resources