Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
5.2 kB
1
Indexable

add_action('woocommerce_before_add_to_cart_form', function(){
	global $post;
	$user_id = get_current_user_id();
	$product_id = $post->ID;

	//$dynamic_minmax_rules = get_transient('b2bking_minmax_'.get_current_user_id());
	$dynamic_minmax_rules = b2bking()->get_global_data('b2bking_minmax',false,get_current_user_id());

	if (!$dynamic_minmax_rules){

		$user_id = b2bking()->get_top_parent_account($user_id);

		$currentusergroupidnr = b2bking()->get_user_group($user_id);
		if (!$currentusergroupidnr || empty($currentusergroupidnr)){
			$currentusergroupidnr = 'invalid';
		}

		$rules_ids_elements = get_option('b2bking_have_minmax_rules_list_ids_elements', array());

		$user_rules = array();
		if (isset($rules_ids_elements['user_'.$user_id])){
			$user_rules = $rules_ids_elements['user_'.$user_id];
		}

		$group_rules = array();
		if (isset($rules_ids_elements['group_'.$currentusergroupidnr])){
			$group_rules = $rules_ids_elements['group_'.$currentusergroupidnr];
		}

		$user_applicable_rules = array_merge($user_rules, $group_rules);
		if (is_user_logged_in()){

			if (isset($rules_ids_elements['all_registered'])){
				// add everyone_registered rules
				$user_applicable_rules = array_merge($user_applicable_rules, $rules_ids_elements['all_registered']);
			}

			// if is user b2b add b2b rules
			if (get_user_meta($user_id,'b2bking_b2buser', true) === 'yes'){
				if (isset($rules_ids_elements['everyone_registered_b2b'])){
					$user_applicable_rules = array_merge($user_applicable_rules, $rules_ids_elements['everyone_registered_b2b']);
				}
			} else {
				// add b2c rules
				if (isset($rules_ids_elements['everyone_registered_b2c'])){
					$user_applicable_rules = array_merge($user_applicable_rules, $rules_ids_elements['everyone_registered_b2c']);
				}
			}
		}

		//set_transient ('b2bking_minmax_'.get_current_user_id(), $user_applicable_rules);
		b2bking()->set_global_data('b2bking_minmax', $user_applicable_rules, false, get_current_user_id());
		//$dynamic_minmax_rules = get_transient('b2bking_minmax_'.get_current_user_id());
		$dynamic_minmax_rules = b2bking()->get_global_data('b2bking_minmax',false,get_current_user_id());

	}

	$smallest_minimum = 'none';
	$largest_maximum = 'none';

	foreach($dynamic_minmax_rules as $dynamic_minmax_rule){
		// get rule details
		$minimum_maximum = get_post_meta($dynamic_minmax_rule, 'b2bking_rule_what', true);
		$quantity_value = get_post_meta($dynamic_minmax_rule, 'b2bking_rule_quantity_value', true);
		$howmuch = floatval(get_post_meta($dynamic_minmax_rule, 'b2bking_rule_howmuch', true));
		$applies = get_post_meta($dynamic_minmax_rule, 'b2bking_rule_applies', true);
		if ($applies === 'cart_total'){


		} else {
			// rule is category or product rule or multiple select rule
			$applies = explode('_',$applies);
			if ($applies[0] === 'category'){

				
			}  else if ($applies[0] === 'product'){
				// rule is product rule
				if(intval($applies[1]) === intval($product_id)){
					if ($quantity_value === 'quantity'){
						if ($minimum_maximum === 'minimum_order'){
							if ($smallest_minimum === 'none'){
								$smallest_minimum = $howmuch;
							} else if ($smallest_minimum > $howmuch) {
								$smallest_minimum = $howmuch;
							}
						} else if ($minimum_maximum === 'maximum_order'){
							if ($largest_maximum === 'none'){
								$largest_maximum = $howmuch;
							} else if ($largest_maximum < $howmuch) {
								$largest_maximum = $howmuch;
							}
						}
					}
				}

			// multiple select rule
			} else if ($applies[0] === 'multiple'){
				$rule_multiple_options = get_post_meta($dynamic_minmax_rule, 'b2bking_rule_applies_multiple_options', true);
				$rule_multiple_options_array = explode(',',$rule_multiple_options);
				// foreach element, category or product
				foreach($rule_multiple_options_array as $rule_element){
					$rule_element_array = explode('_',$rule_element);
					// if is category
					if ($rule_element_array[0] === 'category'){



					// if is product
					} else if ($rule_element_array[0] === 'product'){

						if(intval($rule_element_array[1]) === $product_id){
							if ($quantity_value === 'quantity'){
								if ($minimum_maximum === 'minimum_order'){
									if ($smallest_minimum === 'none'){
										$smallest_minimum = $howmuch;
									} else if ($smallest_minimum > $howmuch) {
										$smallest_minimum = $howmuch;
									}
								} else if ($minimum_maximum === 'maximum_order'){
									if ($largest_maximum === 'none'){
										$largest_maximum = $howmuch;
									} else if ($largest_maximum < $howmuch) {
										$largest_maximum = $howmuch;
									}
								}
							}
						}
					} 
				}
			}
		}
	}

	$meta_min = intval(b2bking()->get_product_meta_min($product_id));

	if ($meta_min !== false){
		if (is_numeric($smallest_minimum)){
			$smallest_minimum = min($smallest_minimum, $meta_min);
		} else {
			$smallest_minimum = $meta_min;
		}
	}


	if ($smallest_minimum !== 'none'){

		$text = '<span class="b2bking_moq_text">'.esc_html__('Minimum Order Quantity: ', 'b2bking').$smallest_minimum.'</span>';
		$text = apply_filters('b2bking_external_moq_text', $text, $smallest_minimum );
		echo $text;

	}
});
Leave a Comment