【问题标题】:Calculate subtotal on quantity increment on single product page in WooCommerce计算 WooCommerce 中单个产品页面上数量增量的小计
【发布时间】:2021-09-10 05:32:55
【问题描述】:

我正在使用以下代码来计算 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 时出现/工作,有什么建议吗?

【问题讨论】:

    标签: php jquery wordpress woocommerce product


    【解决方案1】:

    在 if 条件中使用以下 var qty = $( this ).val();。如果大于 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 );
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-21
        • 2017-04-06
        • 1970-01-01
        相关资源
        最近更新 更多