Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

php - Change Cart total price in WooCommerce

I am running into issues with the cart total only displaying 0

Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal.

So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value.

I took the code from here: Change total and tax_total Woocommerce and customize it this way:

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);
} 

But the total amount shows 0.00 when that code is enabled. If removed the code, I get the standard total.

I also could not find on the woocommerce site where the full api is listed, only generic articles related to how to create a plugin.

Any help or a point in the right direction would be great.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:

function prefix_add_discount_line( $cart ) {

  $discount = $cart->subtotal * 0.1;

  $cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );

}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...