【发布时间】:2022-01-28 13:01:03
【问题描述】:
我遇到了问题。在 Woocommerce 中,我添加了一个自定义产品类型(abo),用户可以在其中选择面积大小(以平方米为单位),并有每平方米的价格和剂量信息,例如“每平方米需要 0.165 公斤”(这是一种肥料)。
例如,如果用户选择 100 平方米,每平方米的价格为 2.30 欧元,剂量为 0.165 公斤/平方米,则 abo 的总价格为 37.95 欧元,税率为 10.7% 的税额应该是 4.06 欧元。
我现在的问题是,我店里也有正常的产品,价格和税收等的标准计算工作得很好,你可以买到abo和正常的产品。
所以我的问题是,如果我在购物车中有一个 abo,我该如何重新计算税额?
到目前为止,我尝试了一些测试方法:
添加自定义费用(这不是我想要的,因为购物车、结帐和收据上的税费必须相同):
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if(current_user_can('administrator')) :
$percent = 10.7;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
endif;
}
calc_tax 函数(我认为这是我需要的):
/ define the woocommerce_calc_tax callback
function filter_woocommerce_calc_tax( $taxes, $price, $rates, $price_includes_tax, $suppress_rounding ) {
if(current_user_can('administrator')) :
foreach(WC()->cart->cart_contents as $item) :
if($item['data']->get_type() == 'abo') :
$qty = $item['quantity'];
$ppqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_price_per_kg')[0]);
$kgpqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_kg_qm')[0]);
$realQty = floatval($kgpqm) * floatval($qty);
$priceCustom = $priceCustom + $realQty * floatval($ppqm);
endif;
endforeach;
return $taxes;
};
// add the filter
add_filter( 'woocommerce_calc_tax', 'filter_woocommerce_calc_tax', 10, 5 );
使用上述功能,我遇到了问题,我没有直接拥有购物车内容,因此可能是物品的顺序与我从该功能获得的税单不同。 我还要注意,woocommerce_calc_tax 会触发两次。出于调试原因,我在控制台中有一个输出,如果我的购物车中有 2 件商品,我有 4 个输出。
希望我的问题很清楚,并且任何人都可以帮助我解决这个问题。
提前致谢。
【问题讨论】:
标签: php wordpress woocommerce