Khandaker Toihidul Islam
Front-end Developer
React Js | Javascript | HTML | CSS | Bootstrap | Wordpress | PHP | Mysql

Uncaught Error: Call to a member function get_stock_quantity()

Replace $product->get_stock_quantity() by $_product->get_stock_quantity().

function wcs_custom_get_availability( $availability, $_product ) {
    global $product;
    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __( 'Plenty available in our store!', 'woocommerce');
    }
         
    // Change in Stock Text to only 1 or 2 left
    if ( $_product->is_in_stock() && $_product->get_stock_quantity() <= 2 ) {
        $availability['availability'] = sprintf( __('Only %s left in store!', 'woocommerce'), $_product->get_stock_quantity() );
    }
         
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
    }
         
    return $availability;
}

You may also like...