- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 WooCommerce 中获取每个订单商品的税务标签。
例如:2x 产品 1 - 19 % MwSt. (税)4x 产品 2 - 19 % MwSt. (税)1x 产品 2 - 19 % MwSt. (税)
因此,我在 woocommerce 设置中添加了“19 %(税)”作为标准值和“7 %(税)”作为减税。我还设置了我想在 WooCommerce 中“免税”显示和输入价格。
现在我用这两个项目(数字和可下载的)下了一个订单,然后我尝试获取每个订单行项目的税值。
$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('line_item')) as $item_id => $line_item) {
$order_product_detail = $line_item->get_data();
$item_tax_class = $order_product_detail['tax_class'];
$item_subtotal_tax = $order_product_detail['subtotal_tax'];
$item_total_tax = $order_product_detail['total_tax'];
$item_taxes_array = $order_product_detail['taxes'];
var_dump($item_tax_class);
var_dump($item_subtotal_tax);
var_dump($item_total_tax);
var_dump($item_taxes_array);
}
这是输出:
string(0) "" string(4) "0.76" string(6) "0.2185" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.2185" } ["subtotal"]=> array(1) { [1]=> string(4) "0.76" } } string(0) "" string(4) "0.38" string(6) "0.2451" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.2451" } ["subtotal"]=> array(1) { [1]=> string(4) "0.38" } } string(0) "" string(4) "2.85" string(5) "2.166" array(2) { ["total"]=> array(1) { [1]=> string(5) "2.166" } ["subtotal"]=> array(1) { [1]=> string(4) "2.85" } } string(0) "" string(4) "0.76" string(6) "0.6251" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.6251" } ["subtotal"]=> array(1) { [1]=> string(4) "0.76" } } string(0) "" string(4) "0.95" string(6) "0.8151" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.8151" } ["subtotal"]=> array(1) { [1]=> string(4) "0.95" } } string(0) "" string(4) "1.14" string(6) "1.0051" array(2) { ["total"]=> array(1) { [1]=> string(6) "1.0051" } ["subtotal"]=> array(1) { [1]=> string(4) "1.14" } } string(0) "" string(4) "1.52" string(6) "1.3851" array(2) { ["total"]=> array(1) { [1]=> string(6) "1.3851" } ["subtotal"]=> array(1) { [1]=> string(4) "1.52" } }
因为这没有给我每个订单项的税费标签,所以我尝试了以下操作:
$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('line_item')) as $item_id => $line_item) {
$order_item_tax = new WC_Order_Item_Tax($item_id);
var_dump($order_item_tax->get_label());
}
这是输出:
string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer"
但它应该是“19 % MwSt”。
所以我尝试了这个:
$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('tax')) as $item_id => $line_item) {
var_dump($line_item->get_label());
}
这个输出:
string(10) "19 % MwSt."
是的,这就是我需要的值,但为什么它只输出一次这个值,因为我的订单中有七件商品?
是否有机会获得每个订单行项目的税务标签,或者在 WooCommerce 中不可能?我的目的是创建一个发票程序,但我必须考虑每行是否有不同的税率。
WooCommerce 版本是 3.4.2
提前致谢
最佳答案
以下代码自 Woocommerce 版本 3 起有效。
它将允许您获得税务标签,因为您可以在一个订单中添加多个,具体取决于您的税务设置、产品设置和运输设置。
// Get an instance of the WC_order object
$order = wc_get_order(143);
// Initializing variables
$tax_items_labels = array(); // The tax labels by $rate Ids
$shipping_tax_label = ''; // The shipping tax label
// 1. Loop through order tax items
foreach ( $order->get_items('tax') as $tax_item ) {
// Set the tax labels by rate ID in an array
$tax_items_labels[$tax_item->get_rate_id()] = $tax_item->get_label();
// Get the tax label used for shipping (if needed)
if( ! empty($tax_item->get_shipping_tax_total()) )
$shipping_tax_label = $tax_item->get_label();
}
// 2. Loop through order line items and get the tax label
foreach ( $order->get_items() as $item_id => $item ) {
$taxes = $item->get_taxes();
// Loop through taxes array to get the right label
foreach( $taxes['subtotal'] as $rate_id => $tax ){
$tax_label = $tax_items_labels[$rate_id]; // <== Here the line item tax label
// Test output line items tax label
echo '<pre>Item Id: '.$item_id.' | '; print_r($tax_label); echo '</pre>';
}
}
// Test output shipping tax label
echo '<pre>Shipping tax label: '; print_r($shipping_tax_label); echo '</pre>';
在 Woocommerce 版本 3.4.2 中测试
关于php - 在 Woocommerce 3 中获取每个订单商品的税标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740277/
我刚开始使用 jQuery,所以我还是个新手。有人可以帮我计算动态表中的新价格吗?我有一个包含 3 列的动态表,数量、折扣和价格。用户在当前价格、小计和总计所在的同一文本框中输入折扣 (%) 后,我需
因为一切都应该是 https,所以我启用了它,并注意到 https 与 http 相比要慢得多。 我在达拉斯有一台 Ubuntu/YAWS 服务器。我使用“yaws --daemon --nodebu
我有一个很奇怪的问题,我希望有人能帮我解决这个问题。 以下是影响我的问题的主要配置设置: 管理面板中的目录价格显示为含税 前端的目录价格显示为含税 购物车中的商品显示为不含税(因此在小计附近单独显示)
有没有办法使用 Zillow API 获取最新缴纳的属性(property)税金额?我可以获得评估值(value) - 但不是已缴纳的税款。 查看特性详细信息时,可以在 Zillow 网站的“税务历史
我正在尝试在 MySQL 中创建一个报告(从 Wordpress/Woocommerce 安装中的表,但是这家商店征税的方式与 Woocommerce 做事的方式不同,所以我不能使用 Woocomme
目前我的所有产品都设置为含税。由于我所在国家/地区的规定,我希望我的小计不含税,然后是要支付的税额,然后是所有税金的总计(这已经是默认值) 在我的 review-order.php 这一行 被调用。我
我正在开设一家商店,并且已经获得了产品价格。现在,我正在研究增值税问题,并在“销售”>“税收”下设置了税收详细信息,以应用 20% 的英国增值税税率。 产品目录中输入的价格均包含增值税,因此在“系统”
我是一名优秀的程序员,十分优秀!