skip to Main Content

I need to change something here to show default variation price on my store page:

<div class="deal-part2-details">
<?php
if($product->get_type() == "simple"){
?>
    <h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $product->regular_price; ?>pm</h5>
    <h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
    <h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
        <?php
            }else{
            $product_variations = $product->get_available_variations();
            $variation_product_id = $product_variations [0]['variation_id'];
            $variation_product = new WC_Product_Variation( $variation_product_id );
                                                    
        ?>
    <h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $variation_product->regular_price; ?>pm</h5>
    <h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $variation_product->sale_price; ?>pm</h5>
                                                    
<!--<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> -->
                                                    
<h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
                }
        ?>
</div>

Would you be able to help me?

2

Answers


  1. Chosen as BEST ANSWER

    I just change code like that, and working for me:

    <div class="deal-part2-details">
    <?php
    if($product->get_type() == "simple"){
    ?>
        <h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $product->regular_price; ?>pm</h5>
        <h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
        <h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
            <?php
                }else{
                $product_variations = $product->get_available_variations();
                $variation_product_id = $product_variations [0]['variation_id'];
                $variation_product = new WC_Product_Variation( $variation_product_id );
                $price_regular_min = $product->get_variation_regular_price();
                $price_sale_min = $product->get_variation_sale_price();
            ?>
        <h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency;  echo $price_regular_min; ?>pm</h5>
        <h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>
                                                        
    <!--<?php if ( $price_html = $product->get_price_html() ) : ?>
    <span class="price"><?php echo $price_html; ?></span>
    <?php endif; ?> -->
                                                        
    <h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
    <?php
                    }
            ?>
    </div>

    For that point I just add two variable:

    $price_regular_min = $product->get_variation_regular_price();
    $price_sale_min = $product->get_variation_sale_price();

    And then I activated them here:

    <h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $price_regular_min; ?>pm</h5>
    <h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>

    I made these changes in my loop-start.php This is my simple solution and working pretty well.


  2. Solution

    Step1:
    1st you can edit variable product & select variation values. Which variation
    you want shown on product page Default Form Values See the screenshot Select Default value screenshot

    Step:2
    Then go to your active theme or child theme functions.php file put below code.

    //woo_default_price_variation_price
    
    add_filter('woocommerce_variable_price_html', 'woo_default_price_variation_price', 10, 2);
    
    function woo_default_price_variation_price( $price, $product ) {
    foreach($product->get_available_variations() as $pav){
        $def=true;
        foreach($product->get_variation_default_attributes() as $defkey=>$defval){
           if($pav['attributes']['attribute_'.$defkey]!=$defval){
              $def=false; 
        }
    }
          if($def){
              $price = $pav['display_price']; 
          }
      } 
      return woocommerce_price($price);
    }
    

    It’s 100% working

    Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search