Advertisement
palsushobhan

wcfm-membership-wise-vendor-min-order-amount

Aug 31st, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. function is_allowed_for_vendor($vendor_id) {
  2.     $allowed_membership_ids = [100, 3117, 3121, 3123, 3124];
  3.     if(wcfm_is_vendor($vendor_id)) {
  4.         $wcfm_membership_id = get_user_meta( $vendor_id, 'wcfm_membership', true );
  5.         return (wcfm_is_valid_membership( $wcfm_membership_id ) && in_array($wcfm_membership_id, $allowed_membership_ids));
  6.     }
  7.     return false;
  8. }
  9. add_action( 'end_wcfm_vendor_settings', function( $vendor_id ) {
  10.     if(!is_allowed_for_vendor($vendor_id)) return;
  11.     global $WCFM, $WCFMmp;
  12.     $wcfm_min_order_amt = get_user_meta( $vendor_id, '_wcfm_min_order_amt', true );
  13.     ?>
  14.     <div class="page_collapsible" id="wcfm_settings_form_min_order_amount_head">
  15.         <label class="wcfmfa fa-cart-plus"></label>
  16.         <?php _e('Min Order Amount', 'wc-frontend-manager'); ?>
  17.     </div>
  18.     <div class="wcfm-container">
  19.         <div id="wcfm_settings_form_vendor_invoice_expander" class="wcfm-content">
  20.         <?php
  21.         $WCFM->wcfm_fields->wcfm_generate_form_field( array(
  22.             "_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 ),
  23.         ) );
  24.         ?>
  25.         </div>
  26.     </div>
  27.     <div class="wcfm_clearfix"></div>
  28.     <?php
  29. }, 500 );
  30. add_filter( 'wcfm_marketplace_settings_fields_general', function( $setting_fields, $vendor_id ) {
  31.     if(!is_allowed_for_vendor($vendor_id)) return $setting_fields;
  32.     if( !wcfm_is_vendor() ) {
  33.         $wcfm_min_order_amt = get_user_meta( $vendor_id, '_wcfm_min_order_amt', true );
  34.         $wcfm_min_order_amt_field = array(
  35.         "_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 ),
  36.         );
  37.         $setting_fields = array_merge( $wcfm_min_order_amt_field, $setting_fields );
  38.     }
  39.     return $setting_fields;
  40. }, 50, 2 );
  41. add_action( 'woocommerce_single_product_summary', function() {
  42.     global $WCFM, $WCFMmp, $post;
  43.     $vendor_id = 0;
  44.     $product_id = 0;
  45.     if( is_product() && $post && is_object( $post ) ) {
  46.         $product_id = $post->ID;
  47.     }
  48.     if( !$product_id ) return;
  49.     $vendor_id = wcfm_get_vendor_id_by_post( $product_id );
  50.     if( !$vendor_id || !is_allowed_for_vendor($vendor_id)) return;
  51.     $wcfm_min_order_amt = get_user_meta( $vendor_id, '_wcfm_min_order_amt', true );
  52.     if( !$wcfm_min_order_amt ) return;
  53.     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>';
  54. }, 35 );
  55. add_action( 'wcfm_vendor_settings_update', function( $vendor_id, $wcfm_settings_form ) {
  56.     if(!is_allowed_for_vendor($vendor_id)) return;
  57.     global $WCFM, $WCFMmp;
  58.     if( isset( $wcfm_settings_form['_wcfm_min_order_amt'] ) ) {
  59.         $wcfm_min_order_amt = $wcfm_settings_form['_wcfm_min_order_amt'];
  60.         update_user_meta( $vendor_id, '_wcfm_min_order_amt', $wcfm_min_order_amt );
  61.     }
  62. }, 500, 2 );
  63. add_action( 'woocommerce_check_cart_items', function() {
  64.     $return = true;
  65.     $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
  66.     $chosen_shipping = reset($chosen_methods);
  67.     if(!$chosen_shipping) return $return;
  68.     if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) return $return;
  69.     if( is_cart() || is_checkout() ) {
  70.         $vendor_wise_cart_total = array();
  71.         foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  72.             $cart_product_id = $cart_item['product_id'];
  73.             $cart_product = get_post( $cart_product_id );
  74.             if( !isset( $vendor_wise_cart_total[$cart_product->post_author] ) ) $vendor_wise_cart_total[$cart_product->post_author] = 0;
  75.             $vendor_wise_cart_total[$cart_product->post_author] += $cart_item['line_total'];
  76.         }
  77.         if( !empty( $vendor_wise_cart_total ) ) {
  78.             foreach( $vendor_wise_cart_total as $vendor_id => $cart_total ) {
  79.                 if( wcfm_is_vendor( $vendor_id ) && is_allowed_for_vendor($vendor_id)) {
  80.                     $wcfm_min_order_amt = get_user_meta( $vendor_id, '_wcfm_min_order_amt', true );
  81.                     if( $wcfm_min_order_amt && ( $wcfm_min_order_amt > $cart_total ) ) {
  82.                         wc_clear_notices();
  83.                         $vendor_label = wcfm_get_vendor_store( $vendor_id ) . ' ' . apply_filters( 'wcfm_sold_by_label', $vendor_id, __( 'Store', 'wc-frontend-manager' ) );
  84.                         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_amt ) ), 'error' );
  85.                         $return = false;
  86.                         break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     return $return;
  93. }, 1000 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement