本文整理汇总了PHP中WC_Product_Variation::set_attributes方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::set_attributes方法的具体用法?PHP WC_Product_Variation::set_attributes怎么用?PHP WC_Product_Variation::set_attributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::set_attributes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}