Search code examples
phpwoocommercenumber-formattinghidden

Change WooCommerce price display


Please allow me to provide additional information and I hope this helps.

Targets:

  1. Customize the shop page
  2. Make products unpurchasable for certain categories
  3. Hide price ranges on variables product and prices only appear when selected
  4. Change the display of prices on the shop page (Related with point 1 especially on prices, I created an attribute with a customized price value - 'pa_harga')

Which has been done :
---Code for point 1---

add_action('woocommerce_loop_add_to_cart_link','displaying_product_attributes',5);
function displaying_product_attributes () {
global $product;

$tipe_iklan_val = $product->get_attribute('pa_tipe-iklan');

$title_val = $product->get_title();

$harga_val = $product->get_attribute('pa_harga');

$harga2_val = $product->get_price_html();

$kt_val = $product->get_attribute('pa_kt');
$km_val = $product->get_attribute('pa_km');

$luas_bangunan_val = $product->get_attribute('pa_luas-bangunan');
$luas_tanah_val = $product->get_attribute('pa_luas-tanah');

$kelurahan_val = $product->get_attribute('pa_kelurahan');
$area_val = $product->get_attribute('pa_area');
    
$sku_val = $product->get_sku();


echo "<p style='color:#000; margin-left:5px; text-align:center; font-size:16px; height:24px; border:1px solid transparent; background-color:#e4e4e4; border-radius:5px; width:fit-content; width:-moz-fit-content; font-weight:600;padding-top:3px; padding-left:10px;padding-right:10px; margin-top:5px; letter-spacing:1.1px; box-shadow: 0px 0px 4px 1.5px rgb(0 0 0 / 0.8);'>".str_replace(',',' atau', $product->get_attribute .$tipe_iklan_val)."</p>";

echo "<h2 style='font-size:16px; margin-left:5px;text-overflow: ellipsis;overflow:hidden;white-space:nowrap;'>".$title_val."</h2>"; 

echo "<p style='color:#1ebf41; text-align:left;margin-left:6px; padding-top:10px; font-size:18px;line-height:0.9; font-weight:bold'>".$harga_val."</p>";

echo "<p style='color:#1ebf41; text-align:left;margin-left:6px; padding-top:10px; font-size:18px;line-height:0.9; font-weight:bold'>".$harga2_val."</p>";


echo "<p style='color:#333; text-align:left;margin-left:7px;font-size:14px;line-height: 0.1;'><i class='fas fa-bed'></i> &nbsp;".$kt_val."&emsp;<i class='fas fa-shower'></i> &nbsp;".$km_val."&emsp;<i class='fa fa-home'></i>&nbsp; ".$luas_bangunan_val."m<sup>2</sup>&emsp;<i class='fas fa-expand-arrows-alt'></i>&nbsp; ".$luas_tanah_val."m<sup>2</sup></p>";

echo "<p style='color:#333; text-align:left; margin-left:7px;font-size:14px;line-height: 1;'><i class='fas fa-map-marker-alt'></i>&nbsp; ".$kelurahan_val.",&nbsp;".$area_val."</p>";

echo "<p style='color:#333; text-align:left;margin-left:7px; font-size:14px;line-height: 0.8;'>ID : ".$sku_val."</p>";
}

See result > Point 1

---Code for point 2---

add_filter('woocommerce_is_purchasable','misha_catalog_mode_on_for_category',10, 2 );
function misha_catalog_mode_on_for_category( $is_purchasable, $product {

if( has_term( 'sold', 'product_cat', $product->get_id() ) ) {
return false;
}

return $is_purchasable;

}
add_filter( 'woocommerce_variable_sale_price_html','woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if( is_product_category('sold') || has_term( 'sold', 'product_cat',$product->get_id() ) )
$price = '';

return $price;  
}
add_action( 'woocommerce_single_product_summary','unavailable_product_display_message', 20 );
function unavailable_product_display_message() {
global $product;

if(! $product->is_purchasable() ){
echo '<p style="color:#e00000;font-size:20px;font-weight:600; font-family:verdana;">' . __("Unit telah terjual/Disewa.") . '</p>';
echo '<p style="background-color: #fff5ee; padding-top:6px; text-align:center; border: 1px solid transparent; border-radius: 8px; width: 250px; height: 40px; margin-left: 20px;font-size:15px;box-shadow:0px -2px 6px 0px rgb(210 0 1)"><a style="color: black;"href="http://www.wajaraja.com/hubungi-kami/">Tinggalkan pesan di sini</a></p>';
}
}

See result > Point 2

---Code for point 3---

//Hide Price Range for WooCommerce Variable Products
add_filter('woocommerce_variable_sale_price_html','lw_variable_product_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html','lw_variable_product_price', 10, 2 );

function lw_variable_product_price( $v_price, $v_product ) {

// Product Price
$prod_prices = array( $v_product->get_variation_price( 'min', true ), 
$v_product->get_variation_price( 'max', true ) );
$prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('Harga terendah : %1$s', 'woocommerce'), 
wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );

// Regular Price
$regular_prices = array( $v_product->get_variation_regular_price( 'min',true ), 
$v_product->get_variation_regular_price( 'max', true ) );
sort( $regular_prices );
$regular_price = $regular_prices[0]!==$regular_prices[1] ?
sprintf(__('Harga terendah : %1$s','woocommerce'), wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );

if ( $prod_price !== $regular_price ) {
$prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> <ins>' . 
$prod_price . $v_product->get_price_suffix() . '</ins>';
}
return $prod_price;
}


//Hide “From:$X”
add_filter('woocommerce_get_price_html', 'lw_hide_variation_price', 10, 2);
function lw_hide_variation_price( $v_price, $v_product ) {

$v_product_types = array( 'variable');
if ( in_array ( $v_product->product_type, $v_product_types ) && !(is_shop()) ) {
return '';
}
// return regular price
return $v_price;
}

See Result > Point 3 - before || Point 3 - After

---Code for point 4---

add_filter( 'woocommerce_get_price_html','rei_woocommerce_price_html', 9999, 2 );

function rei_woocommerce_price_html( $price, $product ) {
$currency = get_woocommerce_currency_symbol( );
$price = $currency . custom_number_format( floatval($product->get_price() ), 0 );
return $price;
}
function custom_number_format( $n, $precision = 0 ) {
if ( $n < 1000 ) {
    // Default
    $n_format = number_format( $n );
} elseif ( $n < 1000000 ) {
    // Thousand
    $n_format = number_format( $n / 1000, $precision ) . ' Ribu';
} elseif ( $n < 1000000000 ) {
    // Million
    $n_format = number_format( $n / 1000000, $precision ) . ' Juta';
} else if ( $n < 1000000000000 ) {
    // Billion
    $n_format = number_format( $n / 1000000000, $precision ) . ' M';
} else {
    // Trillion
    $n_format = number_format( $n / 1000000000000, $precision ) . ' T';
}
return $n_format;
}

(To see a comparison, I haven't removed the price built by the attribute product > Top price position)

Note: The code has worked fine. But I have trouble setting how many numbers to appear when condition it's thousand(Ribu), million(Juta), M, or T.

See result > Point 4

I want :

Rp 798,500,000 > Rp 798,5 juta
Rp 925,799,000 > Rp 925,8 juta
Rp 535,000,000 > Rp 535 juta
Rp 1,000,000,000 > Rp 1 M
Rp. 1,050,000,000 > Rp 1,05 M
Rp. 2,539,000,000 > Rp. 2,54 M

So I did a little modification and here's the code (Please correct me if I'm wrong)

---Code Point 4 > Revise---

add_filter( 'woocommerce_get_price_html','rei_woocommerce_price_html', 9999, 2 );
if (!is_shop()) return;
function rei_woocommerce_price_html( $price, $product ) {

$currency = get_woocommerce_currency_symbol( );
$price = $currency .' ' .custom_number_format( floatval( $product->get_price() ), 2 );
return $price;

}



     $n = 1000000;

    function custom_number_format($n) {

        $n = (0+str_replace(",","",$n));


        if(!is_numeric($n)) return false;


        if($n>1000000000000) return round(($n/1000000000000),1).' '.'-T';
        else if($n>1000000000) return round(($n/1000000000),2).' '.'M';
        else if($n>1000000) return round(($n/1000000),3).' '.'Juta';
        else if($n>1000) return round(($n/1000),1).' '.'Ribu';

        return number_format($n);
    }

     $v = custom_number_format($n);
    echo  $v;

See Result > Point 4 - revise

At this point I feel I have reached the target.
But when I browsed through each product, I felt something was off and it needed to be fixed.
Code points 2 & 3 become not working properly. The additional code from point 4 & 4-revise has brought back the price that should have been hidden.
Here's what it looks like after code point 4 is added

See result > Related with point 2 || Related with point 3

Please help me so that the target for point 4 can be achieved without changing what is already there.


Solution

  • Cleaned a bit for you, plus changed the filter:

    add_filter( 'woocommerce_get_price_html','rei_woocommerce_price_html', 9999, 2 );
    
    function rei_woocommerce_price_html( $price, $product ) {
        $currency = get_woocommerce_currency_symbol( );
        $price = $currency . custom_number_format( floatval( $product->get_price() ), 0 );
        return $price;
    }
    
    function custom_number_format( $n, $precision = 0 ) {
        if ( $n < 1000 ) {
            // Default
            $n_format = number_format( $n );
        } elseif ( $n < 1000000 ) {
            // Thousand
            $n_format = number_format( $n / 1000, $precision ) . ' Ribu';
        } elseif ( $n < 1000000000 ) {
            // Million
            $n_format = number_format( $n / 1000000, $precision ) . ' Juta';
        } else if ( $n < 1000000000000 ) {
            // Billion
            $n_format = number_format( $n / 1000000000, $precision ) . ' M';
        } else {
            // Trillion
            $n_format = number_format( $n / 1000000000000, $precision ) . ' T';
        }
        return $n_format;
    }