• Resolved Pradeep

    (@pmaheepala)


    Hello there,

    We have a custom snippet on our site to format prices with and without taxes and whenever we enable that snippet, your search plugin throws the following fatal error.

    Fatal error: Uncaught Error: Call to a member function get_price() on null in /home/customer/www/abcd.com/public_html/wp-content/plugins/woocommerce/includes/wc-product-functions.php:1047 Stack trace: #0 /home/customer/www/abcd.com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code(6): wc_get_price_excluding_tax(NULL) #1 /home/customer/www/abcd.com/public_html/wp-includes/class-wp-hook.php(287): pm_price_formatting('<span class="wo...', NULL) #2 /home/customer/www/abcd.com/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('<span class="wo...', Array) #3 /home/customer/www/abcd.com/public_html/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(1803): apply_filters('woocommerce_get...', '<span class="wo...', Object(WC_Product_Simple)) #4 /home/customer/www/abcd.com/public_html/wp-content/plugins/advanced-woo-search/includes/class-aws-search.php(572): WC_Product->get_price_html() #5 /home/custom in /home/customer/www/abcd.com/public_html/wp-content/plugins/woocommerce/includes/wc-product-functions.php on line 1047

    Here’s the custom snippet:

    add_filter( 'woocommerce_get_price_html', 'pm_price_formatting', 99, 2 );
      
    function pm_price_formatting ( $price, $product ){
    	global $product;
    	$currency = esc_attr( get_woocommerce_currency( ) ); // Get the currency code.
    	$price_excl_tax = number_format (wc_get_price_excluding_tax( $product ), 2); // price without VAT
    	$price_incl_tax = number_format (wc_get_price_including_tax( $product ), 2);  // price with VAT
    	$int = wc_get_price_decimals();
        $price = '<span class="price-without-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) .$price_excl_tax.' (Exc. VAT)'.'</span>'.' | '.'<span class="price-with-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) . $price_incl_tax.' (Inc. VAT)'.'</span>';
        return $price;
    }

    Could you please let us know how to get this issue rectified?

    Many thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    Product search results displayed outside the woocommerce loop so global $product is equal to null here.

    What you can do here is first update your code with

    add_filter( 'woocommerce_get_price_html', 'pm_price_formatting', 99, 2 );
    function pm_price_formatting ( $price, $product ){
        global $product;
        if ( $product ) {
            $currency = esc_attr( get_woocommerce_currency( ) ); // Get the currency code.
            $price_excl_tax = number_format (wc_get_price_excluding_tax( $product ), 2); // price without VAT
            $price_incl_tax = number_format (wc_get_price_including_tax( $product ), 2);  // price with VAT
            $int = wc_get_price_decimals();
            $price = '<span class="price-without-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) .$price_excl_tax.' (Exc. VAT)'.'</span>'.' | '.'<span class="price-with-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) . $price_incl_tax.' (Inc. VAT)'.'</span>';
        }
        return $price;
    }

    and also add this addition one

    add_filter( 'aws_search_pre_filter_products', 'my_aws_search_pre_filter_products' );
    function my_aws_search_pre_filter_products( $products_array ) {
        if ( $products_array && ! empty( $products_array ) ) {
            foreach ( $products_array as $key => $product ) {
                $product_obj = wc_get_product( $product['id'] );
                if ( $product_obj ) {
                    $currency = esc_attr( get_woocommerce_currency( ) ); // Get the currency code.
                    $price_excl_tax = number_format (wc_get_price_excluding_tax( $product_obj ), 2); // price without VAT
                    $price_incl_tax = number_format (wc_get_price_including_tax( $product_obj ), 2);  // price with VAT
                    $int = wc_get_price_decimals();
                    $price = '<span class="price-without-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) .$price_excl_tax.' (Exc. VAT)'.'</span>'.' | '.'<span class="price-with-tax">'.esc_attr( get_woocommerce_currency_symbol( $currency )) . $price_incl_tax.' (Inc. VAT)'.'</span>';
                    $products_array[$key]['price'] = $price;
                }
            }
        }
        return $products_array;
    }

    It will add pricing changes inside ajax results block.

    Thread Starter Pradeep

    (@pmaheepala)

    Hello @mihail-barinov ,

    Many thanks for the quick and prompt reply. Your workaround worked like a charm! Keep up the good work!

    Best regards,

    Pradeep

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Uncaught Error: Call to a member function get_price()’ is closed to new replies.