skip to Main Content

I am trying to add function which will show the price of a product just below the sale badge, but i am having trouble with the code.. it works just fine in frontend, but wordpress find it wrong and shows error in backend (when I open the plugin settings).

What is the right way to add this?

Fatal error: Uncaught Error: Call to a member function get_price_html() on null in /wp-content/plugins/yith-woocommerce-badge-management-premium/templates/badge_content_premium.php:36 Stack trace: #0 
/wp-content/plugins/yith-woocommerce-badge-management-premium/functions.yith-wcbm.php(205): include() #1 
/wp-content/plugins/yith-woocommerce-badge-management-premium/functions.yith-wcbm-premium.php(807): yith_wcbm_get_template('badge_content_p...', Array) #2 
/wp-content/plugins/yith-woocommerce-badge-management-premium/class.yith-wcbm-admin-premium.php(109): yith_wcbm_get_badge_premium(364, 'preview') #3 
/wp-includes/class-wp-hook.php(286): YITH_WCBM_Admin_Premium->badge_custom_columns('yith_wcbm_previ...' in 
/wp-content/plugins/yith-woocommerce-badge-management-premium/templates/badge_content_premium.php on line 36

Here is the code i am adding on line 36

<div class='<?php echo $badge_classes ?> yith-wcbm-badge-custom' <?php echo $position_data_html ?>>

            <div class='yith-wcbm-badge__wrap'>
                <div class="yith-wcbm-badge-text"><b><?php echo $text ?>
LINE 36-      <div class="roundedborder"><?php echo $product->get_price_html(); ?></b></div></div>
            </div><!--yith-wcbm-badge__wrap-->
        </div><!--yith-wcbm-badge-->

I’ve tried to define $product and obviously i do something wrong also tried global $woocommerce; $product = new WC_Product(get_the_ID()); none of them solves the problem

2

Answers


  1. It seems like object $product not exists.
    Here is the same problem, may be helps you:

    Fatal error: Call to a member function get_price_html() on a non-object

    <?php
        global $woocommerce;
        $product = new WC_Product(get_the_ID()); 
        echo $product->get_price_html(); //Shows the price
    ?>
    
    Login or Signup to reply.
  2. This Error is generated because your $product is null, try to get the product object than get the price

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