WooCommerce - Change Add to cart URL - php

I am using WooCommerce Product Bundles, and at the end of the product there is an Add to Cart button. Clicking it refreshed the page, and adds the addons to the cart. I want to change the URL to a different page, instead of just refreshing it.
Here is the button's HTML code:
<button type="submit" class="single_add_to_cart_button bundle_add_to_cart_button button alt">Add to cart</button>

If I got it correctly, you want to redirect to different page after the add to cart action done right.?
If that is the case put this on your functions.php
function redirect_after_add_to_cart( $url ) {
return esc_url( get_permalink( get_page_by_title( 'Your Page Title' ) ) );
}
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_after_add_to_cart', 99 );

Related

Additional custom button for specific single product page in WooCommerce

In WooCommerce on need to create another button that redirects to "Contact Us" form below my current "Add to Cart" button for specific product page (example: http://offers.elements.com.sg/product/ha-power-dose-facial/).
End product page:
There will be 2 different buttons for users to choose
One will be "Add to Cart" that leads to PayPal page and the other will lead to "Contact Us" form
Users can choose either one.
I'm using on OceanWP theme.
Based on additional add to cart button with fixed quantity in woocommerce single product pages answer code, here is the way to do it:
add_action( 'woocommerce_after_add_to_cart_button', 'additional_single_product_button', 20 );
function additional_single_product_button() {
global $product;
// Define your targeted product IDs in the array below
$targeted_product_ids = array( 37, 53 );
if( in_array( $product->get_id(), $targeted_product_ids ) ) {
$link = home_url('/contact-us/'); // <== Here set button link
$name = esc_html ( "Contact Us", "woocommerce" ); // <== Here set button name
$class = 'button alt';
$style = 'display: inline-block; margin-top: 12px;';
// Output
echo '<br><a rel="no-follow" href="'.$link.'" class="'.$class.'" style="'.$style.'">'.$name.'</a>';
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Other related answers:
Adding a custom button after add to cart button in single product pages
Add a button after add to cart and redirect it to some custom link in WooCommerce
Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type
You can use some third party plugins that will provide features to add buttons on a single product page.
or you can add a button in a single product file using coding .. you can use a single product file in the child theme from the WooCommerce templates folder.
or you can also use hook to add button in shop loop like this :
add_action( 'woocommerce_after_shop_loop_item', 'new_add_to_cart_button' );
function new_add_to_cart_button() {
// Your button code.
}

WooCommerce add to cart redirect based on button clicked

I'd like to create two different add-to-cart buttons on the product page. Based on what button you click it has to redirect to the cart page or shop (archive) page.
On the product page I created some extra custom fields (item data) the user have to fill in, before adding the product to their shopping bag. There are multiple explanations for redirecting your customers to the page you want with the woocommerce_add_to_cart_redirect hook, but I'd like to customize it a bit based on the button you click.
I've added a second submit button to the add to cart form on the product page. These buttons are inside the form, else the custom fields won't validate and products won't add to the cart. There are multiple explanations and hooks online to create extra add to cart buttons to your product page, but these won't work because of the validation that needed to be done.
<button type="submit" name="add-to-cart" value="overview <?php echo esc_attr( $product->get_id() ); ?>" class="c-link"><?php _e('Save and go to shop', 'stackoverflowcom'); ?></button>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="c-btn --gold single_add_to_cart_button button alt"><?php _e('Go to shopping bag', 'stackoverflowcom'); ?></button>
Second step is I created a function for the woocommerce_add_to_cart_redirect hook, to check for the button that is clicked.
function custom_add_to_cart_redirect( $url ) {
if ($_POST['add-to-cart'] == 'overview') {
$url = get_permalink( 17 );
return $url;
} else {
$url = get_permalink( wc_get_page_id( 'cart' ));
return $url;
}} add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
Big issue however, you can't have multiple values (or names) inside your button. So the above function won't work. Does anyone have other solutions? Does anyone know how I can create a secondary add to cart button with a different redirect?
Thanks in advance!
My solution based on what CBroe says, I created a javascript function:
<script>
function overviewClick() {
document.getElementById('overview').value="overview"
document.getElementById('cart').submit();
}
</script>
Created a hidden input field with the name 'overview'. Then I created a PHP function that checks if the input field is empty or not:
function custom_add_to_cart_redirect( $url ) {
if( ! empty( $_POST['overview'] ) ) {
$url = get_permalink( 17 );
return $url;
} else {
$url = get_permalink( wc_get_page_id( 'cart' ));
return $url;
}}

Add a button in checkout to empty cart and redirect to shop page in Woocommerce

I would like to have a cancel order button behind/under the checkout button in Woocommerce. Then after a customer click this button, the shopping cart would be empty, redirect to shop page.
I tried several things in the template with the woocommerce_cancelled_order
in template form-checkout.php.
But I cannot figure this out. How can I can solve this problem?
In checkout the order object doesn't exist until customer clicks on "Place Order", so you can't use the hook woocommerce_cancelled_order located in cancel_order() method.
In checkout page you need to empty cart instead using a custom button and the following code will empty the cart and redirect to shop when "Cancel order" is clicked:
add_action( 'woocommerce_review_order_after_submit', 'checkout_reset_button', 10 );
function checkout_reset_button(){
echo '<br><br>
<a class="button alt" style="text-align:center;" href="?cancel=1">'.__("Cancel order", "woocommerce").'</a>';
}
add_action( 'template_redirect', 'checkout_reset_cart' );
function checkout_reset_cart() {
if( ! is_admin() && isset($_GET['cancel']) ) {
WC()->cart->empty_cart();
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
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…

Changing the redirect of the “add to cart” button in woocommerce store in Shop page

I am trying to change the redirect URL in the woocommerce for the 'add to cart' button in pages other than the single product page. The URL for the single product page can be changed with this solution. However this solution will not work for the for any other pages, to change the redirect after pressing the 'add to cart' button. Any help is greatly appreciated.
To make woocommerce_add_to_cart_redirect work on pages like shop, categories or tags archives pages, you need first in backend WooCommerce > Settings > Products > Display:
Disable the checkbox "Enable AJAX add to cart buttons on archives".
Then the hooked the code will work with woocommerce_add_to_cart_redirect hook on all woocommerce pages.
function wc_add_to_cart_custom_redirect() {
// Here the redirection
return site_url('/mypage/');
}
add_filter( 'woocommerce_add_to_cart_redirect', 'wc_add_to_cart_custom_redirect' );
You can use this in your Appearence>Theme Editor>function.php
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
This will change your Add to cart to View porducts and the Url would be that specific products's Url

Categories