0

I have used & customised code from this page to show current stock status in single order details page. Generally it's working good until order get cancelled or refunded status. Current code:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'Stock', 'woocommerce' );
    
    // Display the column name
    echo '<th class="my-class">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

//Add content
function action_woocommerce_admin_order_item_values($product, $item, $item_id  ) {
    // Only for "line_item" items type, to avoid errors
    if ( ! $item->is_type('line_item') ) return; (this is line 611)
    
    // Get value
    $value = $product->get_stock_quantity();
    
    // NOT empty
    if($value>0) {
        echo '<td>' . $value . '</td>';
    } else {
        echo '<td>0</td>';        
    }
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

And the errors when order status is changed to refunded/cancelled:

Fatal error: Uncaught Error: Call to undefined method Automattic\WooCommerce\Admin\Overrides\OrderRefund::is_type() in /home/web/public_html/test/wp-content/themes/theme/functions.php:611 Stack trace: #0 /home/web/public_html/test/wp-includes/class-wp-hook.php(307): action_woocommerce_admin_order_item_values(NULL, Object(Automattic\WooCommerce\Admin\Overrides\OrderRefund), 397) #1 /home/web/public_html/test/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', Array) #2 /home/web/public_html/test/wp-includes/plugin.php(474): WP_Hook->do_action(Array) #3 /home/web/public_html/test/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-refund.php(50): do_action('woocommerce_adm...', NULL, Object(Automattic\WooCommerce\Admin\Overrides\OrderRefund), 397) #4 /home/web/public_html/test/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php(92): include('/home/mks/publi...') #5 /home/web/public_html/test/wp-content/plugins/woocommerce/includes/admin/m in /home/web/public_html/test/wp-content/themes/theme/functions.php on line 611

Notice: Trying to access array offset on value of type bool in /home/web/public_html/test/wp-includes/class-wp-recovery-mode-email-service.php on line 351

Notice: Trying to access array offset on value of type bool in /home/web/public_html/test/wp-includes/class-wp-recovery-mode-email-service.php on line 352
There has been a critical error on this website. Please check your site admin email inbox for instructions.

I have no idea how to change it or to disable function if order status is cancelled, refunded etc. I can't also find WooCommerce $item documentation. I have tried to disable function when there is a specific order status but without successes. Any ideas?

Marcin
  • 23
  • 4

0 Answers0