I having issue for load prices in price comparison chart or anywhere with static product id, sometime we required this becuse we are selling limited membership or product in woocommerce and we just need to load price with different currency based on user location. so in this case we are not able to write statically in website, i have written solution for this type of case in WordPress woocommerce.
Now let’s learn how to create a simple plugin:
In wordpress plugin creation is very easy and anyone can do for custom requirements. you just need to create directory with your plugin name like “Show Woo Product Price” so here we can create a directory with “show-woo-product-price” and then inside of that same directory we can create a php file as “show-woo-product-price.php”, now we need to mention plugin name and details as a comment in that file at top.
<?php
/**
* Plugin Name: Show Woo Product Price
* Plugin URI: https://www.gatetouch.com
* Description: this will show woocomerce product price with different cases.
* Version: 1.0.1
* Author: Parbat Pithiya
* Author URI: https://9code.info
* Text Domain: 9code
* Domain Path: /
* License: GPL2
*/
Here is code for our need as show product price using shortcode with currency converted
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function gt_custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
'price_type'=>"sale",
"decimals"=>2,
"decimal_separator"=>".",
"show_currency"=> 1,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'decimal_separator' => $atts["decimal_separator"],
'thousand_separator' => ' ',
'decimals' => $atts["decimals"],
'price_format' => ((intval($atts["show_currency"]))?'%1$s%2$s':'%2$s'),
);
if( $atts['price_type']=="sale"){
$html = wc_price( $sale_price, $args );
}
else if( $atts['price_type']=="regular"){
$html = wc_price( $price, $args );
}
else if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<del tyle='opacity:0.8;'>" . wc_price( $regular_price, $args ) . "</del> <ins>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
else
$html = "<ins>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
}
return $html;
}
add_shortcode( 'product_price', 'gt_custom_price_shortcode_callback' );
Okay so save this file and active this plugin so your shortcode is ready and available to use as below
[product_price id="<product id>" price_type="<write sale/normal/default>" decimals="<write 2/0 >" decimal_separator="<write ./, >" show_currency="< 0/1 >"]
You looking to download this plugin by clicking below button.
Leave a Reply