| |
| <?php
|
|
|
| add_action('woocommerce_before_single_product_summary', 'display_category_notice');
|
|
|
| function display_category_notice() {
|
| global $product;
|
|
|
| if (!$product) {
|
| return;
|
| }
|
|
|
| $categories = get_categories_with_minimum_amount();
|
| $cart_items = WC()->cart->get_cart();
|
| $notices = array();
|
|
|
| foreach ($categories as $category_name => $minimum_amount) {
|
| if (has_term($category_name, 'product_cat', $product->get_id())) {
|
| $category_total = 0;
|
|
|
| foreach ($cart_items as $cart_item) {
|
| if (has_term($category_name, 'product_cat', $cart_item['product_id'])) {
|
| $category_total += $cart_item['line_total'];
|
| }
|
| }
|
|
|
| $category_balance = $minimum_amount - $category_total;
|
|
|
| if ($category_balance <= 0) {
|
| $notices[] = sprintf(__('Congratulations! You have met the minimum order amount for the category "%s".', 'your-text-domain'), $category_name);
|
| continue;
|
| }
|
|
|
| $product_names = getProductNamesByCategory($category_name);
|
| $category_link = get_term_link($category_name, 'product_cat');
|
|
|
| if (!is_wp_error($category_link)) {
|
| $notice = sprintf(
|
| __('This product belongs to the category "<a href="%s" class="category-product">%s</a>". Current Total cart amount of products from this category: <span class="amount">%s</span>.<br> Amount needed to meet the minimum: <span class="amount">%s</span>. Category products in the Cart: <span class="category-products">%s</span>', 'your-text-domain'),
|
| esc_url($category_link), esc_html($category_name), wc_price($category_total), wc_price($category_balance), $product_names
|
| );
|
|
|
| echo '<div class="woocommerce-info">' . $notice . '</div>';
|
| }
|
| }
|
| }
|
|
|
| if (!empty($notices)) {
|
| wc_add_notice(implode('<br>', $notices), 'success');
|
| }
|
| }
|
|
|
|
|
| function getProductNamesByCategory($category_name) {
|
| $product_names = array();
|
|
|
| foreach (WC()->cart->get_cart() as $cart_item) {
|
| if (has_term($category_name, 'product_cat', $cart_item['product_id'])) {
|
| $product = wc_get_product($cart_item['product_id']);
|
| $product_names[] = sprintf('<a href="%s" class="product-name">%s</a>', esc_url($product->get_permalink()), esc_html($product->get_name()));
|
| }
|
| }
|
|
|
| return implode(', ', $product_names);
|
| }
|
|
|
|
|
| add_action('woocommerce_check_cart_items', 'check_cart_outlet_items');
|
|
|
| function check_cart_outlet_items() {
|
| $categories = get_categories_with_minimum_amount();
|
| $cart = WC()->cart;
|
| $cart_items = $cart->get_cart();
|
| $notices = array();
|
|
|
| foreach ($categories as $category_name => $minimum_amount) {
|
| $found = false;
|
| $category_total = 0;
|
| $product_names = array();
|
|
|
| foreach ($cart_items as $cart_item) {
|
| if (has_term($category_name, 'product_cat', $cart_item['product_id'])) {
|
| $found = true;
|
| $category_total += $cart_item['line_total'];
|
| $product = wc_get_product($cart_item['product_id']);
|
| $product_names[] = sprintf('<a href="%s" class="product-name">%s</a>', esc_url($product->get_permalink()), esc_html($product->get_name()));
|
| }
|
| }
|
|
|
| if ($found && $category_total < $minimum_amount) {
|
| $category_link = get_term_link($category_name, 'product_cat');
|
|
|
| if (!is_wp_error($category_link)) {
|
| $notices[] = sprintf(
|
| __('Minimum order amount for category <a href="%s" class="category-link">%s</a> is %s. Add <span class="amount">%s</span> to meet the minimum. Total amount of products in this category already in the cart: <span class="amount">%s</span>. Category products: <span class="category-products">%s</span>', 'your-text-domain'),
|
| esc_url($category_link), esc_html($category_name), wc_price($minimum_amount), wc_price($minimum_amount - $category_total), wc_price($category_total), implode(', ', $product_names)
|
| );
|
| }
|
| }
|
| }
|
|
|
| if (!empty($notices)) {
|
| wc_add_notice(implode('<br>', $notices), 'error');
|
| restrict_checkout_buttons();
|
| }
|
| }
|
|
|
| function restrict_checkout_buttons() {
|
| if (WC()->cart->total < array_sum(get_categories_with_minimum_amount())) {
|
| remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);
|
| if (is_checkout()) {
|
| remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
|
| }
|
| }
|
| }
|
|
|
|
|
| function get_categories_with_minimum_amount() {
|
| return array(
|
| 'Cleanser' => 1000,
|
| 'Moisturizer' => 250,
|
| 'Cat 3' => 500,
|
| 'Cat 4' => 500,
|
| );
|
| }
|
|
|
|
|
| add_action('wp_head', 'custom_error_notice_styles');
|
|
|
| function custom_error_notice_styles() {
|
| echo '
|
| <style>
|
| .woocommerce-error {
|
| background-color: #000000;
|
| border: 1px solid #ff3333;
|
| color: #ffffff;
|
| padding: 10px;
|
| margin-bottom: 20px;
|
| text-align: center;
|
| }
|
|
|
| .woocommerce-error strong {
|
| font-weight: bold;
|
| }
|
|
|
| .woocommerce-error ul {
|
| margin: 0;
|
| padding-left: 20px;
|
| }
|
|
|
| .woocommerce-error li {
|
| list-style-type: disc;
|
| }
|
|
|
| .woocommerce-error .amount {
|
| color: #ff3333;
|
| font-weight: bold;
|
| border-radius: 20px;
|
| padding: 5px 10px;
|
| }
|
|
|
| .woocommerce-error .category-link {
|
| color: #8AFF33;
|
| text-decoration: none;
|
| }
|
|
|
| .woocommerce-error .category-link:hover {
|
| text-decoration: underline;
|
| }
|
|
|
| .woocommerce-error .category-product {
|
| color: #00ff00;
|
| cursor: pointer;
|
| }
|
|
|
| .woocommerce-error .product-name {
|
| color: #3366cc;
|
| cursor: pointer;
|
| text-decoration: underline;
|
| }
|
|
|
| .woocommerce-info {
|
| background-color: #000000;
|
| border: 1px solid #ffffff;
|
| color: #ffffff;
|
| padding: 10px;
|
| margin-bottom: 20px;
|
| text-align: center;
|
| }
|
|
|
| .woocommerce-info .amount {
|
| color: #ff3333;
|
| font-weight: bold;
|
| border: 1px solid #ff3333;
|
| border-radius: 20px;
|
| padding: 5px 10px;
|
| }
|
|
|
| .woocommerce-info .category-product {
|
| color: #00ff00;
|
| cursor: pointer;
|
| }
|
|
|
| .woocommerce-info .product-name {
|
| color: #3366cc;
|
| cursor: pointer;
|
| text-decoration: underline;
|
| }
|
| </style>
|
| ';
|
| }
|
|
|
|
|
| add_action('woocommerce_checkout_process', 'limit_checkout_until_thresholds_met');
|
|
|
| function limit_checkout_until_thresholds_met() {
|
| $categories = get_categories_with_minimum_amount();
|
| $cart = WC()->cart;
|
| $cart_items = $cart->get_cart();
|
| $all_thresholds_met = true;
|
|
|
| foreach ($categories as $category_name => $minimum_amount) {
|
| $category_total = 0;
|
|
|
| foreach ($cart_items as $cart_item) {
|
| if (has_term($category_name, 'product_cat', $cart_item['product_id'])) {
|
| $category_total += $cart_item['line_total'];
|
| }
|
| }
|
|
|
| if ($category_total < $minimum_amount) {
|
| $all_thresholds_met = false;
|
| wc_add_notice(sprintf(__('You must add more products from the category "%s" to meet the minimum order amount of %s.', 'your-text-domain'), $category_name, wc_price($minimum_amount)), 'error');
|
| }
|
| }
|
|
|
| if (!$all_thresholds_met) {
|
| remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
|
| }
|
| }
|
|
|
|
|
| add_action('woocommerce_before_checkout_form', 'display_checkout_notice_if_thresholds_not_met');
|
|
|
| function display_checkout_notice_if_thresholds_not_met() {
|
| $categories = get_categories_with_minimum_amount();
|
| $cart = WC()->cart;
|
| $cart_items = $cart->get_cart();
|
| $notices = array();
|
|
|
| foreach ($categories as $category_name => $minimum_amount) {
|
| $category_total = 0;
|
|
|
| foreach ($cart_items as $cart_item) {
|
| if (has_term($category_name, 'product_cat', $cart_item['product_id'])) {
|
| $category_total += $cart_item['line_total'];
|
| }
|
| }
|
|
|
| if ($category_total < $minimum_amount) {
|
| $notices[] = sprintf(__('You have not met the minimum order amount for the category "%s". Please add more products to your cart to proceed to checkout.', 'your-text-domain'), $category_name);
|
| }
|
| }
|
|
|
| if (!empty($notices)) {
|
| wc_print_notice(implode('<br>', $notices), 'error');
|
| restrict_checkout_buttons();
|
| }
|
| }
|
| |
| |
Comments