From 8de75dffd29a10dd5764e85646b948f2d1f945bc Mon Sep 17 00:00:00 2001 From: Stephan Hoogland Date: Fri, 3 May 2019 11:27:50 +0200 Subject: [PATCH 1/2] set more product values and trigger an action on update --- includes/class-xcore-products.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/includes/class-xcore-products.php b/includes/class-xcore-products.php index 9324160..2ea7a4e 100644 --- a/includes/class-xcore-products.php +++ b/includes/class-xcore-products.php @@ -132,9 +132,19 @@ public function update_item($request) $product = new WC_Product_Variation($request['id']); $product->set_stock_quantity($request['stock_quantity']); $product->set_manage_stock($request['manage_stock']); + $product->set_regular_price($request['regular_price']); + $product->set_tax_class($request['tax_class']); + $product->set_weight($request['weight']); + $product->set_catalog_visibility($request['catalog_visibility']); + $product->set_backorders($request['backorders']); $product->set_stock_status(); $product->save(); + // trigger an update (for example to let polylang sync products between languages) + if(has_action('woocommerce_update_product')){ + do_action( 'woocommerce_update_product', $product->get_id() ); + } + return parent::prepare_object_for_response($product, $request); } @@ -155,9 +165,9 @@ public function find_item_by_sku($request) $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id - FROM $wpdb->postmeta - WHERE meta_key='%s' - AND meta_value='%s'", + FROM $wpdb->postmeta + WHERE meta_key='%s' + AND meta_value='%s'", $meta_key, $product_reference ) @@ -250,4 +260,4 @@ public function get_product_category($request, $id = null) return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } } -} \ No newline at end of file +} From 14da13f5c65624ab9fab30ab5376204ba2a7f25f Mon Sep 17 00:00:00 2001 From: Stephan Hoogland Date: Fri, 3 May 2019 11:32:48 +0200 Subject: [PATCH 2/2] check if the values exist before trying to update them --- includes/class-xcore-products.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/includes/class-xcore-products.php b/includes/class-xcore-products.php index 2ea7a4e..05914da 100644 --- a/includes/class-xcore-products.php +++ b/includes/class-xcore-products.php @@ -130,13 +130,27 @@ public function update_item($request) $object = $this->get_object((int)$request['id']); if ($object->is_type('variation')) { $product = new WC_Product_Variation($request['id']); - $product->set_stock_quantity($request['stock_quantity']); - $product->set_manage_stock($request['manage_stock']); - $product->set_regular_price($request['regular_price']); - $product->set_tax_class($request['tax_class']); - $product->set_weight($request['weight']); - $product->set_catalog_visibility($request['catalog_visibility']); - $product->set_backorders($request['backorders']); + if(isset($request['stock_quantity'])){ + $product->set_stock_quantity($request['stock_quantity']); + } + if(isset($request['manage_stock'])){ + $product->set_manage_stock($request['manage_stock']); + } + if(isset($request['regular_price'])){ + $product->set_regular_price($request['regular_price']); + } + if(isset($request['tax_class'])){ + $product->set_tax_class($request['tax_class']); + } + if(isset($request['weight'])){ + $product->set_weight($request['weight']); + } + if(isset($request['catalog_visibility'])){ + $product->set_catalog_visibility($request['catalog_visibility']); + } + if(isset($request['backorders'])){ + $product->set_backorders($request['backorders']); + } $product->set_stock_status(); $product->save();