• Resolved daniel982474

    (@daniel982474)


    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.
    • This topic was modified 7 years, 4 months ago by daniel982474.
    • This topic was modified 7 years, 4 months ago by daniel982474.
    • This topic was modified 7 years, 4 months ago by daniel982474.
    • This topic was modified 7 years, 4 months ago by daniel982474.
    • This topic was modified 7 years, 4 months ago by daniel982474.
    • This topic was modified 7 years, 4 months ago by daniel982474.
Viewing 2 replies - 1 through 2 (of 2 total)
  • @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.
    Thread Starter daniel982474

    (@daniel982474)

    Hello @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.