Tas*_*sos 5 php wordpress cart woocommerce fee
我尝试根据我在 Woocommerce 购物车上所做的一些计算来增加费用,但我想将其从增值税中排除。这是我的代码:
function woo_add_cart_fee( $cart ) {
    global $woocommerce; $bookable_total = 0; 
    foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
        $_product = $values['data'];
        //doing my stuff to calculate $fee variable
    WC()->cart->add_fee( 'Fees: ', $fee, false, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, false, 'zero rate' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有评论版本,每个版本也都包含增值税。
知道我如何实现它吗?
(更新):使用add_fee() 方法的税收选项
\n\n\n重要提示: TAX是否有效的事实
\nadd_fee()首先取决于您在 woocommerce 中的税务设置。由于您没有在问题中说明您的税务设置是什么,因此不可能为您提供帮助(每个电子商务网站的税务设置可能会有很大不同)。
特殊情况:当使用添加折扣(负金额)add_fee()的方法时,始终应用税费。 
例如,如果您想使用“零税率”税级,但尚未为客户所在国家/地区定义正确的“零税率”税级,则如果您尝试将其与以下方式一起使用,则这将不起作用:
\n WC()->cart->add_fee( \'Fees: \', $fee, true, \'zero rate\' );\n\xe2\x80\xa6取决于您的全局税务设置。
以下是购物车中 3 件商品的真实结账总额的屏幕截图(使用下面的代码):
\n\n\n\nWC_Cart 类 add_fee() 方法,向购物车添加额外费用。
\n
\n\nRun Code Online (Sandbox Code Playgroud)\nadd_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = \'\' )\n
\n\n
\n\nRun Code Online (Sandbox Code Playgroud)\nParameters:\n
    $name      Unique name for the fee. Multiple fees of the same name cannot be added.\n    $amount    Fee amount.\n    $taxable   (default: false) Is the fee taxable?\n    $tax_class    (default: \'\') The tax class for the fee if taxable. A blank string is standard tax class.\nRun Code Online (Sandbox Code Playgroud)\n原始答案 (更新代码):
\n\n\n您的主要问题是在这一行:
\nglobal $woocommerce, $bookable_total = 0;
\n\n\n
\n- 由于您使用的是
 \nWC()->cart语法而不是$woocommerce->cart语法,因此您实际上不需要global $woocommerce;.- 如果你使用
 \nglobal $bookable_total = 0;这个$bookable_total将永远= 0。
\n相反,您将使用不带值的方式来获取函数外部定义的值。\n但是如果你想将值设置为零,以防在你的函数之外没有定义,你可以这样做:global $bookable_total;woo_add_cart_fee( $bookable_total=0 )
我们现在可以在函数外部定义$bookable_total变量值。
这是您的代码的一个工作示例:
\n// This variable value is passed to our function\n$bookable_total = 1;\n\nfunction woo_add_cart_fee( $bookable_total = 0 ) {\n    global $bookable_total;\n\n    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )\n        return;\n\n    // just for this example\n    $item_count = 0;\n    $item_fee = 5;\n\n    // going through each cart items\n    foreach( WC()->cart->get_cart() as $values ) {\n        $item = $values[\'data\'];\n        if ( empty( $item ) )\n            break;\n        // getting the cart item_id\n        $item_id = $item->id;\n        $item_count++;\n        // your calculations\n    }\n\n    // We test $bookable_total value, defined to \'1\' outside our function\n    // and to \'O\' if not defined outside (in this case the fee will be \'0\')\n    $fee = $item_count * $bookable_total * $item_fee;\n\n    // add_fee method (TAX will NOT be applied here)\n    WC()->cart->add_fee( \'Fees: \', $fee, false );\n\n}\nadd_action( \'woocommerce_cart_calculate_fees\',\'woo_add_cart_fee\' );\nRun Code Online (Sandbox Code Playgroud)\n这段代码已经过测试并且可以工作。 它位于活动子主题或主题的 function.php 文件中。
\n如果该$bookable_total变量未在外部定义,则该值将为0.
\n\n注意:最好通过以下方式获取 $items id:
\n$item = $values[\'data\']; $item_id = $item->id;
参考:
 \n WC_Cart 类 -add_fee( $name, $amount, $taxable = false, $tax_class = \'\' )方法
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           9733 次  |  
        
|   最近记录:  |