Adding a skip to checkout button woocmmerce - php

I want to add another button beside "add to cart" on single product page that will add the product to cart and also take the user to checkout page .
if I will use
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
this will override the add to cart button, but I want it to be like it is and want another button that will do this job ? is it possible with hooks ?

I solved it by using a hidden field and two submit button, May be a dirty way but it solved my problem. It can be done in a better way using a checkbox in place of two button, *( if checkbox is checked go to checkout page ).
add_action( 'woocommerce_variable_skip_to_checkout', 'woocommerce_variable_skip_to_checkout', 30 );
if ( ! function_exists( 'woocommerce_variable_skip_to_checkout' ) ) {
function woocommerce_variable_skip_to_checkout() {
wc_get_template( 'single-product/add-to-cart/variation-skip-to-checkout-button.php' );
}
}
add_filter ('woocommerce_add_to_cart_redirect', function() {
if ( isset($_POST['skip_to_checkout']) && $_POST['skip_to_checkout'] == 'true' ){
return WC()->cart->get_checkout_url();
}
} );
Template part
variation-skip-to-checkout-button.php
template holds the "skip to checkout" button and a hidden field, which you can replace with direct html in this function or a checkbox. and in place of woocommerce_variable_skip_to_checkout hook you should definitely prefer woocommerce_after_add_to_cart_button hook.

Related

Woocommerce: redirect hook overruled by error

In Wordpress/Woocommerce when clicking an order button, I would like it to skip the basket and immediately go to checkout. To this end, I implemented the following hook:
add_filter('add_to_cart_redirect', 'cw_redirect_add_to_cart');
function cw_redirect_add_to_cart() {
global $woocommerce;
$cw_redirect_url_checkout = $woocommerce->cart->get_checkout_url();
return $cw_redirect_url_checkout;
}
This works. However, in the scenario that the user already has the product in their basket and clicks on the order button, this would normally produce an error message "You cannot add another productname to your basket", which would be displayed on the basket page. But with the code snippet, in this scenario it just refreshes the page where the user clicked the order button and nothing happens. A user will not understand why the button doesn't work (only if they would manually type in the basket url, they will see the error message).
How, in this scenario, can I still redirect to the checkout page?
The hook add_to_cart_redirect you are using is deprecated from version 3.0.0 as the get_checkout_url method is deprecated from version 2.5.
The updated function can be found here: Woocommerce add to cart button redirect to checkout
Your problem is related to the woocommerce_add_to_cart_redirect hook not being executed in case there are any error notices. Below you will find the part of the code extracted from the WooCommerce source code:
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && 0 === wc_notice_count( 'error' ) ) {
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
if ( $url ) {
wp_safe_redirect( $url );
exit;
} elseif ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
So to fix the problem you can find a solution here.The post includes a Fix for "Sold Individually" Products section that answers your question.

Remove WooCommerce admin order action from order preview

I want to remove, for shop-managers, the ability to mark an order as completed. To do so, I used the following based on "Hide a specific action button conditionally in Woocommerce admin Orders list" answer in my theme's functions.php file:
add_filter( 'woocommerce_admin_order_actions', 'custom_admin_order_actions', 900, 2 );
function custom_admin_order_actions( $actions, $the_order ){
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop-manager')
unset($actions['complete']);
return $actions;
}
By this, I succesfully removed the complete button from the shop_order page. However, the shop-manager is still able to complete the order using the Complete button that appears in the order preview. To avoid this, I tried the next action after the previous one:
add_action( 'woocommerce_admin_order_preview_start', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Class = "button hidden wc-action-button wc-action-button-complete complete"</div><br>';
}
However, this does not remove the button from the preview window because it does not substitute the line in the code.
Is there a way to remove this ability from the shop_order page and the order preview at once? If not, how can I hide this button from the preview window?
To remove the "complete" update order status button from admin order preview for "Shop manager" user role, use the following:
add_filter( 'woocommerce_admin_order_preview_actions', 'filter_admin_order_preview_actions', 10, 2 );
function filter_admin_order_preview_actions( $actions, $order ) {
if( current_user_can('shop-manager') && isset($actions['status']['actions']['complete']) ) {
unset($actions['status']['actions']['complete']);
}
return $actions;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

WooCommerce Button to Clear Cart, Add One Item and go to Checkout

I need a button on a unique landing page which does this:
clears the cart
adds a specific Item to the cart
go directly to checkout
(disable or hide menue-bar in checkout) <-I tried this with JavaScript but failed
I got a button which calls the link: https://www.snoooze.co/?add-to-cart= 12374
And then this snippet in my "functions.php"
add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');
function woo_redirect_to_checkout() {
global $woocommerce;
$woocommerce->cart->empty_cart();
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url; }
The problem is that if I add the item in my function with add_to_Cart() it does this every time I want to add it manually in the shop, not just if I click on the button on the landing page.
I see that I have to assign the function to the button in some way, so doesn't get called on other sites, but how?
Any help please.
First you need a custom button embedded in an form. So here is a simple shortcode that you can use in a content editor or in php:
// Shortcode: Special button "add-to-cart" with form
function special_button() {
return '<form method="post" action="">
<button type="submit" class="button" name="add_to_cart_special">Special add to cart</button>
</form>';
}
add_shortcode( 'special_button', 'special_button' );
// Usage: [special_button]
// or for php: echo do_shortcode("[special_button]");
And this custom hooked function that will be triggered when you press that custom button. It will:
empty cart,
add product 12374 to cart
redirect to checkout
display a custom notice in checkout page (optionally)
The code:
// Special add to cart (empty cart before and redirect to checkout)
add_action( 'template_redirect', 'special_add_to_cart' );
function special_add_to_cart() {
if ( isset($_POST['add_to_cart_special']) ){
WC()->cart->empty_cart();
WC()->cart->add_to_cart( 12374 );
wc_add_notice( __('this product X has been added to cart'), 'notice' );
wp_redirect( wc_get_checkout_url() );
exit();
}
}
ALL Code goes in function.php file of your active child theme (or active theme).
Tested and works.
But to disable or hide your "menu bar" in checkout, I really don't know… This should be another new question with more details…

Woocommerce check if coupon already applied?

So on the checkout page, how can i tell if a coupon has previously been applied from the cart page? I can check this condition via jquery but the doesnt function how i want because that doesnt happen until the DOM has already loaded. I want the form-checkout.php page to check for the coupon before its sent to the user, so i can either hide or show <p class="woocommerce-info">Have a coupon? Click here to enter your code</p>
Try this code. This will hide 'Coupon form' on checkout page, if any coupon is already applied from cart
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;
}
Hope this will be helpful

Woocommerce add to cart button redirect to checkout

I created an ecommerce using the plugin woocommerce. I am selling only a subscription so the "/cart/" page is useless. I'm trying to get rid of it so that when my customer click on "Add to cart" button, he ends up on the checkout page.
In WooCommerce 3.6 or later you can use woocommerce_add_to_cart_redirect (props #roman)
add_filter ('woocommerce_add_to_cart_redirect', function( $url, $adding_to_cart ) {
return wc_get_checkout_url();
}, 10, 2 );
Original answer:
you can use a filter in functions.php:
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
it doesn't seem to work with ajax, but it works from the single product pages, which I think is what you use
On WooCommerce (>= 2.1) the function can be simplified as:
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
There is an option within WooCommerce settings that allows you to enable this functionality:
Simply login to your WP admin panel > WooCommerce > Catalog and select the option. I hope this helps!
I've found a simple solution that work like magic.
As mentioned by #Ewout, check the box that says "Redirecto to cart page after succesful addtion".
Woocommerce > Settings > Checkout (Tab) - where you should select pages for cart and checkout, select the checkout page as the cart page (image attached).
That's it. works for me.
Update for WooCommerce 3.5.1
Step 1.
First of all go to WooCommerce Products settings and deactivate AJAX add to cart.
Step 2.
Use woocommerce_add_to_cart_redirect hook to make a redirect to checkout.
add_filter( 'woocommerce_add_to_cart_redirect', function( $url ) {
return wc_get_checkout_url();
});
Of course there some small things are left to do, like changing add to cart buttons text and removing some WooCommerce cart-related notices. I recommend to check this tutorial for more https://rudrastyh.com/woocommerce/redirect-to-checkout-skip-cart.html
#RemiCorson posted this brief but beneficial tutorial:
http://www.remicorson.com/woocommerce-skip-product-cart-pages/
He mentions the same filter as #Ewout above,
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
but one line of code stands out and is of super value for me for my current woocommerce project:
There is a direct link that a user can use to automatically bypass the product page.
http://your-site.com/?add-to-cart=37
'37' will be replaced by your product ID.
This was useful for me to eliminate unnecessary steps and take users directly to checkout from the home page and other non-woocommerce pages/posts.
Filter add_to_cart_redirect is deprecated in WooCommerce 2.6. Use woocommerce_add_to_cart_redirect instead.
Add this to your functions.php :
add_filter ('woocommerce_add_to_cart_redirect', function() {
return WC()->cart->get_checkout_url();
} );
Try the below code in the themes function.php file
add_filter( 'woocommerce_add_to_cart_redirect', 'woo_skip_cart_redirect_checkout' );
function woo_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
On shop page, if you want use ajax and redirect toghether. The second method only when there are some condition, you can use this filter and leave on Woocommerce setting ajax enabled:
add_filter('woocommerce_loop_add_to_cart_link', array( $this, 'add_quantity_input' ), 4, 2);
to remove on a class attribute ajax_add_to_cart and change the href value to checkout url page;
On my template case:
public function add_quantity_input($text = null, $product = null) {
global $product, $woocommerce;
if ( $text != null and $product != null ) {
if(ismycondition($product->id)) {
$s = explode('class="', $text);
$s[2]=str_replace('ajax_add_to_cart', '', $s[2]);
$text = implode('class="', $s);
$text = preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="'.$woocommerce->cart->get_checkout_url().'"$3>', $text);
}
}
return $text;
}
I hope that this help.
None of the solutions actually worked out for me, the filter add_to_cart_redirect was triggering on every page,not only on the cart.I did some modification on the suggested answer.
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
if(is_cart()){
$checkout_url = WC()->cart->get_checkout_url();
?>
<script>
location = '<?=$checkout_url?>';
</script>
<?php
}
}

Categories