Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active October 17, 2018 16:04
Show Gist options
  • Save eto4detak/a034337d534aa4bb60f87b8cbec73fdd to your computer and use it in GitHub Desktop.
Save eto4detak/a034337d534aa4bb60f87b8cbec73fdd to your computer and use it in GitHub Desktop.
woocommerce add custom attribute
<?php
add_action('init', 'generate_taxonomy_product');
function generate_taxonomy_product($value=''){
$my_product_attributes = array('hdd1' => 'qqqqqq', 'ram2' => 'wwwwwww');
wcproduct_set_attributes(10807, $my_product_attributes);
}
function wcproduct_set_attributes($post_id, $attributes) {
global $superopt;
$product = wc_get_product($post_id);
$old_attributes = $product->get_attributes();
$product_attributes = [];
foreach ( $old_attributes as $name => $value ) {
$product_attributes[] = array (
'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
'value' => $value, // set attribute value
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
}
foreach ($attributes as $name => $value) {
$product_attributes[] = array (
'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
'value' => $value, // set attribute value
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
}
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment