Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
3
Indexable
Never
add_action('b2bking_bulkorder_cream_custom_heading', function(){
	?>
	<div class="b2bking_bulkorder_form_container_content_header_qty b2bking_bulkorder_form_container_content_header_qty_indigo b2bking_bulkorder_form_container_content_header_qty_cream">
		<?php esc_html_e('Lowest Price', 'b2bking'); ?>
	</div>
	<?php
}, 100);
add_action('b2bking_bulkorder_cream_custom_column', function($product){

	$post_id = $product->get_id();

	// calculate lowest price
	$user_id = get_current_user_id();
	$user_id = b2bking()->get_top_parent_account($user_id);

	$currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id);
	$is_b2b_user = get_user_meta( $user_id, 'b2bking_b2buser', true );

	if ( apply_filters('b2bking_tiered_table_discount_uses_sale_price', $product->is_on_sale() ) ) {
		$original_user_price = get_post_meta($product->get_id(),'_sale_price',true);
	   	
		if ($is_b2b_user === 'yes'){
			// Search if there is a specific price set for the user's group
			$b2b_price = b2bking()->tofloat(get_post_meta($product->get_id(), 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true ));
								
			if (!empty($b2b_price)){
				$original_user_price = $b2b_price;
			}
		}

	} else {
		$original_user_price = get_post_meta($product->get_id(),'_regular_price',true);

		if ($is_b2b_user === 'yes'){
			// Search if there is a specific price set for the user's group
			$b2b_price = b2bking()->tofloat(get_post_meta($product->get_id(), 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true ));
								
			if (!empty($b2b_price)){
				$original_user_price = $b2b_price;
			}
		}
	}

	// adjust price for tax
	$original_user_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $original_user_price ) ); // get sale price

	$price_tiers = get_post_meta($post_id, 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true);

	$user_price = array();
	$grpriceexists = 'no';

	// if didn't find anything as a price tier + user does not have group price, give regular price tiers
	// if no tiers AND no group price exists, get B2C tiered pricing
	if ($currentusergroupidnr){
		$grregprice = get_post_meta($post_id, 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true );
		$grsaleprice = get_post_meta($post_id, 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true );
		if (!empty($grregprice) && b2bking()->tofloat($grregprice) !== 0){
			$grpriceexists = 'yes';	
		}
		if (!empty($grsaleprice) && b2bking()->tofloat($grsaleprice) !== 0){
			$grpriceexists = 'yes';	
		}
	}

	if (empty($price_tiers) && $grpriceexists === 'no'){
		$price_tiers = get_post_meta($product->get_id(), 'b2bking_product_pricetiers_group_b2c', true );
	}

	$price_tiers = b2bking()->convert_price_tiers($price_tiers, $product);

	if (!empty($price_tiers) && strlen($price_tiers) > 1 ){
		$price_tiers_array = explode(';', $price_tiers);
		$price_tiers_array = array_filter($price_tiers_array);

		// need to order this array by the first number (elemnts of form 1:5, 2:5, 6:5)
		$helper_array = array();							
		foreach ($price_tiers_array as $index=> $pair){
			$pair_array = explode(':', $pair);
			$helper_array[$pair_array[0]] = b2bking()->tofloat($pair_array[1], 4);
		}
		ksort($helper_array);
		$price_tiers_array = array();
		foreach ($helper_array as $index=>$value){
			array_push($price_tiers_array,$index.':'.$value);
		}

		$prices_array = array();

		foreach ($price_tiers_array as $index => $tier){
			$tier_values = explode(':', $tier);
			array_push($prices_array, $tier_values[1]);
		}

		if (!empty($prices_array)){
			$original_user_price = min($prices_array);
		}
	}

	?>
	<div class="b2bking_bulkorder_cream_stock"><?php echo wc_price($original_user_price);?></div>
	<?php
}, 100, 1);