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