本文整理汇总了PHP中WC_Product_Variation::save方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::save方法的具体用法?PHP WC_Product_Variation::save怎么用?PHP WC_Product_Variation::save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::save方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1:
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']);
}
示例2: link_all_variations
/**
* Link all variations via ajax function.
*/
public static function link_all_variations()
{
check_ajax_referer('link-variations', 'security');
if (!current_user_can('edit_products')) {
die(-1);
}
wc_maybe_define_constant('WC_MAX_LINKED_VARIATIONS', 49);
wc_set_time_limit(0);
$post_id = intval($_POST['post_id']);
if (!$post_id) {
die;
}
$variations = array();
$product = wc_get_product($post_id);
if ($product->is_type('variable')) {
$attributes = wc_list_pluck(array_filter($product->get_attributes(), 'wc_attributes_array_filter_variation'), 'get_slugs');
if (!empty($attributes)) {
// Get existing variations so we don't create duplicates.
$existing_variations = array_map('wc_get_product', $product->get_children());
$existing_attributes = array();
foreach ($existing_variations as $existing_variation) {
$existing_attributes[] = $existing_variation->get_attributes();
}
$added = 0;
$possible_attributes = wc_array_cartesian($attributes);
foreach ($possible_attributes as $possible_attribute) {
if (in_array($possible_attribute, $existing_attributes)) {
continue;
}
$variation = new WC_Product_Variation();
$variation->set_parent_id($post_id);
$variation->set_attributes($possible_attribute);
do_action('product_variation_linked', $variation->save());
if ($added++ > WC_MAX_LINKED_VARIATIONS) {
break;
}
}
echo $added;
}
}
die;
}
示例3: save_variations
/**
* Save meta box data.
*
* @param int $post_id
* @param WP_Post $post
*/
public static function save_variations($post_id, $post)
{
if (isset($_POST['variable_post_id'])) {
$parent = wc_get_product($post_id);
$max_loop = max(array_keys($_POST['variable_post_id']));
$data_store = $parent->get_data_store();
$data_store->sort_all_product_variations($parent->get_id());
for ($i = 0; $i <= $max_loop; $i++) {
if (!isset($_POST['variable_post_id'][$i])) {
continue;
}
$variation_id = absint($_POST['variable_post_id'][$i]);
$variation = new WC_Product_Variation($variation_id);
$errors = $variation->set_props(array('status' => isset($_POST['variable_enabled'][$i]) ? 'publish' : 'private', 'menu_order' => wc_clean($_POST['variation_menu_order'][$i]), 'regular_price' => wc_clean($_POST['variable_regular_price'][$i]), 'sale_price' => wc_clean($_POST['variable_sale_price'][$i]), 'virtual' => isset($_POST['variable_is_virtual'][$i]), 'downloadable' => isset($_POST['variable_is_downloadable'][$i]), 'date_on_sale_from' => wc_clean($_POST['variable_sale_price_dates_from'][$i]), 'date_on_sale_to' => wc_clean($_POST['variable_sale_price_dates_to'][$i]), 'description' => wp_kses_post(wc_sanitize_textarea($_POST['variable_description'][$i])), 'download_limit' => wc_clean($_POST['variable_download_limit'][$i]), 'download_expiry' => wc_clean($_POST['variable_download_expiry'][$i]), 'downloads' => self::prepare_downloads(isset($_POST['_wc_variation_file_names'][$variation_id]) ? $_POST['_wc_variation_file_names'][$variation_id] : array(), isset($_POST['_wc_variation_file_urls'][$variation_id]) ? $_POST['_wc_variation_file_urls'][$variation_id] : array(), isset($_POST['_wc_variation_file_hashes'][$variation_id]) ? $_POST['_wc_variation_file_hashes'][$variation_id] : array()), 'manage_stock' => isset($_POST['variable_manage_stock'][$i]), 'stock_quantity' => wc_clean($_POST['variable_stock'][$i]), 'backorders' => wc_clean($_POST['variable_backorders'][$i]), 'stock_status' => wc_clean($_POST['variable_stock_status'][$i]), 'image_id' => wc_clean($_POST['upload_image_id'][$i]), 'attributes' => self::prepare_set_attributes($parent->get_attributes(), 'attribute_', $i), 'sku' => isset($_POST['variable_sku'][$i]) ? wc_clean($_POST['variable_sku'][$i]) : '', 'weight' => isset($_POST['variable_weight'][$i]) ? wc_clean($_POST['variable_weight'][$i]) : '', 'length' => isset($_POST['variable_length'][$i]) ? wc_clean($_POST['variable_length'][$i]) : '', 'width' => isset($_POST['variable_width'][$i]) ? wc_clean($_POST['variable_width'][$i]) : '', 'height' => isset($_POST['variable_height'][$i]) ? wc_clean($_POST['variable_height'][$i]) : '', 'shipping_class_id' => wc_clean($_POST['variable_shipping_class'][$i]), 'tax_class' => wc_clean($_POST['variable_tax_class'][$i])));
if (is_wp_error($errors)) {
WC_Admin_Meta_Boxes::add_error($errors->get_error_message());
}
$variation->save();
do_action('woocommerce_save_product_variation', $variation_id, $i);
}
}
}