Answer a question

I have received a message from WordPress about a fatal error. It relates to a particular page in the backend in the WPML string translation plugin. I think a particular translation:

visit the page where the error was caught (xxxxxxxx/wp-admin/admin.php?page=wpml-translation-management%2Fmenu%2Ftranslations-queue.php&return_url=%2Fwp-admin%2Fpost.php%3Fpost%3D117195%26action%3Dedit%26lang%3Den%26message%3D6&job_id=3741&update_needed=1&trid=129987&language_code=fr)

It also says

Error Details ============= An error of type E_ERROR was caused in line 259 of the file /nas/content/live/mywebsite/wp-content/themes/babasouk/functions.php. Error message: Uncaught Error: Call to a member function get_stock_quantity() on null in /nas/content/live/mywebsite/wp-content/themes/mytheme/functions.php:259 Stack trace: #0 /nas/content/live/mywebsite/wp-includes/class-wp-hook.php(303): bbloomer_custom_get_availability_text('100 in stock', NULL)

#1 /nas/content/live/mywebsite/wp-includes/plugin.php(189): WP_Hook->apply_filters('100 in stock', Array)

#2 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2082): apply_filters('woocommerce_get...', '100 in stock', Object(WC_Product_Variation))

#3 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2058): WC_Product->get_availability_text()

#4 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/wc-template-functions.php(3512): WC_Product->get_availability()

#5 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/class-wc-product-variable.php(367): wc_get_stock_html(Object(W

In my PHP I echo get_stock_quantity()and it returns a value. I also tried echo of $product and returned everything. I disabled it because it is too much information. Website is here. https://babasoukstage.wpengine.com/shop/product-category/decor/moroccan-kilim-pillows/

Could the problem be the '' empty value I am declaring for translation? __( '', 'babasouk' );

My PHP is

function bbloomer_show_stock_shop() {
  global $product;
  echo wc_get_stock_html( $product );
}
// CHANGE STOCK MESSAGES
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );

function bbloomer_custom_get_availability_text( $availability, $product ) {
  global $product; // TO MAKE CATEGORY AVAILABLE
  $stock = $product->get_stock_quantity(); //THIS IS LINE 259
    //echo $product; does echo all product details when active
    echo $stock;//test to see if stock quantity returned. Does display on category and product pages.
  // OUTSIDE THE CATEGORY EXCLUSION SO THEY APPLY TO ALL 
  if ( !$product->is_in_stock() )$availability = __( 'Sorry, I am sold out!', 'babasouk' ); //OUT OF STOCK MESSAGE
    if ( $product->is_in_stock() && ( $stock >= 2 ) )$availability = __( '', 'babasouk' ); //NO MESSAGE IF STOCK OVER 1
  //EXCLUDE THESE CATEGORIES FOR 1 ITEM LEFT MESSAGE. IF THESE CATEGORIES MESSAGE IS BLANK.
  if ( has_term( array( 'ORIGINAL ARTWORKS', 'OEUVRES ORIGINALES' ), 'product_cat' ) ) {
    if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( '', 'babasouk' ); //
  } //IF NOT EXCLUDED CATEGORIES MESSAGE IS 'Only 1 Available!'
  else if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( 'Only 1 Available!', 'babasouk' );
  return $availability;
}

Answers

You have an error in your code and the logs are telling you the issue as well. Since you are in the functions.php file, there is no global $product available. Because of this you get a null pointer since you overwrite the available product variable with nothing. When I check the snippet on the page you got it from, there is also no global $product used - just wondering...

When you check the hook, you can see that there is the product already passed to the function. Just use it:

add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, WC_Product $product ) {
    $stock = $product->get_stock_quantity(); //THIS IS LINE 259
    //echo $product; does echo all product details when active
    error_log( $stock );//test to see if stock quantity returned. Does display on category and product pages.
    // OUTSIDE THE CATEGORY EXCLUSION SO THEY APPLY TO ALL
    if ( $product->is_in_stock() ) {
        if ( $stock > 1 ) {
            $availability = __( '', 'babasouk' );
        }

        if ( $stock === 1 ) {
            if ( has_term( [ 'ORIGINAL ARTWORKS', 'OEUVRES ORIGINALES' ], 'product_cat', $product ) ) {
                $availability = __( '', 'babasouk' );
            } else {
                $availability = __( 'Only 1 Available!', 'babasouk' );
            }
        }
    } else {
        $availability = __( 'Sorry, I am sold out!', 'babasouk' );
    }

    return $availability;
}

I have also improved your code a bit. At the end your function makes not very much sense to me but never mind - just to show you a better way of nesting and speed improvements. If you call a function only once instead multiple times you can improve your page speed.

Logo

WooCommerce社区为您提供最前沿的新闻资讯和知识内容

更多推荐