How to Program Custom Fees
Sometimes you have to setup complex fees which can’t be done via UI.
Here is sample code which adds 2 different fees to the cart:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//This code runs after ADP processed WooCommerce cart add_action( 'wdp_after_apply_to_wc_cart', function ( $wdp_cart ) { $wc_cart = WC()->cart; $totals = $wc_cart->get_totals(); //add fees for each matched item if order total <150 if ( isset( $totals['total'] ) && $totals['total'] < 150.00 ) { foreach ( $wc_cart->get_cart_contents() as $item_key => $cart_content ) { // check item meta keys added by external plugin YITH Product Addons if ( isset( $cart_content['yith_wapo_options'] ) ) { foreach ( $cart_content['yith_wapo_options'] as $option ) { //fee for some values only if ( "Upload Logo" === $option['name'] && ( "Upload file now" === $option['value'] || "Add logo later" === $option['value'] ) ) { WDP_Functions::add_fee( __( 'Total fee' ), 15 ); } } } } } //add fees for each item if qty <8 and item belongs to category, category ID - hardcoded! foreach ( $wc_cart->get_cart_contents() as $item_key => $cart_content ) { $product = $cart_content['data']; $parent = wc_get_product($cart_content['product_id']); /** * @var $product WC_Product * @var $parent WC_Product */ if ( isset( $cart_content['quantity'] ) && $cart_content['quantity'] < 8 && in_array( 255, $parent->get_category_ids() ) ) // Clothing { WDP_Functions::add_fee( __( 'Small embroidery run fee' ), 25 ); } } }); |