Last active
November 21, 2018 11:13
-
-
Save eto4detak/42869645335bc568923d35564e749b27 to your computer and use it in GitHub Desktop.
woo update variation 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', 'update_attributes_variation_product'); | |
function add_variation_product($post_id){ | |
$cars = array('c1', 'c2', 'c3' ); | |
$info = array('ww' ); | |
$att_all = []; | |
$att_all['car'] = $cars; | |
$att_all['info'] = $info; | |
$arr_attr_to_paren_product = []; | |
foreach ($att_all as $key => $value) { | |
$arr_attr_to_paren_product[$key] = $this->to_string_attribute($value); | |
} | |
$my_product_attributes = $arr_attr_to_paren_product; | |
wcproduct_set_attributes($post_id, $my_product_attributes); | |
foreach ($cars as $key_car => $car) { | |
foreach ($info as $key_inf => $inf) { | |
$_pv = new WC_Product_Variation(); | |
$_pv->set_parent_id($post_id); | |
$_pv->set_manage_stock('true'); | |
$_pv->set_stock_status('instock'); | |
$_pv->set_status('publish'); | |
$_pv->set_regular_price( 50 ); | |
$_pv->set_sale_price( 50 ); | |
$_pv->set_menu_order(0); | |
// $_pv->set_sku($_sku); | |
$_pv->set_stock_quantity(11); | |
$ar_attr = []; | |
$ar_attr['car'] = $car; | |
$ar_attr['info'] = $inf; | |
$_pv->set_attributes($ar_attr); | |
update_post_meta( $_pv->get_id(), 'attribute_'.'car', $car); | |
update_post_meta( $_pv->get_id(), 'attribute_'.'info', $inf); | |
$_pv->save(); | |
} | |
} | |
$data = []; | |
$data['attribute_names'] = []; | |
$data['attribute_values'] = []; | |
$data['attribute_position'] = []; | |
$data['attribute_visibility'] = []; | |
$data['attribute_variation'] = []; | |
foreach ($att_all as $key => $value) { | |
$data['attribute_names'][] = $key; | |
$data['attribute_position'][] = '0'; | |
$data['attribute_values'][] = $this->to_string_attribute($value); | |
$data['attribute_visibility'][] = '1'; | |
$data['attribute_variation'][] = '1'; | |
} | |
$attributes = WC_Meta_Box_Product_Data::prepare_attributes( $data ); | |
$product_id = absint( $post_id ); | |
$classname = WC_Product_Factory::get_product_classname( $product_id, 'variable' ); | |
$product = new $classname( $product_id ); | |
$product->set_attributes( $attributes ); | |
$product->save(); | |
} | |
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); | |
} | |
function to_string_attribute($attr_val) | |
{ | |
$str_arr = ''; | |
foreach ($attr_val as $key => $val) { | |
if ($key == 0) { | |
$str_arr = $val; | |
} else { | |
$str_arr .= ' | ' . $val; | |
} | |
} | |
return $str_arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment