简体   繁体   中英

Remove custom quantity field when product out of stock on WooCommerce archives

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

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

There's no need for additional code as WooCommerce does this automatically. On the Inventory tab when you're editing the product, tick "Manage Stock" and then select "Allow back-orders?" and set it to "Do not allow".

For your archives pages quantity field, you will need to make some change in your code:

add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9 )
function custom_quantity_field_archive() {
    global $product;

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

Code goes in function.php file of your active child theme (or active theme). It should work.

I am using WooCommerce, and my products are out of stock, this morning I discovered that the customer ordered the product that is out of stock and not evens listed on the featured product list. What is puzzling is that WooCommerce added stock automatically. I am trying to figure out why. It happened twice. Please advise what the issue can be as all the necessary settings are in place making the product out of stock. Should I turn off manage stock? I am really confused

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