
[英]Display product total and cart subtotal on quantity change in WooCommerce product page
[英]Calculate subtotal on quantity increment on single product page in WooCommerce
我正在使用以下代码计算 WooCommerce 中单个产品页面上数量增量的小计。 这很好用
/**
* @snippet Calculate Subtotal Based on Quantity - WooCommerce Single Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 4.1
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_product_price_recalculate' );
function bbloomer_product_price_recalculate() {
global $product;
echo '<div id="subtot" style="display:inline-block;">Total: <span></span></div>';
$price = $product->get_price();
$currency = get_woocommerce_currency_symbol();
wc_enqueue_js( "
$('[name=quantity]').on('input change', function() {
var qty = $(this).val();
var price = '" . esc_js( $price ) . "';
var price_string = (price*qty).toFixed(2);
$('#subtot > span').html('" . esc_js( $currency ) . "'+price_string);
}).change();
" );
}
我希望计算的小计仅在 WooCommerce 产品数量(在产品页面中)大于 1 时才显示/工作,有什么建议吗?
使用以下var qty = $( this ).val();
在 if 条件下。 如果大于 1..
所以你得到:
function action_woocommerce_after_add_to_cart_button() {
global $product;
echo '<div id="subtot" style="display:inline-block;"><span></span></div>';
$price = $product->get_price();
$currency = get_woocommerce_currency_symbol();
wc_enqueue_js( "
$( '[name=quantity]' ).on( 'input change', function() {
var qty = $( this ).val();
var price = '" . esc_js( $price ) . "';
var price_string = ( price*qty ).toFixed(2);
// Greater than
if ( qty > 1 ) {
$( '#subtot > span').html( 'Total: " . esc_js( $currency ) . "' + price_string );
} else {
$( '#subtot > span').html( '' );
}
}).change();
" );
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10, 0 );
试试这个
add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_product_price_recalculate' );
function bbloomer_product_price_recalculate() {
global $product;
if( $product->get_stock_quantity() > 1 ) {
echo '<div id="subtot" style="display:inline-block;">Total: <span></span></div>';
$price = $product->get_price();
$currency = get_woocommerce_currency_symbol();
wc_enqueue_js( "
$('[name=quantity]').on('input change', function() {
var qty = $(this).val();
var price = '" . esc_js( $price ) . "';
var price_string = (price*qty).toFixed(2);
$('#subtot > span').html('" . esc_js( $currency ) . "'+price_string);
}).change();
" );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.