In woocommerce, I am trying to add a fee using this answer code:
Update fee dynamically based on radio buttons in Woocommerce checkout (credits: LoicTheAztec)
I need to update the code to add third conditional option to the fee like "ribbon" in this hooked function:
// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
$fee = $packing_fee == 'box' ? 9.00 : 3.00;
$cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );
}
But I am stuck trying to add another entry with this line of code:
$fee = $packing_fee == 'box' ? 9.00 : 3.00;
How can I add an additional fee option?
Related
I'd like to add an accordion dropdown to my customer's "fee" at checkout. I'm using the code below but can't add "disclaimer" text to this. I'd love to be able to toggle this field so the customers have fewer questions. Do I need to add a hook a paragraph tag below the surcharge / fee? I'm new to woocommerce hooks.
function wc_add_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('US');
// change the $fee to set the surcharge to a value to suit
$fee = 3.00;
if ( in_array( WC()->customer->get_shipping_country(), $county ) ) :
$woocommerce->cart->add_fee( 'Name of Fee', $fee, true, 'standard' );
endif;
}
All you need to do is go to "review-order.php" and there is a $fee div. You can any custom code here and it will display in the "fee" section of checkout.
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.
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' );
};
?>
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
I'm building a WooCommerce e-shop and I need to tweak my checkout page by doing the following:
Hide a certain shipping method (only one) if the order total > 100€.
Hide the cash on delivery payment method if local pickup is selected.
Does anyone know how to do that? I have the Code Snippets plugin so I can easily add any custom code.
To hide specific shipping method based on the cart total, you can use below code snippet. You need to update your shipping method name in the code.
Disable shipping method as per cart total
Add this snippet in your theme's functions.php file or custom plugin file.
add_filter( 'woocommerce_package_rates', 'shipping_based_on_price', 10, 2 );
function shipping_based_on_price( $rates, $package ) {
$total = WC()->cart->cart_contents_total;
//echo $total;
if ( $total > 100 ) {
unset( $rates['local_delivery'] ); // Unset your shipping method
}
return $rates;
}
Disable Payment Gateway For Specific Shipping Method
Use below code snippet. Update code as per your payment method & shipping method.
add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );
function x34fg_gateway_disable_shipping( $available_gateways ) {
global $woocommerce;
if ( !is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
There are a number of plugins that will do this for you, take a look at this one WooCommerce Conditional Shipping and Payments
You'll want to tie in to the "woocommerce_payment_gateways" action
Something along these lines:
function alter_payment_gateways( $gateways ){
$chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if( in_array( 'local-pickup:6', $chosen_rates ) ) {
$array_diff = array('cod');
$list = array_diff( $list, $array_diff );
}
return $list;
}
add_action('woocommerce_payment_gateways', 'alter_payment_gateways', 50, 1);
The number on the end of 'local-pickup' on line 4 will depend on your woocommerce setup. You can find the string you need to put in here by adding something to a basket, going to the checkout, right clicking on the "Local Pickup" option in the delivery methods and looking at the value attribute.