Reputation: 21
I have a function that it is supposed to create a variation and fill the variation with the values, the attributes of the variable product. To become more flexible i didn't create values for the products attributes besides
I have tried a hundred ways the make the values stick to the variation but they dont. The variation is created in the backend but it has none of the values atributed?????? PLEEEEASE HELP!!!! Im at this for 4 days now. ;(
The only values attributed to the product's atributes is ">>EMPTY<<" value. ///////////// Variables $data -> Array (Key -> Value), so that the keys == to the products atributes $product -> $product_id
function add_variable_product_to_cart($product_id, $data) {
$my_post = array(
'post_title'=> 'Order_Printaway',
'post_name' => 'product_' . $data["itemIdentifier"],
'post_status' => 'publish',
'post_parent' => $product_id ,//post is a child post of product post
'post_type' => 'product_variation',//set post type to product_variation
'guid'=>home_url() . '/?product_variation=product_' . $data["itemIdentifier"]
);
//Insert ea. post/variation into database:
$attID = wp_insert_post( $my_post );
update_post_meta($attID, '_price', $data["final_price"]);
update_post_meta($attID, '_regular_price', $data["final_price"]);
foreach($data as $key => $value){
$avail_attributes = array($value);
wp_set_object_terms($product_id, $avail_attributes, $key);
$thedata[$key]['name'] = $value;
}
update_post_meta( $attID,'_product_attributes',$thedata);
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
else{
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
}
///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// OUTPUT //////////////////////////////////////////////////////////////////////
//Atributes of the Product ->
Array ( [a_color] => Array ( [name] => a_color [value] => >>EMPTY<< [position] => 0 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_creasing] => Array ( [name] => a_creasing [value] => >>EMPTY<< [position] => 1 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing] => Array ( [name] => a_finishing [value] => >>EMPTY<< [position] => 2 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing_desc] => Array ( [name] => a_finishing_desc [value] => >>EMPTY<< [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_format_dir] => Array ( [name] => a_format_dir [value] => >>EMPTY<< [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_page] => Array ( [name] => a_page [value] => >>EMPTY<< [position] => 5 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper] => Array ( [name] => a_paper [value] => >>EMPTY<< [position] => 6 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper_desc] => Array ( [name] => a_paper_desc [value] => >>EMPTY<< [position] => 7 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_perforation] => Array ( [name] => a_perforation [value] => >>EMPTY<< [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_processing] => Array ( [name] => a_processing [value] => >>EMPTY<< [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [product] => Array ( [name] => product [value] => >>EMPTY<< [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [shipping] => Array ( [name] => shipping [value] => >>EMPTY<< [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) )
////Array of data that i am trying to introduce in the Variation
Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) Atributes of the variation after get_post_meta(757, '_product_attributes', true)) Array ( [0] => Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) )
Upvotes: 0
Views: 532
Reputation: 59
I struggled with same issue and the key was in attributes' taxonomies.
If attribute marked as taxonomy then it requires slug instead of string for text based attrs. So firstly check
if ( $attribute['is_taxonomy'] ) {
$options = wc_get_product_terms( $post_id, $attribute['name'], array( 'fields' => 'slugs' ) );
} else {
$options = explode( WC_DELIMITER, $attribute['value'] );
}
and fill $attr_val with slug or string value.
update_post_meta( $post_id, 'attribute_' . sanitize_title($attribute['name'] ), $attr_val);
WC_AJAX::save_attributes() may be helpful
Upvotes: -1