Add this code in function.php
function woocommerce_price_by_id_wps( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = 'Add product id';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
if (isset($_product) && !empty($_product)) {
$html = "price = " . $_product->get_price_html();
} else {
$html = "Wrong product id";
}
}
return $html;
}
add_shortcode( 'woocommerce_price', 'woocommerce_price_by_id_wps' );
Shortcode
[woocommerce_price id="29"]
=======================================================================
Another Way
<?php
$product_id =29;
$_product = wc_get_product( $product_id );
?>
<p class="price">MYPRICE <?php echo $_product->get_price_html(); ?></p>
function woocommerce_price_by_id_wps( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = 'Add product id';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
if (isset($_product) && !empty($_product)) {
$html = "price = " . $_product->get_price_html();
} else {
$html = "Wrong product id";
}
}
return $html;
}
add_shortcode( 'woocommerce_price', 'woocommerce_price_by_id_wps' );
Shortcode
[woocommerce_price id="29"]
=======================================================================
Another Way
<?php
$product_id =29;
$_product = wc_get_product( $product_id );
?>
<p class="price">MYPRICE <?php echo $_product->get_price_html(); ?></p>
Comments
Post a Comment