Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
3.1 kB
1
Indexable
Never
add_action('b2bking_bulkorder_cream_custom_heading', function(){
	?>
	<div class="b2bking_bulkorder_form_container_content_header_cream_sku">
		<?php esc_html_e('Price', 'b2bking'); ?>
	</div>
	<div class="b2bking_bulkorder_form_container_content_header_cream_sku">
		<?php esc_html_e('Price Tier 1', 'b2bking'); ?>
	</div>
	<?php
});
add_action('b2bking_bulkorder_cream_custom_column', function($product){
	?>
	<div class="b2bking_bulkorder_cream_sku"><?php 

	$post_id = $product->get_id();

	$sale_price = $product->get_sale_price();
	$sale_price = round(floatval(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $sale_price))),2);
	$regular_price = $product->get_regular_price();
	$regular_price = round(floatval(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $regular_price))),2);

	if ($product->is_on_sale()){
		$price_display = wc_format_sale_price($regular_price, $sale_price);
	} else {
		$price_display = wc_price($regular_price);
	}

	echo $price_display;
	
	?></div>
	<div class="b2bking_bulkorder_cream_sku"><?php 

	$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);

	$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);
		}

		// get first price tier
		$tier_values = explode(':', $price_tiers_array[0]);

		echo $tier_values[0].'+ items <br> '.wc_price($tier_values[1]);

	}
	
	?></div>
	<?php
}, 10, 1);
Leave a Comment