Woocommerce购物车计算费用问题

时间:2017-06-13 10:57:50

标签: wordpress woocommerce

我创建了钩子动作

  do_action('wpyoug_update_price');

  add_action('wpyoug_update_price', 'wpyoug_update_woocommerce_price');
  function wpyoug_update_woocommerce_price(){
     global $totalshipCharge;
     add_action( 'woocommerce_cart_calculate_fees', 'cp_add_custom_price' );    
  }

当我在wpyoug_update_woocommerce_price()函数内部回显时,它返回。但是add_action('woocommerce_cart_calculate_fees','cp_add_custom_price');不工作

 function cp_add_custom_price(){
     global $woocommerce;
     $thiscarttotal = WC()->cart->get_cart_total();
     $thiscarttotal = preg_replace('/$/', '', $thiscarttotal);
      $woocommerce->cart->add_fee( __('Miscellaneous Handling Fee', 'woocommerce'), 10 );
      }

2 个答案:

答案 0 :(得分:0)

// Hook before adding fees 
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');

function add_custom_fees( WC_Cart $cart ){
    $fees = 0.08;
    $cart->add_fee( 'Handling fee', $fees);
}

答案 1 :(得分:0)

add_action( 'woocommerce_cart_calculate_fees','xa_add_surcharge' );
function xa_add_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $surcharge = 5; 
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

From here