product->save() not saving to the database?
-
Hello, I have used the search function but haven’t found any relevant posts. I have found a lot of sample code that is very much like what I’m doing — but for some reason it’s not working for me.
I have a woocommerce-based site, and I have written a simple PHP page (hosted on that same site) that dumps out a CSV file of all products and variations and their prices in the variety of currencies we support.
The CSV file will be edited to reflect prices for the next year.
I am now working on the parser of that file, basically a perl program that will dump out a PHP file. The file I’ll upload to our site, execute it in the browser, and I’m expecting that all products/variations will get their prices updated.
My simple test program isn’t working though. The program:
<?php require_once(dirname(__FILE__) . '/../wp-config.php'); $wp->init(); $wp->parse_request(); $wp->query_posts(); $wp->register_globals(); $wp->send_headers(); echo "<h2>staging: setting prices via woo php</h2>"; $pid = 1495; $product = wc_get_product( $pid ); echo "<p>test changing price of <b>" . $product->get_name() . "</b></p>"; echo "<p>product id: " . $product->get_id() . "</p>"; echo "<p>product type: " . $product->get_type() . "</p>"; echo "<p>reg price " . $product->get_regular_price() . "</p>"; echo "<p>sale price " . $product->get_sale_price() . "</p>"; echo "<p>price " . $p . "</p>"; $newprice = (float)$product->get_price() + 98.0; $product->set_price( $newprice ); echo "<p>updated var price to " . $product->get_price() . "</p>"; $n = $product->save(); echo "<p>saved product price, status should be product id: $n</p>"; /* read from db and confirm */ $read_product = wc_get_product( $pid ); echo "<p>post price update in db: " . $read_product->get_price() . "</p>"; ?>
The return code from $product->save() is the correct product id. It’s a simple product, so variations don’t enter the game yet. But re-reading the product from the database, in the same session, shows the old price.
product id: 1495 product type: simple reg price 780 sale price price 0 updated var price to 878 saved product price, status should be product id: 1495 post price update in db: 780
What am I missing? I’m guessing something simple, but I’ve been at it for a few hours… Thank you much,
Tom
- The topic ‘product->save() not saving to the database?’ is closed to new replies.