From eae8c6b8e5618676de38403386fead3bb3d1252a Mon Sep 17 00:00:00 2001 From: Guillermo Gette Date: Thu, 12 Mar 2015 22:52:13 +1100 Subject: [PATCH 1/4] add function wc_get_product_by_sku --- includes/wc-product-functions.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 66c43519ccc..cf48cd9b8cd 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -21,6 +21,30 @@ function wc_get_product( $the_product = false, $args = array() ) { return WC()->product_factory->get_product( $the_product, $args ); } +/** + * Return a product object by SKU + * + * @param string $sku + * @return WC_Product + */ +function wc_get_product_by_sku( $sku = "" ) { + global $wpdb; + + $product_id = $wpdb->get_var( $wpdb->prepare( " + SELECT $wpdb->posts.ID + FROM $wpdb->posts + LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) + WHERE $wpdb->posts.post_type IN ( 'product', 'product_variation' ) + AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s' LIMIT 1 + ", $sku ) ); + + if ($product_id) { + return wc_get_product($product_id); + } else { + return false; + } +} + /** * Update a product's stock amount * From e9d20c9f6658494f583d575a2d2bb503f185641a Mon Sep 17 00:00:00 2001 From: Guillermo Gette Date: Thu, 12 Mar 2015 22:52:37 +1100 Subject: [PATCH 2/4] update set_line_item to let you set items by sku --- includes/api/class-wc-api-orders.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/api/class-wc-api-orders.php b/includes/api/class-wc-api-orders.php index 2b4a94ee137..995521e9d3b 100644 --- a/includes/api/class-wc-api-orders.php +++ b/includes/api/class-wc-api-orders.php @@ -846,8 +846,8 @@ class WC_API_Orders extends WC_API_Resource { $creating = ( 'create' === $action ); // product is always required - if ( ! isset( $item['product_id'] ) ) { - throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID is required', 'woocommerce' ), 400 ); + if ( ! isset( $item['product_id'] ) && ! isset( $item['sku'] ) ) { + throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID or SKU is required', 'woocommerce' ), 400 ); } // when updating, ensure product ID provided matches @@ -861,7 +861,11 @@ class WC_API_Orders extends WC_API_Resource { } } - $product = wc_get_product( $item['product_id'] ); + if ( isset( $item['product_id'] ) ) { + $product = wc_get_product( $item['product_id'] ); + } elseif ( isset( $item['sku'] ) ) { + $product = wc_get_product_by_sku( $item['sku'] ); + } // must be a valid WC_Product if ( ! is_object( $product ) ) { From 122cca701bf5e8e8b67b9a22f37a9991a79a81b2 Mon Sep 17 00:00:00 2001 From: Guillermo Gette Date: Wed, 18 Mar 2015 23:09:38 +1100 Subject: [PATCH 3/4] removed wc_get_product_by_sku --- includes/wc-product-functions.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index f1b93a35b9c..0a9711163d9 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -21,30 +21,6 @@ function wc_get_product( $the_product = false, $args = array() ) { return WC()->product_factory->get_product( $the_product, $args ); } -/** - * Return a product object by SKU - * - * @param string $sku - * @return WC_Product - */ -function wc_get_product_by_sku( $sku = "" ) { - global $wpdb; - - $product_id = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) - WHERE $wpdb->posts.post_type IN ( 'product', 'product_variation' ) - AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s' LIMIT 1 - ", $sku ) ); - - if ($product_id) { - return wc_get_product($product_id); - } else { - return false; - } -} - /** * Update a product's stock amount * From 49f84a7bc6495ab11f66a50961ac626e016290fb Mon Sep 17 00:00:00 2001 From: Guillermo Gette Date: Wed, 18 Mar 2015 23:11:28 +1100 Subject: [PATCH 4/4] get product_id using wc_get_product_id_by_sku --- includes/api/class-wc-api-orders.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/api/class-wc-api-orders.php b/includes/api/class-wc-api-orders.php index f3f25d8039a..fcff4e65f11 100644 --- a/includes/api/class-wc-api-orders.php +++ b/includes/api/class-wc-api-orders.php @@ -880,7 +880,8 @@ class WC_API_Orders extends WC_API_Resource { if ( isset( $item['product_id'] ) ) { $product = wc_get_product( $item['product_id'] ); } elseif ( isset( $item['sku'] ) ) { - $product = wc_get_product_by_sku( $item['sku'] ); + $product_id = wc_get_product_id_by_sku( $item['sku'] ); + $product = wc_get_product( $product_id ); } // must be a valid WC_Product