Woocommerce Carriage Surcharge - wordpress

so I found some code via
Global surcharge WooCommerce exclude VAT
I've modified this to suit my requirement:
// Packing fee
add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' );
function wc_add_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/** Declare variable for surcharge custom field state **/
$surcharge_status = get_field('surcharge');
/** Assign surcharge_fee to fee **/
$fee = $surcharge_status;
$cart->add_fee( 'Surcharge', $fee );
}
I'm trying to modify the above to take a value (surcharge) from the product (custom field, as a select value - in this case referenced by get_field) then add that as a fee to the cart. For some reason, it doesn't recognise the surcharge code above and displays '0.00'. Is there any way around this.

Related

Price in the shopping cart [duplicate]

I am trying to change product price in cart using the following function:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+
How can I make it work in WooCommerce Version 3.0+?
Thanks.
Update 2021 (Handling mini cart custom item price)
With WooCommerce version 3.0+ you need:
To use woocommerce_before_calculate_totals hook instead.
To use WC_Cart get_cart() method instead
To use WC_product set_price() method instead
Here is the code:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
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 ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update):
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme).
This code is tested and works (still works on WooCommerce 5.1.x).
Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.
Related:
Set cart item price from a hidden input field custom price in Woocommerce 3
Change cart item prices based on custom cart item data in Woocommerce
Set a specific product price conditionally on Woocommerce single product page & cart
Add a select field that will change price in Woocommerce simple products
With WooCommerce version 3.2.6, #LoicTheAztec's answer works for me if I increase the priority to 1000.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.

WooCommerce: How do I show a notice on product page if a WC session coupon exists?

Bit of a newbie... I have a survey and on submission, I am generating a dynamic coupon code to incentivize purchase of a product. When user adds a product to the cart, the discount code is applied successfully, however I would like to display a notice on the product page indicating that (because the user took the survey), they will receive a discount applied at checkout.
Code for adding the product to cart:
// Add generated coupon to cart
add_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
if ( is_cart() || is_checkout() ) return;
// Load coupon code
$coupon_code = WC()->session->get( 'unique_coupon' );
if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->apply_coupon( $coupon_code ); // Apply coupon code
WC()->session->__unset( 'unique_coupon' ); // remove session variable
}
}
One of the many ways I've tried to add a notice:
add_action( 'woocommerce_before_single_product', 'custom_message_before_single_product', 5 ); // For single product page
function custom_message_before_single_product() {
if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) )
wc_print_notice(sprintf('You will receive a discount at checkout!', $coupon_code), 'notice');
}
I also tried product-based, but the notice appears regardless of whether a coupon has been created:
add_action( 'woocommerce_before_single_product', 'custom_message_before_single_product', 5 ); // For single product page
function custom_message_before_single_product() {
global $product;
if ( $product->get_id() == 1218 && $coupon_code = WC()->session->get( 'unique_coupon' );) {
wc_print_notice(sprintf('You will receive a discount at checkout!', $coupon_code), 'notice');
} }
Of course I'm basing this off the belief that because a session-based coupon was created, it's possible to grab that coupon, or recognize it has been created and display a notice prior to it being added to the cart but perhaps it's not possible...?

WooCommerce: conditional handling fee that is not applied to only one coupon code

I have implemented code that will charge a $1.50 handling fee to all WooCommerce products unless a coupon code is used. However, I only want the handling fee to be removed for a single coupon as opposed to when any coupon is used. Thoughts? Below is my current code and the only coupon that should not have the handling fee applied is QFPZ8KSS (the post ID for the coupon is 4432 if that is required).
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$applied_coupons_count = count($applied_coupons_arr);
$fee = 1.50;
if( 0 == $applied_coupons_count )
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
}
You just need to compare the coupon used in your order against your free shipping one. I think the below should work, bear in mind I haven't tested this. Hopefully it will help.
<?php
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$fee = 1.50;
$applied_coupon_ids;
// Coupons used in the order loop
foreach( $applied_coupons_arr as $coupon ){
// Get Coupon ID
$coupon_post_obj = get_page_by_title($coupon, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
// Store in array
array_push($applied_coupon_ids, $coupon_id);
};
// Check if your free shipping coupon is used
if(! in_array('COUPON_ID_HERE', $applied_coupon_ids)){
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
};
?>

woocommerce additional fees based on product input

The products contain 2 different Attributes (packaging= crate/container) and I have to put 3 different fee and name for 2 different attributes . The fee also need Multiply Fee by Quantity and display name & fee 1 by 1 on cart page and checkout page. In addition, the fee on cart page and checkout page will auto calculate to the total price . Any plugin or add the code for this problem? thank you.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
Here is a sample code on how to add fee

woocommerce exclude state free shipping (australian states)

I'm trying to implement woocommerces exclude states from free shipping (Australian Site) snippet but am running into this error:
'Fatal error: Call to a member function get_shipping_state() on a non-object in
/home/bpcsport/public_html/wp-content/plugins/wa-exclude/wa-exclude.php
on line 30'
My client needs to exclude Western Australia from the free-shipping deal they offer through woocommerce. I get the error either way if I put it in my themes function.php or via plug-in format.
I've also tried the following method to no avail.
How can I disable free shipping for particular states?
This is the snippet from woocommerce that is in the plug-in
/**
* Hide ALL shipping options when free shipping is available and customer is NOT in certain states
* Hide Free Shipping if customer IS in those states
*
* Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping
*/
add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );
/**
* Hide ALL Shipping option when free shipping is available
*
* #param array $available_methods
*/
function hide_all_shipping_when_free_is_available( $available_methods ) {
$excluded_states = array( 'WA' );
if( isset( $available_methods['free_shipping'] ) AND !in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) :
// Get Free Shipping array into a new array
$freeshipping = array();
$freeshipping = $available_methods['free_shipping'];
// Empty the $available_methods array
unset( $available_methods );
// Add Free Shipping back into $avaialble_methods
$available_methods = array();
$available_methods[] = $freeshipping;
endif;
if( isset( $available_methods['free_shipping'] ) AND in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) {
// remove free shipping option
unset( $available_methods['free_shipping'] );
}
return $available_methods;
}
?>
I've made an plugin which allow advanced user configurations. Altough it includes only US states, you can configure country and postal code conditions.
I don't know if this meets you requirements, but might worth checking out: http://wordpress.org/plugins/woocommerce-advanced-free-shipping/

Resources