I added a link to the cart page inside the message that pops up when a product is successfully added to the cart in Woocommerce. This however does nothing when it's clicked. The inspector does show the correct working link.
// Add cart page URL
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
$returnArray['messages']['success'] = sprintf(__('Products successfully added to cart! View cart','woocommerce'), $cart_url);
I think I should note that this part of the code is used twice in the page:
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
I understand that using the same variable twice could cause issues but in this case they should do the same thing anyway but could this be the cause?
EDIT: changed the method for getting the cart page URL
$returnArray['messages']['success'] = sprintf( __('Products successfully added to cart! View cart','sf-european-fasteners'), wc_get_cart_url() );
Related
I would like to hide all buttons only for Eternal/Affiliate and only on the produce page
TLDR
On my site I have a few Pricing tables where the customer can select the appropriate one and then add that item to the cart.
The issue is trying to make an empty product where you cannot purchase it but the pricing table is in the description. So I had to user Eternal/Affiliate product. Which works perfectly as the URL is the product page itself which is great for the shop.
The issue is on the product page you can see a button which says add to cart.
I would like to hide this button only for Eternal/Affiliate and only on the produce page.
You see I already have this code below but obviously I do not want to restrict it to ID numbers and variable products.
Update
so the code below works but if I put a URL link in on the product page then it does not work. Unfortunately now a little bit stumped.
What would be great is if I could remove the button and replace it with some text for example please read below.
function woocommerce_add_aff_link(){
$product = wc_get_product(get_the_ID());
if ($product->is_type('external'))
echo '<a href="' .
$product->get_product_url() .
'" class="woocommerce-LoopProductImage-link">';
}
Update +
After some digging around I found that it is actually more logical to create a new product however the solution below works perfectly:
Reference : https://www.businessbloomer.com/woocommerce-how-to-create-a-new-product-type/
To remove add to cart button on single external product pages use the following:
add_action( 'woocommerce_single_product_summary', 'remove_external_product_add_to_cart_button', 4 );
function remove_external_product_add_to_cart_button(){
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product(get_the_ID());
}
if ( $product->is_type('external') ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in functions.php file of the active child theme (or active theme). TIt should work.
I'm using WooCommerce and added the following code to functions.php:
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
$add_to_cart_url = wc_get_checkout_url();
return $add_to_cart_url;
}
What I would expect is that when a user adds a product to their basket: this code will redirect the user to the checkout page without actually adding the product to the basket. However, it still adds the product to the basket. What is wrong with this code?
Background: I'm using Uncaught Error: Call to a member function generate_cart_id() on null answer code but it doesn't work correctly for the above line, which is why I'm now working with only that line. There's no error in the server logs.
Based on your comment, to get a redirection to checkout page for a custom add to cart button, you could change the link to something like https://example.com/checkout/?add-to-cart=99 where 99 is the product Id that you want to add to cart.
So the code for your custom add to cart button link will be like (where 99 is the product Id):
$product_id = 99;
$url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
I am having the woocommerce store like : example.com/store/
And I am creating the some product sales page in outside of wordpress : example.com/upsell.php
Now I want to clear the cart once you visit the example.com/upsell.php, because we are having multiple step in the upsell and finally we are send a url request to add the products in the cart (example.com/store/cart/?add-to-cart=1,5,8).
Whenever you visit the upsell page we need to clear the cart session.
How can clear the cart session from the upsell page?
You need to add an action that will clear cart items in template redirect hook.
In the custom function, check the current page slug & then clear the cart as per our condition.
Use the below code snippet in your theme's functions.php or custom plugin file.
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$slug = $post->post_name;
if($slug == 'sample-page') {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
Update
If you don't like hard coding the page slug, there is also a better method.
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
Add the above code in your theme's functions.php file.
Then redirect to a page by adding clear-cart query string in your URL & that will clear all cart items.
We can use this function in any URL.
http://example.com?clear-cart
or
http://example.com/sample-page/?clear-cart
I'd use $_SERVER['REQUEST_URI'] to get the current URL of the page so you can test whether you're on the upsell page.
You'll need to use the following function to clear the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
you need to create web api in wp instance which clears cart using this code
global $woocommerce;
$woocommerce->cart->empty_cart();
and call that api on upsell.php file. You can even use HTTP_REFERER to check whether user directly hit the upsell.php url or not.
I would like to disable the WooCommerce cart functionality by all means.
My shop has only a single product, so cart is doesn't really needed.
My desired flow is Click on buy button -> go to checkout page.
Incase user goes back and redo the same process, checkout page will not show 2 products in summary buy just 1.
Any tips on how to achieve this smoothly ?
Thanks,
You will Need 4 code snippets:
1) Disabling quantities buttons (on product page):
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
2) Add-to-cart validation, allowing just one product in cart:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_is_in_cart' );
function check_product_is_in_cart() {
WC()->cart->empty_cart();
return true;
}
3) Checkout redirect customer when your product is added to cart (with modern syntax):
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
// OR ALSO:
// return get_permalink(get_option('woocommerce_checkout_page_id'));
}
The code comme from this answer (with the correct new syntax): Woocommerce add to cart button redirect to checkout
4) Redirect Cart page to Checkout page (in case of):
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
if(is_cart()){
wp_redirect(WC()->cart->get_checkout_url());
// OR ALSO:
// wp_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
exit; // This is mandatory with wp_redirect()
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Disabling redirect to Cart on add-to-cart action and Ajax add-to-cart on Shop page and archives pages (optional)
You can also disable some settings in WooCommerce > Settings > Products > Display (tab).
Optionally keep that 2 options disabled (and save settings):
If you need to skip the cart page the easiest way it is to go to the Woocommerce->Settings->Checkout and set Cart Page to "Checkout" page
Or use this snippet
add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
Don't forget to disable Ajax Add to cart and Redirect to the cart page after successful addition in Woocommerce->Settings->Products->Display
If you use the first method you can check the Redirect to the cart page after successful addition.
For the limit one item in checkout use this snippet.
In this way, the customer only can buy one item, if the customer goes to another product and tries to buy it, the cart will be cleaned and the last item added.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}
Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:
function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .
Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.
How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?
You can skip cart definitively, redirecting customers to checkout page when cart url is called.
To achieve this use this code snippet, that should do the trick:
// Function that skip cart redirecting to checkout
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');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:
wp_redirect( wc_get_checkout_url() );