Woocommerce check if coupon already applied? - php

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

Related

Removing a purchase button from WoocCmmerce by custom stock status with a email

i'm writing a plugin for WooCommerce to allow custom stock status's
All is good apart from the contact1 status which when I try and remove the purchase button and input any text or links with the removal it prints the same status and form at the top of the screen as well with a post error. the code I'm using which works if I comment out the echo of the form. the error shown under the form copied to the top is
class=”product type-product post-11 status-publish first contact1 product_cat-uncategorized sold-individually shipping-taxable product-type-simple”>
Also with contact form 7 is there a way to pull the product from the page your on for the subject. for my short code.
is
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'contact1' ) {
echo 'Click Here to Email for Delivery Date';
echo do_shortcode('[contact-form-7 id="176" title="efdf"]');
return false;
} else {
return $purchasable;}
}

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.

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 - Empty Cart on Page Reload

I have skipped the cart on my course membership site so 'Buy Now' brings you straight to checkout.
However, if you were logged in when you clicked 'Buy Now' then exit without buying, the item remains in the cart the next time you are logged in. If you go to purchase the same item during the next visit, it says "This item is already in your cart" but since I have the cart hidden from the front end, they can't access it.
Is it possible to empty the cart when page reloads with WooCommerce so it always goes straight to checkout when the user clicks 'Buy Now'?
Seems you're selling product individually. In this solution we're bypassing the filter hook woocommerce_add_to_cart_sold_individually_found_in_cart. When $found_in_cart is true users getting the message "This item is already in your cart". That's why we're resetting the quantity to 1. For more details please check https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1064
function op_bypass_add_to_cart_sold_individually_found_in_cart( $found_in_cart, $product_id ) {
if ( $found_in_cart ) {
$cart_contents = WC()->cart->get_cart_contents();
foreach ( $cart_contents as $key => $item ) {
if ( $product_id === $item['product_id'] ) {
WC()->cart->set_quantity( $key, 1 );
break;
}
}
return false;
}
return $found_in_cart;
}
add_filter( 'woocommerce_add_to_cart_sold_individually_found_in_cart', 'op_bypass_add_to_cart_sold_individually_found_in_cart', 10, 2 );
In my case " WC()->cart->set_quantity( $key, 1 );" from the answer #obiPlabon didn't worked proper, so I simplified function.
Since $found_in_cart is already fire only when amount of sold individually products in cart is more then one, I decided to simplify code and just make redirect to checkout page just it run.
if ( $found_in_cart ) {
global $woocommerce;
wp_redirect( wc_get_checkout_url() );
exit;
}

Adding a skip to checkout button woocmmerce

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.

Categories