Advertisement
palsushobhan

product price shortcode

May 28th, 2021
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. add_shortcode( 'woo_product_price', function($atts) {
  2.     global $post;
  3.     $atts = shortcode_atts( array(
  4.         'id' => null,
  5.     ), $atts, 'woo_product_price' );
  6.  
  7.     $html = '';
  8.  
  9.     if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
  10.         $product = wc_get_product( intval( $atts['id'] ) );
  11.     } elseif( is_product() ) {
  12.         $product = wc_get_product($post);
  13.     }
  14.    
  15.     if ( ! is_a( $product, 'WC_Product' ) ) return;
  16.  
  17.     // Get the product prices
  18.     $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
  19.     $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
  20.     $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
  21.  
  22.     // Your price CSS styles
  23.     $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
  24.     $style2 = 'style="font-size:25px;color:#e79a99"';
  25.  
  26.     // Formatting html output
  27.     if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
  28.         $html = "<del $style2>" . wc_price( $regular_price ) . "</del> <ins $style1>" . wc_price( $sale_price ) . "</ins>"; // Sale price is set
  29.     else
  30.         $html = "<ins $style1>" . wc_price( $price ) . "</ins>"; // No sale price set
  31.    
  32.     return $html;
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement