Title: WooCommerce Product save() function returns php notice
Last modified: January 14, 2019

---

# WooCommerce Product save() function returns php notice

 *  Resolved [daniel982474](https://wordpress.org/support/users/daniel982474/)
 * (@daniel982474)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/product-save-function-returns-error/)
 * I can’t figure this one out. I’ve written the following function which updates
   the stock quantity of a particular item:
 *     ```
       	public function update_stock( $sku, $quantity ) {
       		$id      = $this->get_product_id( $sku );
       		$product = wc_get_product( $id );
   
       		if ( $quantity < 0 ) {
       			$quantity = 0;
       		}
   
       		if ( $product != null ) {
       			$product->set_stock_quantity( $quantity );
       			if ( $quantity === 0 ) {
       				$product->set_stock_status( 'outofstock' );
       			} else {
       				$product->set_stock_status( 'instock' );
       			}
       			$product->save();
       		}
       	}
       ```
   
 * However, calling the product save() function at the end returns a strange error
   which seems unrelated:
 * > Notice: Trying to get property ‘name’ of non-object in /opt/lampp/htdocs/wordpress/
   wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-
   cpt.php on line 1488
 * Here’s what you can find in class-wc-product-data-store-cpt.php on line 1488:
 *     ```
           public function get_product_type( $product_id ) {
       		$post_type = get_post_type( $product_id );
       		if ( 'product_variation' === $post_type ) {
       			return 'variation';
       		} elseif ( 'product' === $post_type ) {
       			$terms = get_the_terms( $product_id, 'product_type' );
       			return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; //LINE 1488
       		} else {
       			return false;
       		}
       	}
       ```
   
 * Any help would be greatly appreciated!
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).
    -  This topic was modified 7 years, 4 months ago by [daniel982474](https://wordpress.org/support/users/daniel982474/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [wpriders](https://wordpress.org/support/users/wpriders/)
 * (@wpriders)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/product-save-function-returns-error/#post-11086576)
 * [@daniel982474](https://wordpress.org/support/users/daniel982474/)
 * Hello
 * From what i see you write: $id = $this->**get_product_id**( $sku );
    and you 
   try to get a product by SKU. I suggest you change the function to: **wc_get_product_id_by_sku**
    -  This reply was modified 7 years, 4 months ago by [wpriders](https://wordpress.org/support/users/wpriders/).
 *  Thread Starter [daniel982474](https://wordpress.org/support/users/daniel982474/)
 * (@daniel982474)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/product-save-function-returns-error/#post-11086615)
 * Hello [@wpriders](https://wordpress.org/support/users/wpriders/),
 * Thanks for your response. get_product_id() is a function that I wrote myself.
   Anyway, I’ve changed that to wc_get_product_id_by_sku() but I’m still getting
   the same php Notice!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WooCommerce Product save() function returns php notice’ is closed to 
new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [daniel982474](https://wordpress.org/support/users/daniel982474/)
 * Last activity: [7 years, 4 months ago](https://wordpress.org/support/topic/product-save-function-returns-error/#post-11086615)
 * Status: resolved