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
get_price_html()
$price = $product->get_price_html
function woocommerce_template_loop_product_title() { echo '<h4 class="woocommerce-loop-product__title">' . get_the_title() . '</h4>'; }
Current Output
Desired Output
3
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() .' '. $price .'</h4>';}
My answer.
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; } }
// 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 );
Click here to cancel reply.
3
Answers
My answer.
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