Woocommerce how to remove coupon code box programmatically - woocommerce

I wonder if you can help? I'm trying to get this to work.
I want to only show the coupon box when the price is more than 19.99 AND its not product id 455821
I can't get this to work. Would you be able to give me any ideas why?
Thanks
Kevin
// START Remove coupon box when price is less than 19.99
add_filter( 'woocommerce_coupons_enabled','hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
global $product;
// $id = $product->get_id();
$product_id = 455821;
$price = "19.95";
$cart = WC()->cart->get_cart();
foreach ( $cart as $id => $cart_item ) {
// $prodid = $cart_item[ 'data' ]->$product->get_id();
if( ($cart_item[ 'data' ]->get_price() <= $price) || ( $cart_item[ 'data' ]->$product->get_id() != $product_id ) ) {
return false; // dont remove it
}
}
return $enabled;
}

// START Remove coupon box when price is less than 19.99
function hide_coupon_field_on_cart( $enabled ) {
// settings
$not_product_id = 455821;
$min_price = 19.99;
// Get cart object
$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item ) {
// product id
$product_id = $cart_item['product_id'];
// Price
$price = $cart_item['data']->get_price();
if ( $product_id == $not_product_id && $price < $min_price ) {
$enabled = false;
}
}
// Cart total <= 0
if ( WC()->cart->get_total() <= 0 ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled','hide_coupon_field_on_cart' );

Related

How to update WooCommerce product price based on the selected payment gateway? [duplicate]

I want to add percentage value to cart item prices based on the selected payment gateway.
The problem I am facing is variation product price is not updating for the product price. Initially selected price is showing all the time.
How can I get the changed price accordingly?
My code so far:
// Set custom cart item price
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if($chosen_payment_method == 'cod') {
$increaseby = 3;
} elseif($chosen_payment_method == 'paypal') {
$increaseby = 8;
} else {
$increaseby = 10;
}
$price = get_post_meta($cart_item['product_id'] , '_price', true);
$price = $price + (($price * $increaseby)/100);
$cart_item['data']->set_price( $price );
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
Any help highly appreciated.
There are some mistakes in your code
Use WC()->session->get( 'chosen_payment_method' ); outside the foreach loop
get_post_meta() is not needed to get the price, you can use get_price()
You will also need jQuery that is triggered when changing the payment method.
So you get:
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get payment method
$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
// Compare
if ( $chosen_payment_method == 'cod' ) {
$increaseby = 3;
} elseif ( $chosen_payment_method == 'paypal' ) {
$increaseby = 8;
} else {
$increaseby = 10;
}
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get price
$price = $cart_item['data']->get_price();
// Set price
$cart_item['data']->set_price( $price + ( $price * $increaseby ) / 100 );
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
function action_wp_footer() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery(function($){
$( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function() {
$(document.body).trigger( 'update_checkout' );
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'action_wp_footer' );

Disable shipping method when total of products from certain category below $99

I'm trying to disable certain shipping method if the sum of the products from category "Pillows" is below $99 (it's some sort of a premium shipping active only if customer orders more than $99).
I have this piece of code
add_filter( 'woocommerce_package_rates', 'unset_shipping_below_free', 10, 2 );
function unset_shipping_below_free( $rates, $package ) {
$categories = array('PILLOWS'); // Defined targeted product categories
$threshold = 99; // Defined threshold amount
$cart = WC()->cart;
$cart_items = $cart->get_cart();
//How to sum the value of Pillows in the cart?
if ( $subtotal_pillows < $threshold ) {
if ( isset( $rates['free_shipping:4'] ) ) {
unset( $rates['free_shipping:4'] );
}
if ( isset( $rates['flexible_shipping_single:5'] ) ) {
unset( $rates['flexible_shipping_single:5'] );
}
}
return $rates;
}
But I can't figure out how to sum the value of the Pillows. Any help greatly appeciated
You should try this:
add_filter( 'woocommerce_package_rates', 'unset_shipping_below_free', 10, 2 );
function unset_shipping_below_free( $rates, $package ) {
$categories = array('pillows'); // Defined targeted product categories slug
$threshold = 99; // Defined threshold amount
$subtotal_pillows = 0;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$quantity = $cart_item['quantity'];
$line_subtotal = $cart_item['line_subtotal'];
$item_price = $line_subtotal / $quantity;
$subtotal_pillows = $subtotal_pillows+$item_price;
}
}
//echo $subtotal_pillows; exit;
//How to sum the value of Pillows in the cart?
if ( $subtotal_pillows < $threshold ) {
if ( isset( $rates['free_shipping:4'] ) ) {
unset( $rates['free_shipping:4'] );
}
if ( isset( $rates['flexible_shipping_single:5'] ) ) {
unset( $rates['flexible_shipping_single:5'] );
}
}
return $rates;
}
Note: $categories = array('pillows'); you should use category slug instead of category name for that you will got $subtotal_pillows properly category wise

Disable payment gateway when specific products in WooCommerce cart

I want to unset the payment method 'bacs' for the product ID 6197.
I have tried the code below, but this does not have the desired result.
add_filter( 'woocommerce_available_payment_gateways', 'wp_unset_gateway_by_id', 10, 2 );`
function wp_unset_gateway_by_id( $available_gateways, $products) {
global $woocommerce;
$unset = false;
$product_ids = array('6197');
if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
unset( $available_gateways['bacs'] );
return $available_gateways;
}
}
I believe I am doing something wrong, but I am relatively new to this. Any advice would be appreciated.
$products is not passed as argument to the woocommerce_available_payment_gateways filter hook
Apply it in the following way:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// The targeted product ids (several can be added, separated by a comma)
$targeted_ids = array( 6197 );
// Flag
$found = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
// True
if ( $found ) {
// Bacs
if ( isset( $payment_gateways['bacs'] ) ) {
unset( $payment_gateways['bacs'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

Woocommerce automatic add to cart: product id

So, I found this snippet for automatically adding a product when a user visits a product page:
/*
* Add item to cart on visit
*/
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $value
) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
add_action( 'init', 'add_product_to_cart' );
Now, it is for only product "id=64." How would I change it so that any products will be added.
Thank you

Woocommerce checkout page depending on product category

I am building on a website using woocommerce.
For some products clients need to write the name of their child in the Child name field on the checkout page. (The site sells music lessons)
However for other products like giftcards I dont need this Child name field. I can't find any plugin that can show a different checkout page depending on the catagory of the product that the client is buying.
Anyone an idea for making this possible?
Thnx in advance!
I think I found a website with the answer:
https://wordimpress.com/create-conditional-checkout-fields-woocommerce/
I'm going to try this and place the outcome here.
*** okay couple of hours later!
It did the job, the codes in the website I posted are used for a single product ID. If you want to check for category ID you can change this code:
/**
* Check if Conditional Product is In cart
*
* #param $product_id
*
* #return bool
*/
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$book_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//book is in cart!
$book_in_cart = true;
}
}
return $book_in_cart;
}
for:
/**
* Check if Conditional Product is In cart
*
* #param $product_id
*
* #return bool
*/
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$book_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if ( $_categoryid === 14 ) {
//book is in cart!
$book_in_cart = true;
}
}
return $book_in_cart;
}
If you need to check multiple categorie ID's or product ID's you can copy this excample:
/**
* Check if Conditional Product is In cart
*
* #param $product_id
*
* #return bool
*/
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$book_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if (( $_categoryid === 14 ) || ( $_categoryid === 16 )) {
//book is in cart!
$book_in_cart = true;
}
}
return $book_in_cart;
}
I Hope this post will hopefully save somebody a lot of time searching all the loose bits of information ;)
This worked better for me:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
function optional_default_address_fields( $address_fields ) {
$address_fields['postcode']['required'] = false;
$address_fields['city']['required'] = false;
$address_fields['state']['required'] = false;
$address_fields['address_1']['required'] = false;
$address_fields['country']['required'] = false;
$address_fields['billing_company']['required'] = false;
return $address_fields;
}
function custom_override_checkout_fields( $fields ) {
$categories = array('ajakirjad');
$foundAjakiri = false;
$foundOthers = false;
foreach ( WC()->cart->get_cart() as $cart_item ){
if(has_term( $categories, 'product_cat', $cart_item['product_id'] )) {
$foundAjakiri = true;
} else {
$foundOthers = true;
}
}
if($foundAjakiri == true && $foundOthers == false) {
// echo '1';
} elseif($foundAjakiri == false && $foundOthers == true) {
$fields['billing']['billing_address_1']['required'] = false;
$fields['billing']['billing_address_2']['required'] = false;
$fields['billing']['billing_city']['required'] = false;
$fields['billing']['billing_postcode']['required'] = false;
$fields['billing']['billing_state']['required'] = false;
$fields['billing']['billing_country']['required'] = false;
$fields['billing']['billing_country']['class'][] = 'no-need';
$fields['billing']['billing_company']['required'] = false;
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
// unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
// unset($fields['billing']['billing_phone']);
//unset($fields['order']['order_comments']);
}
//add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );
add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );
return $fields;
}

Resources