Advertisement
palsushobhan

wcfm-min-order-amount-excluding-discount

Apr 7th, 2023
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.55 KB | None | 0 0
  1. add_action('end_wcfm_vendor_settings', function ($vendor_id) {
  2.     global $WCFM, $WCFMmp;
  3.     $wcfm_min_order_amt = get_user_meta($vendor_id, '_wcfm_min_order_amt', 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 Amount', '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_amt" => array('label' => __('Minimum Amount', 'wc-frontend-manager'), 'type' => 'number', 'class' => 'wcfm-text wcfm_non_negative_input wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $wcfm_min_order_amt),
  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_amt = get_user_meta($vendor_id, '_wcfm_min_order_amt', true);
  24.         $wcfm_min_order_amt_field = array(
  25.             "_wcfm_min_order_amt" => array('label' => __('Minimum Amount', 'wc-frontend-manager'), 'type' => 'number', 'class' => 'wcfm-text wcfm_non_negative_input wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $wcfm_min_order_amt),
  26.         );
  27.         $setting_fields = array_merge($wcfm_min_order_amt_field, $setting_fields);
  28.     }
  29.     return $setting_fields;
  30. }, 50, 2);
  31. add_action('woocommerce_single_product_summary', function () {
  32.     global $WCFM, $WCFMmp, $post;
  33.     $vendor_id = 0;
  34.     $product_id = 0;
  35.     if (is_product() && $post && is_object($post)) {
  36.         $product_id = $post->ID;
  37.     }
  38.     if (!$product_id) return;
  39.     $vendor_id = wcfm_get_vendor_id_by_post($product_id);
  40.     if (!$vendor_id) return;
  41.     $wcfm_min_order_amt = get_user_meta($vendor_id, '_wcfm_min_order_amt', true);
  42.     if (!$wcfm_min_order_amt) return;
  43.     echo '<div class="wcfm_clearfix"></div><div class="wcfmmp_shipment_processing_display">' . __('Minimum order amount should be ', 'wc-multivendor-marketplace') . ' ' . wc_price($wcfm_min_order_amt) . '</div><div class="wcfm_clearfix"></div>';
  44. }, 35);
  45. add_action('wcfm_vendor_settings_update', function ($vendor_id, $wcfm_settings_form) {
  46.     global $WCFM, $WCFMmp;
  47.     if (isset($wcfm_settings_form['_wcfm_min_order_amt'])) {
  48.         $wcfm_min_order_amt = $wcfm_settings_form['_wcfm_min_order_amt'];
  49.         update_user_meta($vendor_id, '_wcfm_min_order_amt', $wcfm_min_order_amt);
  50.     }
  51. }, 500, 2);
  52. add_action('woocommerce_check_cart_items', function () {
  53.     global $WCFM, $WCFMmp;
  54.     $return = true;
  55.     if (is_cart() || is_checkout()) {
  56.         $vendor_wise_cart_total = array();
  57.         foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
  58.             $cart_product_id = $cart_item['product_id'];
  59.             $cart_product = get_post($cart_product_id);
  60.             if (!isset($vendor_wise_cart_total[$cart_product->post_author])) $vendor_wise_cart_total[$cart_product->post_author] = 0;
  61.             $vendor_wise_cart_total[$cart_product->post_author] += $cart_item['line_subtotal'];
  62.             if(WC()->cart->display_prices_including_tax()) {
  63.                 $vendor_wise_cart_total[$cart_product->post_author] += $cart_item['line_subtotal_tax'];
  64.             }
  65.         }
  66.         $error_notices = [];
  67.         if (!empty($vendor_wise_cart_total)) {
  68.             foreach ($vendor_wise_cart_total as $vendor_id => $cart_total) {
  69.                 if (wcfm_is_vendor($vendor_id)) {
  70.                     $wcfm_min_order_amt = get_user_meta($vendor_id, '_wcfm_min_order_amt', true);
  71.                     if ($wcfm_min_order_amt && ($wcfm_min_order_amt > $cart_total)) {
  72.                         $vendor_label = wcfm_get_vendor_store($vendor_id) . ' ' . apply_filters('wcfm_sold_by_label', $vendor_id, __('Store', 'wc-frontend-manager'));
  73.                         $error_notices[] = [$vendor_label, wc_price($wcfm_min_order_amt)];
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         if(!empty($error_notices)) {
  79.             wc_clear_notices();
  80.             foreach($error_notices as $vendor_notice) {
  81.                 wc_add_notice(sprintf(__("%s minimum order amount should be %s, please add few more items from this store!", "wc-frontend-manager"), $vendor_notice[0], $vendor_notice[1]), 'error');
  82.             }
  83.         }
  84.         $return = false;
  85.     }
  86.     return $return;
  87. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement