Untitled
unknown
plain_text
2 years ago
12 kB
9
Indexable
function b2bking_has_fixed_price( $product ) {
if (!is_a($product,'WC_Product_Variation') && !is_a($product,'WC_Product')){
$product = wc_get_product($product);
}
$user_id = get_current_user_id();
$account_type = get_user_meta($user_id,'b2bking_account_type', true);
if ($account_type === 'subaccount'){
// for all intents and purposes set current user as the subaccount parent
$parent_user_id = get_user_meta($user_id, 'b2bking_account_parent', true);
$user_id = $parent_user_id;
}
$currentusergroupidnr = b2bking()->get_user_group($user_id);
if (!$currentusergroupidnr || empty($currentusergroupidnr)){
$currentusergroupidnr = 'invalid';
}
// Get current product
$current_product_id = $product->get_id();
$response = b2bking()->get_applicable_rules('fixed_price', $current_product_id);
// abort early
if ($response === 'norules'){
return false;
}
$fixed_price_rules = $response[0];
$current_product_belongsto_array = $response[1];
// if multiple fixed price rules apply, give the smallest price to the user
$have_fixed_price = NULL;
$smallest_fixed_price = 0;
$fixed_price_rules = b2bking()->get_rules_apply_priority($fixed_price_rules);
foreach ($fixed_price_rules as $fixed_price_rule){
// Get rule details
$type = get_post_meta($fixed_price_rule, 'b2bking_rule_what', true);
$howmuch = get_post_meta($fixed_price_rule, 'b2bking_rule_howmuch', true);
$applies = explode('_',get_post_meta($fixed_price_rule, 'b2bking_rule_applies', true));
$rule_multiple_options = get_post_meta($fixed_price_rule, 'b2bking_rule_applies_multiple_options', true);
$rule_multiple_options_array = explode(',',$rule_multiple_options);
$cart = WC()->cart;
// Get conditions
$passconditions = 'yes';
$conditions = get_post_meta($fixed_price_rule, 'b2bking_rule_conditions', true);
if (!empty($conditions)){
$conditions = explode('|',$conditions);
if ($applies[0] === 'multiple'){ // if rule is multiple products / categories rule
$temporary_pass_conditions = 'no'; // if at least 1 element passes, change to yes
// for each element that applies to the product and is part of the rule, check if at least 1 passes the conditions
foreach($current_product_belongsto_array as $element){
if(in_array($element, $rule_multiple_options_array)){
$element_array = explode('_', $element);
// if element is product or if element is category
if ($element_array[0] === 'product'){
$passes_inside_conditions = 'yes';
$product_quantity = 0;
if(is_object($cart)) {
foreach($cart->get_cart() as $cart_item){
if(intval($element_array[1]) === intval($cart_item['product_id'])){
$product_quantity = $cart_item["quantity"];// Quantity
break;
}
}
}
// check all product conditions against it
foreach ($conditions as $condition){
$condition_details = explode(';',$condition);
$condition_details[2] = b2bking()->get_woocs_price($condition_details[2]);
switch ($condition_details[0]){
case 'product_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($product_quantity > intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($product_quantity === intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($product_quantity < intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
break;
}
break;
case 'cart_total_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count > intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count === intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($cart->cart_contents_count < intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
break;
}
break;
}
}
if ($passes_inside_conditions === 'yes'){
$temporary_pass_conditions = 'yes';
break; // if 1 element passed, no need to check all other elements
}
} else if ($element_array[0] === 'category'){
// check all category conditions against it + car total conditions
$passes_inside_conditions = 'yes';
$category_quantity = 0;
if(is_object($cart)) {
foreach($cart->get_cart() as $cart_item){
if(b2bking()->b2bking_has_category($element_array[1], 'product_cat', $cart_item['product_id'])){
$category_quantity += $cart_item["quantity"]; // add item quantity
}
}
}
foreach ($conditions as $condition){
$condition_details = explode(';',$condition);
$condition_details[2] = b2bking()->get_woocs_price($condition_details[2]);
switch ($condition_details[0]){
case 'category_product_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($category_quantity > intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($category_quantity === intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($category_quantity < intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
break;
}
break;
case 'cart_total_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count > intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count === intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($cart->cart_contents_count < intval($condition_details[2]))){
$passes_inside_conditions = 'no';
break 3;
}
break;
}
break;
}
}
if ($passes_inside_conditions === 'yes'){
$temporary_pass_conditions = 'yes';
break; // if 1 element passed, no need to check all other elements
}
}
}
} //foreach element end
if ($temporary_pass_conditions === 'no'){
$passconditions = 'no';
}
} else { // if rule is simple product, category, or total cart role
$category_products_number = 0;
$category_products_value = 0;
$products_number = 0;
$products_value = 0;
// Check rule is category rule or product rule
if ($applies[0] === 'category'){
// Calculate number of products in cart of this category AND total price of these products
if(is_object($cart)) {
foreach($cart->get_cart() as $cart_item){
if(b2bking()->b2bking_has_category($applies[1], 'product_cat', $cart_item['product_id'])){
$item_qty = $cart_item["quantity"];// Quantity
$item_line_total = apply_filters('b2bking_line_total_rules', $cart_item["line_total"], $cart_item); // Item total price (price x quantity)
$category_products_number += $item_qty; // ctotal number of items in cart
$category_products_value += $item_line_total; // calculated total items amount
}
}
}
} else if ($applies[0] === 'product') {
if(is_object($cart)) {
foreach($cart->get_cart() as $cart_item){
if(intval($current_product_id) === intval($cart_item['product_id']) or intval($current_product_id) === intval($cart_item['variation_id'])){
$item_qty = $cart_item["quantity"];// Quantity
if (isset($cart_item['line_total'])){
$item_line_total = apply_filters('b2bking_line_total_rules', $cart_item["line_total"], $cart_item); // Item total price (price x quantity)
$products_value += $item_line_total; // calculated total items amount
}
$products_number += $item_qty; // ctotal number of items in cart
}
}
}
}
foreach ($conditions as $condition){
$condition_details = explode(';',$condition);
$condition_details[2] = b2bking()->get_woocs_price($condition_details[2]);
switch ($condition_details[0]){
case 'product_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($products_number > intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($products_number === intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($products_number < intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
break;
}
break;
case 'category_product_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($category_products_number > intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($category_products_number === intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($category_products_number < intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
break;
}
break;
case 'cart_total_quantity':
switch ($condition_details[1]){
case 'greater':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count > intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'equal':
if (intval($condition_details[2]) !== 0){
if (! ($cart->cart_contents_count === intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
}
break;
case 'smaller':
if (! ($cart->cart_contents_count < intval($condition_details[2]))){
$passconditions = 'no';
break 3;
}
break;
}
break;
}
}
}
}
// Passed conditions
if ($passconditions === 'yes'){
if ($have_fixed_price === NULL){
$have_fixed_price = 'yes';
$smallest_fixed_price = floatval($howmuch);
} else {
if (floatval($howmuch) < $smallest_fixed_price){
$smallest_fixed_price = floatval($howmuch);
}
}
} else {
// do nothing
}
} //foreach end
if($have_fixed_price !== NULL){
return $smallest_fixed_price;
} else {
return false;
}
}Editor is loading...