Untitled
unknown
plain_text
3 years ago
4.7 kB
7
Indexable
add_filter( 'woocommerce_get_price_html', 'b2bking_show_both_prices2', 99999, 2);
function b2bking_show_both_prices2($price, $product){
$group_id = 308;
// do not apply to B2B
$user_id = get_current_user_id();
$is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);
if ($is_b2b === 'yes'){
return $price;
}
$product_id = $product->get_id();
$retail_price = $price;
$retail_text = get_option('b2bking_retail_price_text_setting', esc_html__('Retail price','b2bking')).': ';
$wholesale_text = get_option('b2bking_wholesale_price_text_setting', esc_html__('Wholesale price','b2bking')).': ';
// variable product
if( $product->is_type('variable') ){
$children = $product->get_children();
$min_price = 0;
$max_price = 0;
foreach ($children as $variation_id){
// get retail price
$variation = wc_get_product($variation_id);
$variation_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_regular_product_price_group_'.$group_id, true)));
$variation_price_temp = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_sale_product_price_group_'.$group_id, true)));
if (!empty($variation_price_temp) && apply_filters('b2bking_use_retail_sale_price', true)){
// there is indeed a sale price
$variation_price = $variation_price_temp;
}
if ($max_price === 0){
$min_price = $max_price = $variation_price;
} else {
if ($variation_price < $min_price){
$min_price = $variation_price;
}
if ($variation_price > $max_price){
$max_price = $variation_price;
}
}
}
// apply tax
if (apply_filters('b2bking_both_prices_retail_adjust_tax', true)){
$min_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $min_price ) );
$max_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $max_price ) );
}
// if min and max prices are different, show range, else show the price
if ($min_price !== $max_price){
$wholesale_price= wc_format_price_range( $min_price, $max_price );
} else {
$wholesale_price = wc_price($min_price);
if ($min_price === 0){
$wholesale_price = ''; // do not show
}
}
} else {
// Simple product
// get retail price
$wholesale_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'b2bking_regular_product_price_group_'.$group_id, true)));
$wholesale_sale_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'b2bking_sale_product_price_group_'.$group_id, true)));
if (!empty($wholesale_price) && floatval($wholesale_price) !== 0){
if (!empty($wholesale_sale_price) && apply_filters('b2bking_use_retail_sale_price', true)){
// if there is a sale price
if (apply_filters('b2bking_both_prices_retail_adjust_tax', true)){
$wholesale_price = wc_format_sale_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_price ) ), b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_sale_price ) ));
} else {
$wholesale_price = wc_format_sale_price($wholesale_price, b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_sale_price ) ));
}
} else {
if (apply_filters('b2bking_both_prices_retail_adjust_tax', true)){
$wholesale_price = wc_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_price )));
} else {
$wholesale_price = wc_price($wholesale_price);
}
}
}
}
if (($wholesale_price !== $retail_price && $retail_price !== 0 && $retail_price !== wc_price(0) && $retail_price !== '' && $wholesale_price !== '' && !b2bking()->price_difference_may_be_rounding_error($wholesale_price, $retail_price)) || (apply_filters('b2bking_always_show_both_prices', false))){
if ( ! b2bking()->price_is_already_formatted($wholesale_price)){
$wholesale_price = wc_price($wholesale_price);
}
$wholesale_price = apply_filters('b2bking_filter_wholesale_price', $wholesale_price, $product_id);
$price = '<span class="b2bking_both_prices_text b2bking_retail_price_text">'.$retail_text .'</span><span class="b2bking_both_prices_price b2bking_retail_price_price">'. $retail_price . '<br></span><span class="b2bking_both_prices_text b2bking_b2b_price_text">'.$wholesale_text.'</span><span class="b2bking_both_prices_price b2bking_b2b_price_price b2bking_b2b_price_id_'.esc_attr($product_id).'">'.$wholesale_price.'</span>';
$price = apply_filters('b2bking_filter_wholesale_price_final', $price, $retail_text, $retail_price, $wholesale_text, $wholesale_price, $product_id);
}
return $price;
}Editor is loading...