设置Woocommerce中特定产品和类别的最低订购量
我已经进行了广泛的搜索,看看其他人是否也有这种情况,并且没有运气就得到了答案.
I've searched extensively to see if other folks had this situation and received an answer with no luck.
从本质上讲,我有两个客户可以将其添加到购物车中的项目.我要这样做,以便如果小计的总金额不超过15美元或以上,则他们将无法使用其中任何一项进行结帐.
Essentially, I have two items customers can add to their cart. I want to make it so they cannot checkout with either of those items if their subtotal is not $15 or more.
能够将其ID放入代码中就可以了.或者,我可以将它们分配到同一类别,然后按类别设置此最小值.
Having the ability to just drop their IDs into the code would be fine. Or, I can assign them to the same category and set this minimum by category.
到目前为止,我所拥有的只是设置通用最小值的基本PHP.我只需要一些帮助来弄清楚如何限制它以满足我的需求.
So far all I have is the basic PHP that sets a universal minimum. I just need some help figuring out how to limit this to meet my needs.
我不是设计师,而是开发人员,因此非常感谢您的帮助.
I'm a designer not a dev, so any help is much appreciated.
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 10;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart's total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 10 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
仅为购物车和结帐页面设置购物车中某些产品类别或产品ID的最小值.
This untested snippet made of this and this from wooThemes, and this too:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// minimum order value
$minimum = 10;
if ( WC()->cart->total < $minimum && sizeof( WC()->cart->get_cart() ) > 0 ) {
$products_min = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$category_term_id = $term->term_id;
}
// your products categories
if ( in_array( $category_term_id, array( 17, 18 ) ) ) {
$products_min = true;
// your product ID'S
} elseif ( in_array( $product_id, array( 64, 67, 78 ) ) ) {
$products_min = true;
}
}
if( ( is_cart() || is_checkout() ) && $products_min ) {
wc_print_notice( sprintf(
__("You must have an order with a minimum of %s to place your order. Your current order total is %s."),
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error' );
}
}
}
您可以设置类别ID,产品ID和最小订单金额.
You can set your categories ID's, your products ID's and the minimum order value amount.
— —密码— —
此挂钩适用于消息.但是要避免用户点击,您需要添加Javascript/jQuery,也许还要添加ajax(客户端检测).
This hooks are working for messages. But to avoid users from clicking you need to add Javascript/jQuery and maybe with ajax too (client side detection).