![](/img/trans.png)
[英]Remove product button if out of stock from Woocommerce shop and archives
[英]Remove custom quantity field when product out of stock on WooCommerce archives
當商品缺貨時,我試圖從商店、類別和產品頁面中刪除數量選擇框。 有沒有一種簡單的方法可以不使用代碼顯示數量選擇器框?
我正在使用下面的代碼來顯示數量選擇器框。
/**
* 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' );
您可以覆蓋WooCommerce模板。 在主題文件夾中打開一個woocommerce文件夾,然后單擊“單產品/添加到購物車/simple.php”
復制並粘貼woocommerce插件/模板/單模板/添加到購物車/simple.php中的代碼
在第46行,您將看到
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(),
) );
您可以在調用woocommerce_quantity_input函數之前添加自定義if語句。
if($product->get_stock_quantity() > 0) {
// woocommerce_quantity_input calling here...
}
我希望這能幫到您。
不需要其他代碼,因為WooCommerce會自動執行此操作。 編輯產品時,在“庫存”選項卡上,勾選“管理庫存”,然后選擇“允許延期交貨?”。 並將其設置為“不允許”。
對於檔案頁面數量字段,您需要對代碼進行一些更改:
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()
) );
}
}
代碼進入您的活動子主題(或活動主題)的function.php文件中。 它應該工作。
我正在使用 WooCommerce,我的產品缺貨,今天早上我發現客戶訂購的產品缺貨,並且沒有列在特色產品列表中。 令人費解的是,WooCommerce 會自動添加庫存。 我想弄清楚為什么。 它發生了兩次。 請告知可能是什么問題,因為所有必要的設置都已到位,導致產品缺貨。 我應該關閉管理庫存嗎? 我真的很困惑
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.