public function productByOrderItemId($item_id, \WC_Abstract_Order $WC_Order = null)
    {
        // WC 3.0+ uses `\WC_Order_Item_Product` extending `\WC_Order_Item`.
        // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html>
        // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item.html>

        if (!($item_id = (int) $item_id)) {
            return null; // Not possible.
        } elseif (!($WC_Order = $WC_Order ?: $this->orderByItemId($item_id))) {
            return null; // Not possible.
        }
        foreach ($WC_Order->get_items() as $_item_id => $_WC_Order_Item) {
            if ($_item_id === $item_id) {
                if ($_WC_Order_Item instanceof \WC_Order_Item_Product) {
                    $WC_Product = $_WC_Order_Item->get_product();
                    return $WC_Product instanceof \WC_Product && $WC_Product->exists() ? $WC_Product : null;
                    //
                } elseif ($_WC_Order_Item instanceof \WC_Order_Item) {
                    return null; // Not a product item.
                    //
                } else { // Back compat.
                    $_item      = (array) $_WC_Order_Item;
                    $WC_Product = $WC_Order->get_product_from_item($_item);
                    return $WC_Product instanceof \WC_Product && $WC_Product->exists() ? $WC_Product : null;
                }
            }
        } // unset($_item_id, $_WC_Order_Item, $_item); // Housekeeping.

        return null; // Failure.
    }
public function productByOrderItemId($item_id, \WC_Abstract_Order $WC_Order = null)
    {
        // WC 3.0+ uses `\WC_Order_Item_Product` extending `\WC_Order_Item`.
        // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html>
        // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item.html>

        if (!($item_id = (int) $item_id)) {
            return null; // Not possible.
        } elseif (!($WC_Order = $WC_Order ?: $this->orderByItemId($item_id))) {
            return null; // Not possible.
        }
        foreach ($WC_Order->get_items() as $_item_id => $_WC_Order_Item) {
            if ($_item_id === $item_id) {
                if ($_WC_Order_Item instanceof \WC_Order_Item_Product) {
                    $WC_Product = $_WC_Order_Item->get_product();
                    return $WC_Product instanceof \WC_Product && $WC_Product->exists() ? $WC_Product : null;
                    //
                } elseif ($_WC_Order_Item instanceof \WC_Order_Item) {
                    return null; // Not a product item.
                    //
                } else { // Back compat.
                    $_item      = (array) $_WC_Order_Item;
                    $WC_Product = $WC_Order->get_product_from_item($_item);
                    return $WC_Product instanceof \WC_Product && $WC_Product->exists() ? $WC_Product : null;
                }
            }
        } // unset($_item_id, $_WC_Order_Item, $_item); // Housekeeping.

        return null; // Failure.
    }
public function productByOrderItemId ( $ item_id , \ WC_Abstract_Order $ WC_Order = null ) { // WC 3.0+ uses `\WC_Order_Item_Product` extending `\WC_Order_Item`. // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html> // <https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item.html> if ( ! ( $ item_id = ( int ) $ item_id ) ) { return null ; // Not possible. } elseif ( ! ( $ WC_Order = $ WC_Order ? : $ this -> orderByItemId ( $ item_id ) ) ) { return null ; // Not possible. } foreach ( $ WC_Order -> get_items ( ) as $ _item_id => $ _WC_Order_Item ) { if ( $ _item_id === $ item_id ) { if ( $ _WC_Order_Item instanceof \ WC_Order_Item_Product ) { $ WC_Product = $ _WC_Order_Item -> get_product ( ) ; return $ WC_Product instanceof \ WC_Product && $ WC_Product -> exists ( ) ? $ WC_Product : null ; // } elseif ( $ _WC_Order_Item instanceof \ WC_Order_Item ) { return null ; // Not a product item. // } else { // Back compat. $ _item = ( array ) $ _WC_Order_Item ; $ WC_Product = $ WC_Order -> get_product_from_item ( $ _item ) ; return $ WC_Product instanceof \ WC_Product && $ WC_Product -> exists ( ) ? $ WC_Product : null ; } } } // unset($_item_id, $_WC_Order_Item, $_item); // Housekeeping. return null ; // Failure. }
Get product by order item ID.

@since 160608 Order item utilities.

@param string|int              $item_id  Order item ID.
@param \WC_Abstract_Order|null $WC_Order The order if already known.

@return \WC_Product|null A product object instance, else `null`.
Get product by order item ID .