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…
Related
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.
}
I'm trying to replace the default woocommerce product archive add to cart button based on a condition.
For example
Product A - Checkbox Active --> Display Find A Dealer Button
Product B - Checkbox inactive -- > Display default add to cart button
I have managed to successfully write the code to add the checkbox and the condition to replace the button if the product has a custom checkbox active. The button for product A works fine and diaplyas as intended in the shop archives.
I am however not sure how to retain the woocommerce default add to cart button if for products that do no have this checkbox activate. I thought adding the action would work however I am stumped. Any help would be greatly appreciated. Thank you in advance.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
global $product;
if ($product->get_meta('_checkbox_active') === 'yes' ){
return '<button>Finda Dealer</button>';}
else {add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );}
You just forgot the hooked function variables arguments. Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
if ( $product->get_meta('_checkbox_active') === 'yes' ){
$button = '' . __( "Find a dealer", "woocommerce" ) . '';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
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.
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.
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 );