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()?
Related
Note: In looking for an answer to my question I came across this post but it is NOT duplicate: Remove add to cart notice and change "add to cart" button in Woocommerce the answer there gives the option to remove the notice from the entire site. I want to remove it only from the cart page and I don't want to do it with CSS.
I use external links to my site to send people directly to the shopping cart with the item already added to the cart. When doing so, the "added-to-cart notification" shows up on the cart page which I do not want.
I found this code which removes the added-to-cart notification: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); but it removes the notification from all pages of my site which is not what I want.
To be more specific, I want the added-to-cart notification to show on every product archive page and nowhere else.
I tried to add a filter but it doesn't work the way I would expect it to, I tried the following two ways (and tested it with various pages to see if I could make anything work but it seems my general syntax is off because I Can't get it to do anything...
function hide_cart_notes() {
if ( ! is_archive() ) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
add_action( 'woocommerce', 'hide_cart_notes' );
function hide_cart_notes() {
if ( is_archive() ) {
return;
}
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
add_action( 'woocommerce', 'hide_cart_notes' );
when woocommerce hook starts? where it's docs? does it run at all?
these question should be answered before.
i know that WordPress parses query at parse_query hook, so i would try this
add_action('parse_query', function() {
if (!is_archive()) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
});
because is_shop(), is_archive(), is_* need query to be parsed first.
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.
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.
I am working on a Wordpress site with Woocommerce. I am using this function inside functions.php to redirect everyone who visits my store to login since it is a private area. Once the person logs in, they should be redirected to the store again. I have the following code implemented, which works for me to get all those trying to navigate my store to log in, I need to modify this code so that once logged in, it redirects me to the store page in woocommerce, in my case the page of the store is called "tienda".
To clarify: Users who are not registered or logged in, should not be able to see the store or cart or anything from woocommerce, I already implemented this with a wp-members plugin, if they try to navigate these woocommerce urls, they must go to login. When you have already logged in, you should go to the store, in my case you should not redirect to the previous link, but must inevitably go to the page called "tienda".
On the other hand, I would like to know if my function is missing something or is well built. Thank you!
function loggedoutuser_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
header('Location: ' . wp_login_url());
exit;
}
}
add_action('template_redirect', 'loggedoutuser_redirect');
function woocommerce_login_redirect_custom( $redirect, $user ) {
$redirect = wc_get_page_permalink( 'shop' );
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'woocommerce_login_redirect_custom', 10, 2 );
Paste this above snippet at the end of your active child theme functions.php file or else if you are good in plugin development then create a custom plugin.
Snippet Explanation:
Create a custom function to redirect after logged in
Fetch the shop URL using WooCommerce helper function wc_get_page_permalink( 'shop' ) and assign it to the variable $redirect
Then return the URL
Hook the custom function to the filter hook woocommerce_login_redirect
Note: You can check the user role inside our custom function then based on the role you can redirect it to the dashboard if he is administrator else to the shop page.
I wanna do the following:
If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5. I tried adding the below code into my header, but it didn't work.
add_action( 'init', 'check_redirect_page' );
function check_redirect_page() {
if ( is_user_logged_in() && is_page( 10 ) ) {
wp_redirect( get_permalink( 5 ) );
exit;
}
}
Try using the wp hook rather than init; WordPress won't have got far enough at init to know whether it's dealing with a particular page or not.
add_action( 'wp', 'check_redirect_page' );
From the Conditional Tags documentation:
Warning: You can only use conditional query tags after the
posts_selection action hook in WordPress (the wp action hook is the
first one through which you can use these conditionals). For themes,
this means the conditional tag will never work properly if you are
using it in the body of functions.php, i.e. outside of a function.