Display product prices with a shortcode by product ID in WooCommerce

前端 未结 4 1733
迷失自我
迷失自我 2021-01-06 11:05

IN WooCommerce I am using the code of this tread to display with a short code the product prices from a defined product ID. But it don\'t really do what I want. Here is that

4条回答
  •  花落未央
    2021-01-06 11:49

    OK, a friend of me helped me, and gave me the good code. If it help someone else ... Here it is:

    function so_30165014_price_shortcode_callback( $atts ) {
        $atts = shortcode_atts( array('id' => null,), $atts, 'bartag' );
        $html = '';
    
        if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){ 
              $_product = wc_get_product( $atts['id'] ); 
    
          $price = $_product->get_price();
          $regular_price = $_product->get_regular_price();
    
              $price_format = number_format($_product->get_price(), 2, '.', ','); 
              $regular_price_format = number_format($_product->get_regular_price(), 2, '.', ',');
    
          if($price != $regular_price){
            $html .= "".$regular_price_format." €"."";  
          } 
          $html .= " ". $price_format." €".""; 
      } 
      return $html; 
    } 
    
    add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
    

    bye !

提交回复
热议问题