【问题标题】:Show tax amount from a single product in woocommerce在woocommerce中显示单个产品的税额
【发布时间】:2018-02-07 07:17:51
【问题描述】:

我想添加一个 woocommerce 函数,该函数创建一个短代码,用于在产品页面中显示单个产品的税额。

最佳解决方案是直接在 php 函数中进行数学运算(productprice * 0,25)并回显结果,因为我们出售的产品是免税的。编写此脚本的原因是向我们的客户展示他们在从非欧盟国家/地区进口产品期间将支付多少税款。

即 产品价格:$100 税率 25 % 税额 $25

我想用简码显示税额,如下所示:

此产品的总税额:25 美元

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce shortcode tax


    【解决方案1】:

    这可以通过以下代码轻松完成:

    if( ! function_exists('get_formatted_product_tax_amount') ) {
    
        function get_formatted_product_tax_amount( $atts ) {
            // Attributes
            $atts = shortcode_atts( array(
                'id' => '0',
            ), $atts, 'tax_amount' );
    
            global $product, $post;
    
            if( ! is_object( $product ) || $atts['id'] != 0 ){
                if( is_object( $post ) && $atts['id'] == 0 )
                    $product_id = $post->ID;
                else
                    $product_id = $atts['id'];
    
                $product = wc_get_product( $product_id );
            }
    
            if( is_object( $product ) ){
                $price_excl_tax = wc_get_price_excluding_tax($product);
                $price_incl_tax = wc_get_price_including_tax($product);
                $tax_amount = $price_incl_tax - $price_excl_tax;
    
                return wc_price($tax_amount);
            }
        }
    
        add_shortcode( 'tax_amount', 'get_formatted_product_tax_amount' );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试和工作


    用法 (示例)

    1) 在单个产品页面中,在简短描述文本编辑器中:

    Total tax amount for this product: [tax_amount]
    

    2) 对于单个产品页面,在 php 代码中:

    $text = __('Total tax amount for this product', 'woocommerce');
    echo '<p>' . $text . ': ' . do_shortcode("[tax_amount]") . '</p>';
    

    3) 在其他页面上,在文本编辑器中(将产品 ID 设置为参数):

    Total tax amount for this product: [tax_amount id="37"]
    

    4) 任何地方,在 php 代码中(将产品 ID 设置为参数):

    $text = __('Total tax amount for this product', 'woocommerce');
    echo '<p>' . $text . ': ' . do_shortcode("[tax_amount id='37']") . '</p>';
    

    【讨论】:

    • 完美。完全按预期工作。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多