我遇到的问题是购物车总数只显示0
基本上我想要做的只是在将所有购物车商品添加到购物车小计后才接受一定金额的存款.
因此,例如,如果客户增加价值100美元的物品,他们最初只需支付10美元或小计的(10%)作为总价值.
我从这里获取代码:更改total和tax_total Woocommerce并以这种方式自定义:
add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);
function calculate_totals($wc_price){
$new_total = ($wc_price*0.10);
return wc_price($new_total);
}
Run Code Online (Sandbox Code Playgroud)
但是当启用该代码时,总金额显示为0.00.如果删除了代码,我会得到标准总数.
我也找不到列出完整api的woocommerce网站,只有与如何创建插件有关的通用文章.
任何帮助或正确方向上的一点都会很棒.
functions.php在WordPress 的页面中,如何使用以下方法在WooCommerce中获取税金总额:
global $woocommerce;
$discount = $woocommerce->cart->tax_total;
Run Code Online (Sandbox Code Playgroud)
但是没有返回任何值。
如何获得购物车税总额?
从本质上讲,我希望为用户计算税金,但是由于客户将支付COD税金,因此减少了税金。
完整代码如下:
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
$cart_object->cart_contents_total *= .10 ;
endif;
}
//Code for removing tax from total collected
function prefix_add_discount_line( $cart ) {
global $woocommerce;
$discount = $woocommerce->cart->tax_total;
$woocommerce->cart->add_fee( __( 'Tax Paid On COD', 'your-text-domain' ) , - $discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Run Code Online (Sandbox Code Playgroud)