问题描述
我对使用钩子片段自定义 Woocommerce 还很陌生。我正在尝试自定义计算 Woocommerce 网站运费和税费的方法:数学逻辑有效,因此可以使用商品成本、运费和商品税费。
我不清楚的是:
到目前为止,我已经在我的主题的functions.PHP 文件中添加了这个片段,它基于每个购物车产品,计算其新的税收成本。
add_filter('woocommerce_shipping_packages','override_woocommerce_shipping_packages');
function override_woocommerce_shipping_packages($packages) {
$products_subtotal = Array();
$products_shiptax = Array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$subtotal = WC()->cart->get_product_subtotal( $product,$cart_item['quantity'] );
$products_subtotal[$cart_item_key] = floatval( preg_replace( '#[^\d.]#','',$subtotal) )/100;
$products_shiptax[$cart_item_key] = $product->get_tax_class();
//based on the product's tax class
if($products_shiptax[$cart_item_key]=="vat-20"){
// HERE I HAVE TO OVERRIDE ITS TAX VALUE
}else{
// HERE I HAVE TO OVERRIDE ITS TAX VALUE
}
}
return $packages;
}
从 WC 代码参考页面,我看到许多“set_taxes”或“set_tax_total”函数。但是很难理解我必须在哪个钩子上调用这些函数。
你有什么解释和例子吗?非常感谢您的回答!
WC 5.1.0 版
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)