WC_Product::save PHP Method

save() public method

Save data (either create or update depending on if we are working on an existing product).
Since: 2.7.0
public save ( )
    public function save()
    {
        $this->validate_props();
        if ($this->data_store) {
            if ($this->get_id()) {
                $this->data_store->update($this);
            } else {
                $this->data_store->create($this);
            }
            if ($this->get_parent_id()) {
                wp_schedule_single_event(time(), 'woocommerce_deferred_product_sync', array('product_id' => $this->get_parent_id()));
            }
            return $this->get_id();
        }
    }

Usage Example

Beispiel #1
1
 /**
  * Test product setters and getters
  * @since 2.7.0
  */
 public function test_product_getters_and_setters()
 {
     global $wpdb;
     $attributes = array();
     $attribute = new WC_Product_Attribute();
     $attribute->set_id(0);
     $attribute->set_name('Test Attribute');
     $attribute->set_options(array('Fish', 'Fingers'));
     $attribute->set_position(0);
     $attribute->set_visible(true);
     $attribute->set_variation(false);
     $attributes['test-attribute'] = $attribute;
     $getters_and_setters = array('name' => 'Test', 'slug' => 'test', 'status' => 'publish', 'catalog_visibility' => 'search', 'featured' => false, 'description' => 'Hello world', 'short_description' => 'hello', 'sku' => 'TEST SKU', 'regular_price' => 15.0, 'sale_price' => 10.0, 'date_on_sale_from' => '1475798400', 'date_on_sale_to' => '1477267200', 'total_sales' => 20, 'tax_status' => 'none', 'tax_class' => '', 'manage_stock' => true, 'stock_quantity' => 10, 'stock_status' => 'instock', 'backorders' => 'notify', 'sold_individually' => false, 'weight' => 100, 'length' => 10, 'width' => 10, 'height' => 10, 'upsell_ids' => array(2, 3), 'cross_sell_ids' => array(4, 5), 'parent_id' => 0, 'reviews_allowed' => true, 'default_attributes' => array(), 'purchase_note' => 'A note', 'menu_order' => 2, 'gallery_image_ids' => array(), 'download_expiry' => -1, 'download_limit' => 5, 'attributes' => $attributes);
     $product = new WC_Product();
     foreach ($getters_and_setters as $function => $value) {
         $product->{"set_{$function}"}($value);
     }
     $product->save();
     $product = new WC_Product_Simple($product->get_id());
     foreach ($getters_and_setters as $function => $value) {
         $this->assertEquals($value, $product->{"get_{$function}"}(), $function);
     }
     $image_url = media_sideload_image("https://cldup.com/Dr1Bczxq4q.png", $product->get_id(), '', 'src');
     $image_id = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid='%s';", $image_url));
     $product->set_image_id($image_id[0]);
     $product->save();
     $this->assertEquals($image_id[0], $product->get_image_id());
 }
All Usage Examples Of WC_Product::save
WC_Product