在Woocommerce中,我想在客户结账之前设定一些要求得到批准。我希望客户在进行结账前有6个数量。
数量与一种产品无关,它们可以组合更多的产品,直到达到总量。
如果恰好有6个,则可以继续。下一阶段,必须有正好12个数量才能结账(例如在购物车中有8个数量),之后18个,24个等......
我正在使用此功能,仅适用于6项数量计数限制。我想用之前解释的逻辑扩展功能,使其具有动态性和渐进性。
我的实际代码:
// Set a minimum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum number of products before checking out
$minimum_num_products = 6;
// Get the Cart's total number of products
$cart_num_products = WC()->cart->cart_contents_count;
// Compare values and add an error is Cart's total number of products
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 20 products is required before checking out. (Cont. below)
// Current number of items in the cart: 6
if( $cart_num_products < $minimum_num_products ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_num_products,
$cart_num_products ),
'error' );
}
}
}
任何帮助都将非常感激。
答案 0 :(得分:1)
以下是在订单状态更改时计算用户动态累进料品数量限制的方法,并根据数字6(6)增加步骤。
此外,数量必须是六(6)的倍数。
代码:
// User dynamic progressive item quantity count limitation
add_action( 'woocommerce_check_cart_items', 'user_dynamic_progressive_min_item_qty_count_limitation' );
function user_dynamic_progressive_min_item_qty_count_limitation() {
// Only in the Cart or Checkout pages
if( ! ( is_cart() || is_checkout() ) ) return; // Exit
$base_step = 6; // The quantity step base
$user_id = get_current_user_id(); // User ID
$min_qty = get_user_meta( $user_id, '_min_item_qty_ref', true ); // Get min qty from user data
$min_qty = $user_id == 0 ? $base_step : ( empty($min_qty) ? $base_step : $min_qty );
$qty_count = (int) WC()->cart->get_cart_contents_count(); // cart items count
if( $qty_count < $min_qty ):
// Display an error notice when quantity count is below the minimum
wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>'
. '<br />Current number of items in the cart: %s.', $min_qty, $qty_count ), 'error' );
elseif( ( $qty_count % 6 ) != 0 ):
// Display an error notice when quantity count is not a multiple of 6
wc_add_notice( sprintf( '<strong>A Multiple of %s products is required before checking out.</strong>'
. '<br />Current number of items in the cart: %s.', $base_step, $qty_count ), 'error' );
endif;
}
// User dynamic progressive item quantity count calculation (increase or decrease) on order status change
add_action('woocommerce_order_status_changed', 'user_dynamic_progressive_min_item_qty_calculation', 50, 4 );
function user_dynamic_progressive_min_item_qty_calculation( $order_id, $old_status, $new_status, $order ){
$statuses_increase_limit = array('on-hold', 'processing', 'completed'); // Succesful statuses
$statuses_decrease_limit = array('cancelled', 'failed'); // Negative statuses (decreasing)
$min_qty_step = 6; // The quantity step base
$customer_id = (int) $order->get_customer_id(); // User ID
$user_min_qty = (int) get_user_meta( $customer_id, '_min_item_qty_ref', true ); // Get min qty from user data
$order_flag = get_post_meta( $order_id, '_min_item_qty_flag', true ); // Get order min qty flag
$order_flag = empty($order_flag) ? false : $order_flag;
if ( in_array($new_status, $statuses_increase_limit) && ! $order_flag )
{
$user_min_qty = $user_min_qty == 0 ? $min_qty_step : $user_min_qty;
update_post_meta( $order_id, '_min_item_qty_flag', true );
update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty + $min_qty_step ); // Increase
}
elseif ( in_array($new_status, $statuses_increase_limit) && $order_flag )
{
update_post_meta( $order_id, '_min_item_qty_flag', false );
update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty - $min_qty_step ); // Decrease
}
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
下订单时,计算会增加最小项数量限制计数,并在用户数据中设置。订单已标记。如果订单被取消或失败,它将检查订单标志,如果设置了标志,它将减少最小项目数量限制计数。