Last active
October 17, 2018 16:04
-
-
Save eto4detak/a034337d534aa4bb60f87b8cbec73fdd to your computer and use it in GitHub Desktop.
woocommerce add custom attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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