skip to Main Content

I have this function and I want to add next to the product price.
I used get_price_html() and $price = $product->get_price_html

function woocommerce_template_loop_product_title() {
echo '<h4 class="woocommerce-loop-product__title">' . get_the_title() . '</h4>'; }

Current Output

Current Output

Desired Output

Desired Output

3

Answers


  1. Chosen as BEST ANSWER
    function woocommerce_template_loop_product_title() {
    global $product;
    $price = $product->get_price_html();
    echo '<h4 class="woocommerce-loop-product__title h-price">' . get_the_title() .'&nbsp'. $price .'</h4>';}
    

    My answer.


  2. You’ll need to add an action to change the title. So I wrote this code for you, simply add this to your functions.php

    add_action( 'the_title', 'add_price_title' );
    
    function add_price_title($title) {
     $post_ID = get_the_ID();
     $the_post = get_post($post_ID);
     $date = $the_post->post_date;
     $maintitle = $the_post->post_title;
     $count='';
     $product = wc_get_product( $post_ID );
     $price = $product->get_price();
    
    
    if ($the_post->post_status == 'publish' AND $the_post->post_type == 'product' AND in_the_loop()) { 
     return "<span type='number' class='notbold'>".$title." $".$price.""."</span>";
    }
    
    else{
     return $title;   
    }
    }
    
    Login or Signup to reply.
  3. // define the woocommerce_shop_loop_item_title callback 
    function action_woocommerce_shop_loop_item_title() { 
        // make action magic happen here... 
        global $product;
        echo '<p>'.get_post_meta($product->get_id(), '_price', true).'</p>';
    }; 
    
    // add the action 
    add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 10 ); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search