Fatal error uncaught error call to a member function get_price() on null on WooCommerce

HI,
I am using Flatsome theme with WooCommerce and I am working on WooCommerce API calls.
For Orders and Customers, it is working perfectly.
However, I get this error for Products:

fatal error uncaught error call to a member function get_price() on null in home/xxx/public_html/wp-content/plugins/woocommerce/inc/wc-products-functions.php 1046.

I have added the following code in this file location: /home/xxx/public_html/wp-content/themes/flatsome/woocommerce/single-product/price.php.

<?php
function sv_change_product_price_display( $price ) {
    global $product;
    $price_excl_tax = wc_get_price_excluding_tax( $product ); 
    $price_incl_tax = wc_get_price_including_tax( $product );  
    <p class="price">Ex. VAT:<?php echo wc_price( $price_excl_tax ); ?>
    <p class="price-incl">Incl. VAT: <?php echo wc_price( $price_incl_tax ); ?>
    <?php 
    //return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );

I have added this code functions.php files:

<?php
function sv_change_product_price_display( $price ) {
    global $product;
    $price_excl_tax = wc_get_price_excluding_tax( $product ); 
    $price_incl_tax = wc_get_price_including_tax( $product );  
    <p class="price">Ex. VAT:<?php echo wc_price( $price_excl_tax ); ?>
    <p class="price-incl">Incl. VAT: <?php echo wc_price( $price_incl_tax ); ?>
    <?php 
    //return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );

What I am doing wrong?

Thanks

Well for starters, your function is trying to output things without breaking from the PHP.

<?php
//Inside PHP...
function sv_change_product_price_display( $price ) {
    global $product;
    $price_excl_tax = wc_get_price_excluding_tax( $product ); 
    $price_incl_tax = wc_get_price_including_tax( $product );  //Still inside PHP...
   //Why is there HTML here?                  why is there a PHP tag inside PHP?
 <font color='red'><p class="price">Ex. VAT:<?php echo wc_price( $price_excl_tax ); ?>

Second of all, what is the value of $product? (and is the value of product what you THINK it is? Double check!

Thanks and please check this following errors

Fatal error : Uncaught ArgumentCountError: Too few arguments to function sv_change_product_price_display(), 1 passed in /home/symeyoc1/public_html/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected in /home/symeyoc1/public_html/wp-content/themes/flatsome-child/functions.php:3 Stack trace: #0 /home/symeyoc1/public_html/wp-includes/class-wp-hook.php(289): sv_change_product_price_display(‘<span class="wo…’) #1 /home/symeyoc1/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(‘<span class="wo…’, Array) #2 /home/symeyoc1/public_html/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(1803): apply_filters(‘woocommerce_get…’, ‘<span class="wo…’, Object(WC_Product_Simple)) #3 /home/symeyoc1/public_html/wp-content/themes/flatsome/woocommerce/content-widget-product.php(40): WC_Product->get_price_html() #4 /home/symeyoc1/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(344): include(‘/home/symeyoc1/…’) #5 /home/symeyoc1/public_html/wp-content/plugins/ in /home/symeyoc1/public_html/wp-content/themes/flatsome-child/functions.php on line 3

i have added this code:

<?php
add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );
function sv_change_product_price_display( $price, $product  ) {
  global $product;
  if ( $product->is_type('variable') ) return $price;
  $price_excl_tax = wc_get_price_excluding_tax( $product ); 
  $price_incl_tax = wc_get_price_including_tax( $product ); 
  $price = '<p class="price">' . __("Ex. VAT:") . wc_price( $price_excl_tax ) . '</p>';
  $price .= '<p class="price-incl">' . __("Incl. VAT:") . wc_price( $price_incl_tax ) . '</p>';

   return $price;
 }

please suggest me,
Thanks

Your error messages keep pointing me in the direction that $product is null. The stack trace seems to say that apply_filters did not send the product to your function; beyond that, someone with more knowledge of what wordpress does under the hood will have to weigh in, because I don’t know the appropriate method of handling this.

OK Bro but, Thanks for your help

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.