dqdmvg7332 2018-04-16 06:05
浏览 79

当WooCommerce存档上的产品缺货时删除自定义数量字段

I am trying to remove the quantity selector box from shop, category and product pages when the item is out of stock. Is there an easy way to not show the quantity selector box using code?

I am using the code below to show the quantity selector box.

 /**
   * Add quantity field on the archive page.
  */
 function custom_quantity_field_archive() {

$product = wc_get_product( get_the_ID() );

if ( ! $product->is_sold_individually() && 'variable' != $product- 
>product_type && $product->is_purchasable() ) {
    woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => 
$product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
}

}
   add_action( 'woocommerce_after_shop_loop_item', 
   'custom_quantity_field_archive', 0, 9 );


/**
* Add requires JavaScript.
 */
 function custom_add_to_cart_quantity_handler() {

wc_enqueue_js( '
    jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", 
 function() {
        return false;
    });
    jQuery( ".post-type-archive-product" ).on( "change input", ".quantity 
.qty", function() {
        var add_to_cart_button = jQuery( this ).parents( ".product" ).find( 
  ".add_to_cart_button" );

        // For AJAX add-to-cart actions
        add_to_cart_button.data( "quantity", jQuery( this ).val() );

        // For non-AJAX add-to-cart actions
        add_to_cart_button.attr( "href", "?add-to-cart=" + 
  add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this 
  ).val() );
    });
' );

 }
  add_action( 'init', 'custom_add_to_cart_quantity_handler' );
  • 写回答

3条回答 默认 最新

  • dtu15253 2018-04-16 07:13
    关注

    You can override the WooCommerce Template. open a woocommerce folder inside your theme folder and then single-product/add-to-cart/simple.php

    copy paste the code from woocommerce plugin/templates/single-template/add-to-cart/simple.php

    on line no 46 you will see

    woocommerce_quantity_input( array(
      'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
      'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
      'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
    ) );
    

    You can add custom if statement before calling the woocommerce_quantity_input function.

    if($product->get_stock_quantity() > 0) {
      // woocommerce_quantity_input calling here...
    }
    

    I hope this will help you.

    NOTE Template structure & Overriding templates via a theme

    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算