Wordpress 计算费用问题

Wordpress 计算费用问题,wordpress,woocommerce,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_

我创造了钩子动作

  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()函数中回显时,它返回。但是添加操作(“woocommerce\u cart\u calculate\u fees”、“cp\u add\u custom\u 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 );
      }

您是否希望在任何特定页面中使用此cp\u add\u custom\u price函数是的,我想更新price只需在该函数上添加页面条件,因为您使用它的方式是正确的。请输入代码。你想要什么say@Sudhir您的购物车费用添加功能不正确,请尝试以下功能answer@David我输入了完全相同的代码。但它没有返回任何内容。我已经将此代码放在了短代码中。这是背后的原因吗it@Sudhir只需将此代码放入活动子主题的functions.php中,即可添加surcharge@Sudhir您不能使用该内部短代码。只要把它放在活动主题的functions.php和tryYes中,我同意你的看法。但我想在form POST方法之后在某些条件下运行它。那么我该如何使用它呢?我已经输入了相同的代码。但它不起作用。我在短代码中使用此代码。这是不工作的原因吗?您只需在这个函数中添加if条件和post数据
// 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);
}
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, '' );

}