Мне нужно, чтобы оптовые пользователи собрали минимум 10 товаров, чтобы перейти на страницу оформления заказа из корзины. Я реализовал некоторый PHP-код, чтобы скрыть кнопку «Перейти к оформлению заказа» и любые кнопки оплаты Stripe, такие как GPay и Apple Pay, если минимальное количество 10 не достигнуто.
Хотя изначально код работает когда я нажимаю кнопку «Корзина» на странице магазина, загружаю или обновляю страницу корзины, кнопки снова появляются, если я обновляю количество корзины (например, меняю его с 9 на 8 или с 8 на 9). Кажется, реализованная мной функция работает только при обновлении страницы корзины. Они должны оставаться скрытыми или отключенными, если в корзине не будет минимального количества товаров в 10.
Я пробовал различные фрагменты PHP из StackOverflow, которые имеют аналогичный запрос, но ни один из них не решил проблему. Я использую плагин Snippet для управления кодом PHP, а не дочерней темой.
Вот код, с которым я работал:
Код: Выделить всё
// Set a minimum quantity for wholesale role
add_action( 'woocommerce_before_cart', 'thumbtoe_minimum_order_quantity' ); // Error message on cart page
add_action( 'woocommerce_check_cart_items', 'thumbtoe_minimum_order_quantity' );
add_action( 'woocommerce_proceed_to_checkout', 'thumbtoe_minimum_order_quantity' ); // Error message on cart page
function thumbtoe_minimum_order_quantity() {
// Set the minimum quantity
$minimum_quantity = 10;
// Get the current user
$user = wp_get_current_user();
// Check if the user has the wholesale role
if ( in_array( 'wholesale', (array) $user->roles ) ) {
// Get total quantity in cart
$total_quantity = WC()->cart->get_cart_contents_count();
// If total quantity is less than minimum
if ( $total_quantity < $minimum_quantity ) {
// Error message
$error_message = sprintf( 'Retail Partners Minimum order amount must exceed %s items in your cart to proceed.', $minimum_quantity );
// Display the error message on the cart page
wc_print_notice( $error_message, 'error' );
// Remove the Proceed to Checkout button
add_action( 'wp_footer', function() {
?>
jQuery(function($) {
$('a.checkout-button').remove(); // Remove the Proceed to Checkout button
$('.gpay-card-info-container-fill').hide(); // Hide GPay button
$('.apple-pay-button').hide(); // Hide Apple Pay button
$('.wc-stripe-cart-checkout-container ul.wc_stripe_cart_payment_methods.active li.wc-stripe-payment-method, p').remove(); // Remove specified Stripe elements
});
Подробнее здесь: [url]https://stackoverflow.com/questions/79069731/woocommerce-wholesale-user-role-setting-minimum-order-quantity-to-10-products-ph[/url]