WordPress free shipping if country and payment gateway - wordpress

I am having a small problem trying to set up free shipping in latest Woocommerce (2.6.x) if certain conditions are met.
Customer wants to set a free shipping if "cash on delivery" is chosen, price is over X€ and certain country is chosen.
I was trying a lot of snippets I found, but nothing seems to work. I don't wish to use too many plugins (already have quite a few).
I need something like:
if ($country == 'DE' && $cart_total >= 100 && $payment_gateway == 'cod' ) {
$shipping == 0;
}
Thank you for all the answers in advance,
Jure

I am using Table Rate Shipping for WordPress and I found a solution to my question. There is missing check for payment method, but that should not be that hard to find. Just in case somebody else finds it useful.
add_filter( 'woocommerce_package_rates', 'sl_custom_shipping_rates', 10, 2 );
function sl_custom_shipping_rates( $rates, $package ) {
foreach( $rates as $key => $value ) {
if( $value->method_id == 'table_rate_shipping' && WC()->cart->cart_contents_total >= 100 && WC()->customer->get_shipping_country() == "DE") {
$rates[ $key ]->cost = 100;
}
}
return $rates;
}

Related

Add custom product option to woocommerce orders list

I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.

Hide shipping method working with Innozilla Per Product Shipping WooCommerce plugin

Hi all :) I understand that there are a lot of such topics, but, unfortunately, I cannot apply any code, and I do not know how to redo and apply it.
I added an additional shipping method for specific countries using the Innozilla Per Product Shipping WooCommerce plugin for a specific product
when selecting a specific country, I see old shipping methods like flexible_shipping_9_2, flat_rate: 12 and + new "per_product" shipping method
I would like to hide all delivery methods except per_product when the per_product delivery method is used.
How to do it? Thank you in advance.
add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = 15; // shipping class ID
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['free_shipping:8'] ); // shipping method with ID
}
return $rates;
}

How disable local pickup, when cart weight = 0 in WordPress

pls how can I disable local pickup depending on a cart total weight? I want to disable possibility for local pickup when the cart total weight == 0. I used this snippet found elsewhere and modified, but it is not working and the local pickup is still offered
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 9999, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
if ( WC()->cart->get_cart_contents_weight() == 0 ) {
unset( $rates['local_pickup'] );
unset( $rates['local_delivery'] );
}
return $rates;
}
What am I doing wrong please?
Actually my code mentioned above is working. I did not realized two things:
ID of my local_pickup was wrong, I did not explore the page to find out, that the correct variable name is 'local_pickup:13' (code updated)
To see changes on the web I always have to open the site in anonymous window. Even Ctrl+F5, Ctrl+Shift+R in Chrome does not help.
You can count total weight by WC()->cart->cart_contents loop.
try this code
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_weight', 10, 2 );
function hide_shipping_based_on_weight( $rates, $package ) {
$cart_total_weight = 0;
$shipping_id = 'local_pickup'; // your shipping id
// Calculate cart weight total.
foreach( WC()->cart->cart_contents as $key => $value ){
$cart_total_weight += $value['data']->weight * $value['quantity'];
}
// check cart total
if( $cart_total_weight <= 0 ){
unset( $rates[ $shipping_id ] );
}
return $rates;
}

WooCommerce coupon calculate from regular price

I need functionality for "friend" customers, who can get products with better price. User role based solution is not what I'm looking for and customer wants it to work with coupons. They give coupon code to their customers and they get for example 8€ off from REGULAR price on certain products. The problem is, by default WooCommerce coupons calculate it from smallest price. If sale price is set, then this calculation is wrong and the customer gets it too cheap. Basically, I want coupon to give certain products fixed "friend" price.
I've been googling around and can't find a ready solution for it. Any suggestions how to solve this problem are welcome.
I have checked multiple plugins, but none of them has filled my requirements.
Add to functions.php. Based off of this code https://brilliantinfo.net/apply-coupon-discount-on-regular-price-in-woocommerce/
Modified for coupon type of fixed_product. If you need this to only work for a specific coupon then I can modify for that. (As it stands this will apply to any fixed_product coupons that are used)
add_action( 'woocommerce_before_calculate_totals', 'adjust_cart_coupon', 10, 1);
function adjust_cart_coupon( $cart_object) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
$coupon = False;
if ($coupons = WC()->cart->get_applied_coupons() == False ) {
$coupon = False;
} else {
foreach ( WC()->cart->get_applied_coupons() as $code ) {
$coupons1 = new WC_Coupon( $code );
if ($coupons1->type == 'fixed_product'){
$coupon = True;
}
}
}
if ($coupon == True){
foreach ( $cart_object->get_cart() as $cart_item ){
$price = $cart_item['data']->regular_price;
//sets cart item to use regular price and not sale price
$cart_item['data']->set_price( $price );
}
}
}
The Discount Rules for WooCommerce plugin might fit your requirements.
With it you can apply a coupon, or any discount rule, to specific customers.
Also there is a setting to Apply discount based on Sale price or Regular price.

Change shipping cost when buyer buy specific amount price

I am building an eCommerce website by WordPress and stuck in one place. I want to set a function on my website. When buyer buys $30+ product then I want to make shipping cost zero. So I cannot understand how to do it. Please, someone, help me.
Something like this should do the trick.
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
if ( WC()->cart->subtotal > 30 ) {
foreach($rates as $key => $rate ) {
$rates[$key]->cost = 0;
}
}
return $rates;
}
You can find more examples about shipping fee here:
http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/

Resources