简体   繁体   中英

Calculate subtotal on quantity increment on single product page in WooCommerce

I'm using the following code to calculate subtotal on quantity increment on single product page in WooCommerce. This works well

/**
 * @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();
   " );

}

I would like the calculated subtotal to appear/work only if WooCommerce product quantity (in the product page)- is more than 1, any advice?

Use the following var qty = $( this ).val(); in an if condition. If greater than 1..

So you get:

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

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM