adding attributes to variable product
-
hey everyone,
ive been strugling with this for some time now and i can realy use some help.
i am tring to add 3 variation for each product (700 products).
so i try to do this in steps, first create a attribute for a product, and i just cant get this to work.
this is my code (in the functions.php):add_action( 'init', 'fixProducts'); function fixProducts() { $productTestId = '1270'; $productTestGroup = 'test'; $ps_product_attribute = array( array( 'id_product_attribute' => '1', 'id_product' => $productTestId, 'reference' => $productTestId, 'id_attribute_group' => '1', 'group_name' => $productTestGroup, 'attribute_name' => 'גודל סטנדרטי (כמו בתמונה)', 'id_attribute' => '7', 'unit_price_impact' => '0.00', 'price' => '0.000000' ),array( 'id_product_attribute' => '2', 'id_product' => $productTestId, 'reference' => $productTestId, 'id_attribute_group' => '1', 'group_name' => $productTestGroup, 'attribute_name' => 'גדול יותר', 'id_attribute' => '8', 'unit_price_impact' => '0.00', 'price' => '10.000000' ), array( 'id_product_attribute' => '3', 'id_product' => $productTestId, 'reference' => $productTestId, 'id_attribute_group' => '1', 'group_name' => $productTestGroup, 'attribute_name' => 'הכי גדול', 'id_attribute' => '9', 'unit_price_impact' => '0.00', 'price' => '20.000000' ) ); // Group attributes by shared id $ps_product_attributes = array(); foreach($ps_product_attribute as $attribute) { $attributeId = $attribute['id_product']; if(!array_key_exists($attributeId, $ps_product_attributes)) { $ps_product_attributes[$attributeId] = array($attribute); } else if (array_key_exists($attributeId, $ps_product_attributes) && !array_key_exists($attribute['id_product_attribute'], $ps_product_attributes[$attributeId])) { array_push($ps_product_attributes[$attributeId], $attribute); } } // Get products $query = new WC_Product_Query( array( // 'type' => 'variable', 'orderby' => 'modified', 'order' => 'DESC', )); $query->set( 'include', array($productTestId)); $products = $query->get_products(); foreach ($products as $key => $product) { // Attribute group for product id $ps_product_attribute = $ps_product_attributes[$product->get_id()]; // Filter out only option names $attributeOptions = array_map(function($attribute) { return $attribute['attribute_name']; }, $ps_product_attribute); // create attribute with names // create a terms in wp_terms - (term_id, name, slug, term_group) // save term_id // create a taxamony in wp_term_taxonomy - (term_taxonomy_id, term_id, taxonomy (pa_), description) // save term_taxonomy_id // link taxonomy and product in wp_term_relationships - (object_id - product, term_taxonomy_id) // add attribute to product // create variations from attribute options // save // The variation data $variation_data = array( 'attributes' => array( $ps_product_attribute[0]['group_name'] => "testttt" ,//$attributeOptions, ), 'sku' => '', 'regular_price' => '22.00', 'sale_price' => '', 'stock_qty' => 10, ); // The function to be run create_product_variation( $product->get_id(), $variation_data ); // save_product_attribute_from_name($ps_product_attribute[0]['group_name'], 'small'); // $term_taxonomy_ids = wp_set_object_terms( $product->get_id(), '0.25m', 'diameter', true ); $thedata = Array( 'test' => Array( 'name'=>'testttt', 'value'=>'testttt', 'is_visible' => '1', 'is_taxonomy' => '1' ) ); update_post_meta( $product->get_id(),'_product_attributes',$thedata); } }/** * Create a product variation for a defined variable product ID. * * @since 3.0.0 * @param int $product_id | Post ID of the product parent variable product. * @param array $variation_data | The data to insert in the product. */ function create_product_variation( $product_id, $variation_data ){ // Get the Variable product object (parent) $product = wc_get_product($product_id); $variation_post = array( 'post_title' => $product->get_title(), 'post_name' => 'product-'.$product_id.'-variation', 'post_status' => 'publish', 'post_parent' => $product_id, 'post_type' => 'variation', 'guid' => $product->get_permalink() ); // Creating the product variation $variation_id = wp_insert_post( $variation_post ); // Get an instance of the WC_Product_Variation object $variation = new WC_Product_Variation( $variation_id ); // Iterating through the variations attributes foreach ($variation_data['attributes'] as $attribute => $term_name ) { $taxonomy = 'pa_'.$attribute; // The attribute taxonomy // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil) if( ! taxonomy_exists( $taxonomy ) ) { register_taxonomy( $taxonomy, 'product_variation', array( 'hierarchical' => false, 'label' => ucfirst( $attribute ), 'query_var' => true, 'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug ) ); } // foreach( $terms_name as $term_name ){ // Check if the Term name exist and if not we create it. if( ! term_exists( $term_name, $taxonomy ) ) { wp_insert_term( $term_name, $taxonomy ); // Create the term } else $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug // Get the post Terms names from the parent variable product. $post_term_names = wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') ); // } // Check if the post term exist and if not we set it in the parent variable product. if( !in_array( $term_name, $post_term_names ) ) wp_set_post_terms( $product_id, $term_name, $taxonomy, true ); else debug([$term_name, 'in heystack', $post_term_names]); // Set/save the attribute data in the product variation update_post_meta( $variation_id, $taxonomy, $term_slug ); } ## Set/save all other data // SKU if( ! empty( $variation_data['sku'] ) ) $variation->set_sku( $variation_data['sku'] ); // Prices if( empty( $variation_data['sale_price'] ) ){ $variation->set_price( $variation_data['regular_price'] ); } else { $variation->set_price( $variation_data['sale_price'] ); $variation->set_sale_price( $variation_data['sale_price'] ); } $variation->set_regular_price( $variation_data['regular_price'] ); // Stock if( ! empty($variation_data['stock_qty']) ){ $variation->set_stock_quantity( $variation_data['stock_qty'] ); $variation->set_manage_stock(true); $variation->set_stock_status(''); $variation->set_manage_stock(false); } $variation->set_weight(''); // weight (reseting) $variation->save(); // Save the data }function debug($variable_to_test) { ?><pre dir="ltr" style="margin-top: 100px;"><?php var_dump( $variable_to_test ); ?></pre><?php }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘adding attributes to variable product’ is closed to new replies.