While working on a new website for a Swiss-based company that provides business solutions client had few requests that go outside basic Wordpress/WoCommerce. We have developed a new Wordpress theme for them, integrated Woocommerce and imported data. Client was happy with everything, but had a few requests. Each product they sell has a one-time set-up fee and recurring cost for each product payed per month per user.
WooCommere totals however calculate everything in the total and we needed it to be split into different types of costs. Client’s requirements were met in 3 steps.
One time set-up fees differ from product to product. They are also taxable so Woocommerce standard tax rate applies to them. To get this done right we installed Advanced Custom Fields plugin to add custom field to our products. Custom field is called “Pauschal” since the site is in German.
Once this was added we added the fees into our site by inserting this code to our theme’s functions.php file:
function one_time_setup_fee( $cart ) { $ima=0; $ total =0; $ids=""; foreach(WC()->cart->get_cart() as $cart_item) { $id = $cart_item['data']->post->ID; if(intval(get_field("pauschal", $id))>0){ $ima=1; $total+=intval(get_field("pauschal", $id)); } } if($ima){ $rezpo= reset( WC_Tax::get_rates() )['rate']; $cart->add_fee( __( 'Einrichtungs Pauschale', 'pauschale-domain' ) , number_format($total, 2, ".", ",") ); $cart->add_fee( __( 'Einrichtungs Pauschale MWST', 'pauschale-domain' ) , number_format($total * $rezpo * 0.01, 2, ".", ",")); } } add_action( 'woocommerce_cart_calculate_fees', 'one_time_setup_fee' );
This code is executed on cart, checkout and order details pages. We fetch everything in cart then loop each item to check if it has a defined set-up fee (clients may, at some point, have products that don’t have any set-up fees). If it does have a fee defined then a flag ($ima) is set and total set-up fee is calculated. Then the $cart->add_fee code adds the two fees (Set-up fee and a Set-up fee tax).
This is fine so far, but these fees are calculated in total amount, but total should represent only the amount that’s payed per user per month, without these one-time fees.
To fix this we need to recalculate the totals and subtract the fees from total amount. We used this code (placed in functions.php file):
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 ); function custom_calculated_total( $total, $cart ){ $ima=0; $fees=0; $ids=""; foreach(WC()->cart->get_cart() as $cart_item) { $id = $cart_item['data']->post->ID; if(intval(get_field("pauschal", $id))>0){ $ima=1; $fees +=intval(get_field("pauschal", $id)); } } if($ima){ $rezpo= reset( WC_Tax::get_rates() )['rate']; return round( $total - $fees * (1 + $rezpo * 0.01), $cart->dp ); } else return $total; }
This code recalculates total – it fetches each item in cart, calculates total one-time set-up fees and fetches the standard tax rate and if any fees exist returns new total that is old total minus fees multiplied by a (1 + tax_rate), where tax rate should be converted from percent decimal, thus multiplication by 0.01 – this could be also divided by 100, so instead * 0.01 it could go /100.
Total text could be changed using a plugin like Loco Translate, but instead of installing a whole plugin in order to change one label we changed the text in functions.php file.
add_filter('gettext', 'wc_renaming_checkout_total', 20, 3); function wc_renaming_checkout_total( $translated_text, $untranslated_text, $domain ) { if( !is_admin() && is_checkout ) { if( $untranslated_text == 'Total' ) $translated_text = __( 'Total (pro Benutzer pro Monat)','theme_slug_domain' ); } return $translated_text; } //change total label in e-mail add_filter( 'woocommerce_get_order_item_totals', 'renaming_shipping_order_item_totals', 10, 3 ); function renaming_shipping_order_item_totals( $total_rows, $order, $tax_display ){ // Only on emails notifications if( ! is_wc_endpoint_url() ) $total_rows['order_total']['label'] = __('Total (pro Benutzer pro Monat)', 'woocommerce'); return $total_rows; }
We used two functions because first one changes the text in cart, checkout and order details on site and second is used to change the label in e-mails sent to clients. So, if statement checks if the text is equal to “Total” and changes it to “Total (per user per month)”, only in German.
Kategorije: wordpress, woocommerce
Ključne riječi: wordpress, woocommerce, fees, checkout, cart, change total