Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.8 kB
7
Indexable
Never
add_filter('b2bking_applicable_rules_products', 'b2bking_discount_rules_sale_price', 10, 5);
function b2bking_discount_rules_sale_price($results, $rule_type, $product_id, $user_id, $categories_array){

	$user_id = get_current_user_id();
	$currentusergroupidnr = b2bking()->get_user_group($user_id);

	$b2b_regular_price = get_post_meta($product_id, 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true );
	$regular_price = get_post_meta($product_id,'_regular_price', true);
	if (!empty($b2b_regular_price)){
		$regular_price = $b2b_regular_price;
	}

	$b2b_sale_price = get_post_meta($product_id, 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true );
	$sale_price = get_post_meta($product_id,'_sale_price', true);

	// get current sale price
	$current_sale_price = '';
	if (!empty($b2b_sale_price)){
		$current_sale_price = $b2b_sale_price;
	} else {
		if (!empty($sale_price)){
			$current_sale_price = $sale_price;
		}
	}

	$current_sale_price = b2bking()->tofloat($current_sale_price);
	$regular_price = b2bking()->tofloat($regular_price);
	$rules = $results[0];

	// if there is a sale price, choose largest discount between sale price and rule
	if (!empty($current_sale_price)){
	    if ($rule_type == 'discount_everywhere'){
	    	foreach ($rules as $index => $rule_id){

	    		// calculate rule sale price
	    		$type = get_post_meta($rule_id, 'b2bking_rule_what', true);
	    		$howmuch = b2bking()->tofloat(get_post_meta($rule_id, 'b2bking_rule_howmuch', true));

	    		if ($type === 'discount_percentage'){
	    			$rule_sale_price = $regular_price * (100-$howmuch)/100;
	    		} else {
	    			$rule_sale_price = $regular_price - $howmuch;
	    		}

	    		if ($rule_sale_price > $current_sale_price){
	    			unset($rules[$index]);
	    		}
	    	}
	    }
	}

    return array($rules, $results[1]);
}
Leave a Comment