Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wc_product_attribute set_options value

// Add global attributes.
$global_attributes = ['Size', 'Colour'];
foreach ($global_attributes as $global_attribute) {
  wc_create_attribute([
    'name' => $global_attribute,
    'type' => 'text',
  ]);
}
// Add product.
$product_attributes = [
  'Size' => 'Small',
  'Colour' => 'Red',
  'Rating' => '5 Star!'
];
$product = new WC_Product;
// Add names, prices, etc.
$attributes = [];
foreach ($product_attributes as $name => $value) {
  $attribute = new WC_Product_Attribute();
  $attribute->set_name($name);
  $attribute->set_options($value);
  if (in_array($key, $global_attributes)) {
    // Deal with global attributes.
    $term_name = "pa_" . sanitize_title( $name );
    if (! $term = get_term_by( 'name', $value, $term_name )) {
      wp_insert_term( $value, $term_name );
      $term = get_term_by( 'name', $value, $term_name );
    }
    $attribute->set_id($term->term_taxonomy_id);
    $attribute->set_name($term_name);
    $attribute->set_options([$term->term_id]);
  }
  $attributes[] = $attribute;
}
$product->set_attributes($attributes);
$product->save();
Comment

PREVIOUS NEXT
Code Example
Php :: wordpress Warning: Cannot modify header information - headers already sent by 
Php :: Reset Admin password in Magento 2 
Php :: php ternary shorthand 
Php :: laravel eloquent search json column 
Php :: wp query search 
Php :: use resource in laravel 8 
Php :: laravel retry specific failed job 
Php :: get next month first day php 
Php :: laravel hasmany relationship 
Php :: check if not empty blade engine 
Php :: check if array contains only unique values php 
Php :: woocommerce change add to cart message 
Php :: increase php memory 
Php :: limiting requests to controllers in laravel 
Php :: laravel: get last id 
Php :: laravel create mode 
Php :: display image in html using php 
Php :: php artisan serve on lumen 
Php :: Best debugging tools for php 
Php :: Call to a member function move() on string 
Php :: password change logout from wordpress 
Php :: eloquent unique combination 
Php :: user location using php 
Php :: setup cron on macos for laravel 
Php :: get nearby from longitude and latitude in laravel 
Php :: codeigniter installation with composer 
Php :: php new line 
Php :: laravel foreign 
Php :: load-styles.php 403 
Php :: is replace case sensitive php 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =