update WooCommerce product attributes on save_post_product not working
Question
I want to develop a function, that update the attribute sizes-eu
of my WooCommerce products. This should happen every time I save/update the product.
So basically I use the save_post_product
action with a custom function as followed:
add_action('save_post_product', 'update_product_attribute_sizes' );
function update_product_attribute_sizes ( $post_id ) {
$sizes = array( '41', '42', '43', '44' );
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
wp_set_object_terms( $post_id, $sizes, 'pa_size-eu' );
}
An alternative solution I tried was this:
add_action('save_post_product', 'update_product_attribute_sizes' );
function update_product_attribute_sizes ( $post_id ) {
$sizes = array(
'name' => 'pa_size-eu',
'value' => '41 | 42 | 43 | 44',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
);
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
update_post_meta($post_id, '_product_attributes', $sizes);
}
These are my attributes:
and here is the taxonomy:
Unfortunately my attributes are not updated when saving the product and I don’t know why. Can anyone here give me some advice what the error might be?
0
2 months
0 Answers
11 views
0
Leave an answer
You must login or register to add a new answer .