Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.7 kB
1
Indexable
Never

function b2bking_display_tier_table_in_category() {
    global $product;
    if ($product){
    	?>

    	<div class="tiered_table_archive"> <?php
	    $product_id = $product->get_id();

	    $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($product_id, 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true);
	    $price_tiers = b2bking()->convert_price_tiers($price_tiers, $product);

		if (!(!empty($price_tiers) && strlen($price_tiers) > 1 )){
			$price_tiers = get_post_meta($product_id, 'b2bking_product_pricetiers_group_b2c', true);
			$price_tiers = b2bking()->convert_price_tiers($price_tiers, $product);
		}

		$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){
			if (empty($pair)) {continue;}
			$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);
		}

		// finished sort
		$number_of_tiers = count($price_tiers_array);
		// only 1 tier
		if ($number_of_tiers === 1){
			$tier_values = explode(':', $price_tiers_array[0]);
			?>
			<tr>
				<td><?php 

				echo esc_html($tier_values[0]).apply_filters('b2bking_tiered_pricing_table_show_direct_qty', '+'); 

				$tier_values[1] = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $tier_values[1] ) ); // get sale price

				?></td>

				<td><?php 

				echo apply_filters('b2bking_tiered_pricing_table_price', wc_price(b2bking()->get_woocs_price($tier_values[1])), $tier_values[1], $tier_values[0], $product_id).'<br>';
				?>
				</td>
			</tr>
			<?php
		} else {
			$previous_tier = 'no';
			$previous_value = 'no';

			foreach ($price_tiers_array as $index => $tier){
				if (empty($tier)) {continue;}
				$tier_values = explode(':', $tier);
				if ($previous_tier !== 'no'){

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

					// check that tier price is better than group price, else don't show tier
					$show_row = 'yes';
					if (!empty($user_price)){
						if ($previous_value > $user_price){
							$show_row = 'no';
						}
					}
					if ($show_row === 'yes'){
						?>
							<tr>
								<td><?php
								if (b2bking()->tofloat($previous_tier) !== b2bking()->tofloat($tier_values[0]-1)){
									// do not show 1-1
									if ($previous_tier == $tier_values[0]-1 && $previous_tier == 1){
										echo 1;
									} else {
										// do not show 2-2 3-3 etc
										if ($previous_tier == $tier_values[0]-1){
											echo esc_html($previous_tier);
										} else {
											echo esc_html($previous_tier).apply_filters('b2bking_tiered_pricing_table_show_direct_qty', ' - '.esc_html($tier_values[0]-1));
										}
									}
								} else {
									echo esc_html(apply_filters('b2bking_tiered_pricing_table_show_direct_qty', $previous_tier));
								}

								?></td>
								
								<td><?php 

								echo apply_filters('b2bking_tiered_pricing_table_price', wc_price(b2bking()->get_woocs_price($previous_value)), $previous_value, $previous_tier, $product_id).'<br>';

								?>
									
								</td>
							</tr>
						<?php
					}
				}
				$previous_tier = $tier_values[0];
				$previous_value = $tier_values[1];

				// if this tier is the last tier
				if (intval($index+1) === intval($number_of_tiers)){

					?>
					<tr>
						<td><?php echo esc_html($previous_tier).apply_filters('b2bking_tiered_pricing_table_show_direct_qty','+'); ?></td>
						<?php 
						// adjust price for tax
						$previous_value = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $previous_value ) ); // get sale price

						?>
						<td><?php 

						echo apply_filters('b2bking_tiered_pricing_table_price', wc_price(b2bking()->get_woocs_price($previous_value)), $previous_value, $previous_tier, $product_id).'<br>';

						?>
							
						</td>

					</tr>
					<?php
				}
			}
		}
		?>
		</div>
		<?php
   }
}
add_action('woocommerce_single_product_summary', 'b2bking_display_tier_table_in_category', 20);
add_action('woocommerce_after_shop_loop_item_title', 'b2bking_display_tier_table_in_category', 20);
Leave a Comment