【问题标题】:Woocommerce variation price getWoocommerce 变化价格获取
【发布时间】:2016-08-11 09:45:00
【问题描述】:

我想获取实际的变化价格。

代码默认woocommerce(/plugins/woocommerce/templates/single-product/add-to-cart/variable.php):

<div class="woocommerce-variation-price">
 {{{ data.variation.price_html }}}  </div>

我如何获取这个“data.variation.price_html”?

我试过了:

$pricenow =  $product->get_variation_price();
<? echo $pricenow; ?>

但是,只显示最低价...

我该如何做到这一点,以便您喜欢的价格范围?

感谢您的帮助!

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    请将以下代码添加到您当前主题的 function.php 文件中,并在前端查看更改(即可变产品详细信息页面)

    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
    function display_price_in_variation_option_name( $term ) {
        global $wpdb, $product;
    
        if ( empty( $term ) ) return $term;
        if ( empty( $product->id ) ) return $term;
    
        $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    
        $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    
        $query = "SELECT postmeta.post_id AS product_id
                    FROM {$wpdb->prefix}postmeta AS postmeta
                        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                    WHERE postmeta.meta_key LIKE 'attribute_%'
                        AND postmeta.meta_value = '$term_slug'
                        AND products.post_parent = $product->id";
    
        $variation_id = $wpdb->get_col( $query );
    
        $parent = wp_get_post_parent_id( $variation_id[0] );
    
        if ( $parent > 0 ) {
             $_product = new WC_Product_Variation( $variation_id[0] );
             return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
        }
        return $term;
    }
    

    【讨论】:

      【解决方案2】:
      <?php 
                      global $product;
                      if ($product->is_type( 'simple' )) { ?>
                          <p class="price"><?php echo $product->get_price_html(); ?></p>
                      <?php } ?>
                      <?php 
                      if($product->product_type=='variable') {
                          $available_variations = $product->get_available_variations();
      $count = count($available_variations)-1;
                          $variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
                          $variable_product1= new WC_Product_Variation( $variation_id );
                          $regular_price = $variable_product1 ->regular_price;
                          $sales_price = $variable_product1 ->sale_price;
                          echo $regular_price+$sales_price;
                          }
                      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-28
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多