Berikut ini adalah source code untuk menambahkan Custom diskon menggunakan php pada woocommerce, disini saya mengambil data diskon dari data option yg sudah saya buat sebelumnya.
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 36 37 38 39 40 41 42 |
<?php class Modify_Price { public function __construct() { add_action( 'woocommerce_cart_calculate_fees', array($this,'lima_price_after_calculate_totals') ); } function lima_price_after_calculate_totals( $cart ) { global $woocommerce; $user = wp_get_current_user(); $user_role = $user->roles[0]; $diskon = 0; if( $user_role == 'dropshiper' ){ $option = get_option('dropshiper_setting'); $diskon = $option["dropship_discount"]; }elseif( $user_role == 'reseller' ){ $option = get_option('reseller_setting'); $diskon = $option["reseller_discount"]; }elseif( $user_role == 'agen' ){ $option = get_option('agen_setting'); $diskon = $option["agen_discount"]; }elseif( $user_role == 'distributor' ){ $option = get_option('distributor_setting'); $diskon = $option["distributor_discount"]; } if($diskon != 0){ $cart_total = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); $discount = ($cart_total * $diskon )/100; $cart->add_fee( 'Discount '.$diskon.'%', -$discount); } } } $limaPrice = new Modify_Price; |