簡體   English   中英

計算 WooCommerce 中單個產品頁面上數量增量的小計

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM