WC_Product_Variable::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);
            }
            $this->data_store->sync_managed_variation_stock_status($this);
            return $this->get_id();
        }
    }

Usage Example

示例#1
9
 function test_varation_save_attributes()
 {
     // Create a variable product with a color attribute.
     $product = new WC_Product_Variable();
     $attribute = new WC_Product_Attribute();
     $attribute->set_id(0);
     $attribute->set_name('color');
     $attribute->set_options(explode(WC_DELIMITER, 'green | red'));
     $attribute->set_visible(true);
     $attribute->set_variation(true);
     $product->set_attributes(array($attribute));
     $product->save();
     // Create a new variation with the color 'green'.
     $variation = new WC_Product_Variation();
     $variation->set_parent_id($product->get_id());
     $variation->set_attributes(array('color' => 'green'));
     $variation->set_status('private');
     $variation->save();
     // Now update some value unrelated to attributes.
     $variation = wc_get_product($variation->get_id());
     $variation->set_status('publish');
     $variation->save();
     // Load up the updated variation and verify that the saved state is correct.
     $loaded_variation = wc_get_product($variation->get_id());
     $this->assertEquals('publish', $loaded_variation->get_status('edit'));
     $_attribute = $loaded_variation->get_attributes('edit');
     $this->assertEquals('green', $_attribute['color']);
 }