2

After looking at the hooks used, the title has priority 5 and the price has priority 10. So, I did an remove_action and add_action and changed it to 6. That did not work.

What I need is this:

Product title: currency symbol price

Example:

Puzzle for kids: $29

The code I tried:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 6 );

I have tried this code as well now without any luck:

remove_action('woocommerce_single_product_summary','woocommerce_template_single_title', 5);
add_action('woocommerce_single_product_summary', 'woocommerce_new_single_title', 5);

        if ( ! function_exists( 'woocommerce_new_single_title' ) ) {
                function woocommerce_new_single_title() {
                    $product = wc_get_product( $post_id );
                    $product->get_regular_price();
                    $product->get_sale_price();
                    $product->get_price();
                    $pprice = $product->get_price();
            ?>
            <h1 itemprop="name" class="product_title entry-title"><span><?php the_title(); ?>:&nbsp;<?php $product->get_price(); ?></span></h1>
        <?php
    }
}

Any ideas where I am going wrong and how to fix this?

1 Answers1

4

To change an action, simply add your custom function to this action.

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

function show_title_with_price()
{
    global $product;
    $title = $product->get_title();
    $price = $product->get_regular_price();
    $symbol = get_woocommerce_currency_symbol();

    //You may change <p> tag or add any inline CSS here.
    echo "<p>$title: $symbol $price</p>";
}

add_action( 'woocommerce_single_product_summary', 'show_title_with_price', 5 ); 

This code removes the default title and default price sections (actions) from single product page and adds the custom function named "show_title_with_price()" to woocommerce_single_product_summary action with parameter '5' where the actual title is shown in product page.

Note: Code shows only regular price. I believe you already know how to show sale price if exists.

More about single product page actions: WooCommerce Single Product Page Default Actions

Woocommerce action hooks and overriding templates:Stackoverflow Reference

I tested the code, works fine. Have a good day.

MrEbabi
  • 740
  • 5
  • 16
  • Ah, thanks. It was "that easy" after all.. I made a few mistakes. Thanks a lot! –  Aug 22 '19 at 22:03