Avoid PHP notice when getting order item type

This commit changes WC_Order_Item_Data_Store::get_order_item_type() to
avoid a PHP notice ("Notice: Trying to get property 'order_item_type' of
non-object") when there is no entry in the database for the order item
ID passed.
This commit is contained in:
Rodrigo Primo 2019-08-07 18:03:26 -03:00
parent bccb7c3283
commit 21418bb28b
1 changed files with 9 additions and 3 deletions

View File

@ -145,11 +145,17 @@ class WC_Order_Item_Data_Store implements WC_Order_Item_Data_Store_Interface {
* *
* @since 3.0.0 * @since 3.0.0
* @param int $item_id Item ID. * @param int $item_id Item ID.
* @return string * @return string|null Order item type or null if no order item entry found.
*/ */
public function get_order_item_type( $item_id ) { public function get_order_item_type( $item_id ) {
global $wpdb; global $wpdb;
$item_data = $wpdb->get_row( $wpdb->prepare( "SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item_id ) ); $order_item_type = $wpdb->get_var(
return $item_data->order_item_type; $wpdb->prepare(
"SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;",
$item_id
)
);
return $order_item_type;
} }
} }