Advertisement
palsushobhan

wcfm vendor minimum order quantity

Dec 17th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. add_action('end_wcfm_vendor_settings', function ($vendor_id) {
  2.     global $WCFM, $WCFMmp;
  3.     $wcfm_min_order_qty = get_user_meta($vendor_id, '_wcfm_min_order_qty', true);
  4. ?>
  5.     <div class="page_collapsible" id="wcfm_settings_form_min_order_amount_head">
  6.         <label class="wcfmfa fa-cart-plus"></label>
  7.         <?php _e('Min Order Quantity', 'wc-frontend-manager'); ?><span></span>
  8.     </div>
  9.     <div class="wcfm-container">
  10.         <div id="wcfm_settings_form_vendor_invoice_expander" class="wcfm-content">
  11.             <?php
  12.             $WCFM->wcfm_fields->wcfm_generate_form_field(array(
  13.                 "_wcfm_min_order_qty" => array('label' => __('Minimum Quantity', 'wc-frontend-manager'), 'type' => 'number', 'class' => 'wcfm-text wcfm_non_negative_input wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $wcfm_min_order_qty),
  14.             ));
  15.             ?>
  16.         </div>
  17.     </div>
  18.     <div class="wcfm_clearfix"></div>
  19. <?php
  20. }, 500);
  21. add_filter('wcfm_marketplace_settings_fields_general', function ($setting_fields, $vendor_id) {
  22.     if (!wcfm_is_vendor()) {
  23.         $wcfm_min_order_qty = get_user_meta($vendor_id, '_wcfm_min_order_qty', true);
  24.         $wcfm_min_order_qty_field = array(
  25.             "_wcfm_min_order_qty" => array('label' => __('Minimum Quantity', 'wc-frontend-manager'), 'type' => 'number', 'class' => 'wcfm-text wcfm_non_negative_input wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $wcfm_min_order_qty),
  26.         );
  27.         $setting_fields = array_merge($wcfm_min_order_qty_field, $setting_fields);
  28.     }
  29.     return $setting_fields;
  30. }, 50, 2);
  31. add_action('wcfm_vendor_settings_update', function ($vendor_id, $wcfm_settings_form) {
  32.     global $WCFM, $WCFMmp;
  33.     if (isset($wcfm_settings_form['_wcfm_min_order_qty'])) {
  34.         $wcfm_min_order_qty = $wcfm_settings_form['_wcfm_min_order_qty'];
  35.         update_user_meta($vendor_id, '_wcfm_min_order_qty', $wcfm_min_order_qty);
  36.     }
  37. }, 500, 2);
  38. add_action('woocommerce_check_cart_items', function () {
  39.     $return = true;
  40.     if (is_cart() || is_checkout()) {
  41.         $vendor_wise_cart_qty = array();
  42.         foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
  43.             $cart_product_id = $cart_item['product_id'];
  44.             $cart_product = get_post($cart_product_id);
  45.             if (!isset($vendor_wise_cart_qty[$cart_product->post_author]))
  46.                 $vendor_wise_cart_qty[$cart_product->post_author] = 0;
  47.             $vendor_wise_cart_qty[$cart_product->post_author] += $cart_item['quantity'];
  48.         }
  49.         if (!empty($vendor_wise_cart_qty)) {
  50.             foreach ($vendor_wise_cart_qty as $vendor_id => $cart_qty) {
  51.                 if (wcfm_is_vendor($vendor_id)) {
  52.                     $wcfm_min_order_qty = get_user_meta($vendor_id, '_wcfm_min_order_qty', true);
  53.                     if ($wcfm_min_order_qty && ($wcfm_min_order_qty > $cart_qty)) {
  54.                         wc_clear_notices();
  55.                         $vendor_label = wcfm_get_vendor_store($vendor_id) . ' ' . apply_filters('wcfm_sold_by_label', $vendor_id, __('Store', 'wc-frontend-manager'));
  56.                         wc_add_notice(sprintf(__("%s minimum order amount should be %s, please add few more items from this store!", "wc-frontend-manager"), $vendor_label, wc_price($wcfm_min_order_qty)), 'error');
  57.                         $return = false;
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     return $return;
  65. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement