php - 根据费用有条件地自定义 WooCommerce 购物车总输出

标签 php wordpress woocommerce cart tax

我尝试做一件非常简单的事情,但对我来说,我无法让它发挥作用。

这来自 wc-cart-functions.php

        if ( ! empty( $tax_string_array ) ) {
            $taxable_address = WC()->customer->get_taxable_address();
            $estimated_text  = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
                ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
                : '';
            $value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
        }
    }

    echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
}

如果应用任何$fee,$value 应该不同。 但如果我检查的话,我只能从 $fee 变量中获得 0.00 € 值。

有人可以帮我简单地将费用值应用到变量中并检查一个简单的 if 子句,例如:

if fee is applied ---> 1
else ---> 2

我该怎么做?

最佳答案

如您所见,您可以使用位于 wc-cart-functions.php 核心代码中的 woocommerce_cart_totals_order_total_html 过滤器 Hook 来更改大车总产量:

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_cart_totals_order_total_html', 10, 1 );
function custom_cart_totals_order_total_html( $value ) {
    // Get the fee cart amount
    $fees_total = WC()->cart->fee_total;

    // HERE is the condition
    if( ! empty($fees_total) && $fees_total != 0 ){
        // Change/customize HERE $value (just for test)
        $value .= " <small>($fees_total)<small>";
    }

    // Always return $value
    return $value;
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码经过测试并且可以工作。

Never overide core files, as this is something prohibited, dangerous and not convenient for many reasons

您需要在$value的条件中自定义代码

关于php - 根据费用有条件地自定义 WooCommerce 购物车总输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052820/

相关文章:

php - wooCommerce ajax 提交到同一结账页面不起作用

wordpress - template_include 停止处理 woocommerce 更新

php - 调整在电话号码上使用 Preg_Match()

php - WordPress get_query_var()

php - 在DB表上记录数据

paypal - 在我的 wordpress 站点中将 paypal 与 PAID MEMBERSHIP PRO 集成时,帐户受到限制

php - 选择像数组一样的位置

php - 在 WordPress 中循环自定义帖子类型的特定类别

javascript - Wordpress jQuery 错误 &#038

WooCommerce:即使在选择变体之前也总是显示添加到购物车按钮