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

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.

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.

How to reset all fields when adding to WooCommerce Cart

I'm developing a site with WooCommerce and have used the plugin Product Addons to add extra fields of text to the item being purchased (name, email address, phone number, etc). When someone clicks on "Add to Cart", the site currently adds the product to the cart, refreshes the page, but the previous data remains in the fields. I want to reset all the fields and make them empty after the product has been added.
I tried using this function:
( function($) {
$( document.body ).on( 'added_to_cart', function() {
$('input').val(''); }
);
} ) ( jQuery );
Any suggestions?
If the page literally refreshes itself then it is not at all standard. It was done just to update the mini cart at the top right corner of your menu and products are being added through ajax. In this case you can't empty all fields on some event because the page is being refreshed, what you have to do is write you code under document.ready function, perform $.each function on the common class or input and empty the input fields.
Try this action hook in your plugin or theme functions.php file
add_filter( 'woocommerce_checkout_get_value', 'misha_clear_checkout_fields_vals' );
function misha_clear_checkout_fields_vals($input){
return '';
}

Refreshing a WordPress Page using JavaScript

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

Woocommerce: Skip cart and go to checkout

So, I can make an order programmatically, but then when I try to use mysite.com/checkout, it redirects me to mysite.com/cart which has no items... I have tried to redirect hook:
function skip_cart_page_redirection_to_checkout() {
// If is cart page, redirect checkout.
if( is_cart() )
wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
But then I get an error saying there is too many redirects...
So, apparently, this is not possible...
However, you can create orders programmatically, so how do I get my customers to be able to pay for their orders? Is there a shortcut to adding to cart at the same time that I wc_create_order()?

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

Resources