PHP代码段:在WooCommerce单一产品和购物车页面上,根据购买的最低金额设置特定产品的“最小添加到购物车”数量
在下面的示例中,我为商品ID = 123设置了最低购买金额$50。此功能将需要在单个产品页面和购物车页面上起作用。如果产品ID = 123,价格为$ 9,则$min的金额为:ceil(50/9)= 6
// ------------
// 1. Single Product Page
add_filter( 'woocommerce_quantity_input_min', 'bloomer_woocommerce_quantity_min_50_eur', 9999, 2 );
function bloomer_woocommerce_quantity_min_50_eur( $min, $product ) {
if ( is_product() ) {
if ( 123 === $product->get_id() ) {
$min = ceil( 50 / $product->get_price() );
}
}
return $min;
}
// ------------
// 2. Cart Page
add_filter( 'woocommerce_cart_item_quantity', 'bloomer_woocommerce_quantity_min_50_eur_cart', 9999, 3 );
function bloomer_woocommerce_quantity_min_50_eur_cart( $product_quantity, $cart_item_key, $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$min = 0;
if ( 123 === $_product->get_id() ) {
$min = ceil( 50 / $_product->get_price() );
}
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->get_max_purchase_quantity(),
'min_value' => $min,
'product_name' => $_product->get_name(),
), $_product, false );
return $product_quantity;
}