php - WooCommerce dynamic minimum order amount based fee -
i need set minimum order fee in shopping cart if products in cart don't total more £10 fee applied bring price £10.
here code have @ moment works in cart phase when reach checkout pricing section doesn't stop loading reason , can't checkout, can help?
code functions.php:
function woocommerce_custom_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'doing_ajax' ) ) return; $minimumprice = 10; $currentprice = $woocommerce->cart->cart_contents_total; $additionalfee = $minimumprice - $currentprice; if ( $additionalfee >= 0 ) { wc_print_notice( sprintf( 'we have minimum %s per order. current order %s, additional fee applied @ checkout.' , wc_price( $minimumprice ), wc_price( $currentprice ) ), 'error' ); $woocommerce->cart->add_fee( 'minimum order adjustment', $additionalfee, true, '' ); } } add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
the infinite loading spin issue facing due wc_print_notice()
when it's used in woocommerce_cart_calculate_fees
hook. it's seems bug.
if use instead wc_add_notice()
, problem gone but notice displayed 2 times.
additionally have revisited code.the only solution split in 2 separated functions:
// notice in cart page add_action( 'woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1 ); function add_custom_surcharge( $cart_object ) { if ( is_admin() && ! defined( 'doing_ajax' ) ) return; $minimumprice = 100; $currentprice = $cart_object->cart_contents_total; $additionalfee = $minimumprice - $currentprice; if ( $additionalfee >= 0 ) { $cart_object->add_fee( 'minimum order adjustment', $additionalfee, true ); if( ! is_checkout() ){ $message = sprintf( __('we have minimum %s per order. current order %s, additional fee applied.', 'woocommerce'), wc_price( $minimumprice ), wc_price( $currentprice ) ); wc_print_notice( $message, 'error' ); } } } // notice in checkout page add_action( 'woocommerce_before_checkout_form', 'custom_surcharge_message', 10, 0 ); function custom_surcharge_message( ) { $cart_object = wc()->cart; $minimumprice = 100; $currentprice = $cart_object->cart_contents_total; $additionalfee = $minimumprice - $currentprice; if ( $additionalfee >= 0 ) { $message = sprintf( __('we have minimum %s per order. current order %s, additional fee applied.', 'woocommerce'), wc_price( $minimumprice ), wc_price( $currentprice ) ); wc_print_notice( $message, 'error' ); } }
code goes in function.php file of active child theme (or theme) or in plugin file.
tested , works in woocommerce 3+
Comments
Post a Comment