Дополнение по коду
/**
* Adds product attribute value and returns attribute value ID
*
* @param type $label_id
* @param type $value
* @return type
*/
function ic_add_product_attribute_value( $label_id, $value ) {
$term = term_exists( $value, 'al_product-attributes', $label_id );
if ( empty( $term ) ) {
$term = wp_insert_term( $value, 'al_product-attributes', array( 'parent' => $label_id ) );
}
return $term[ 'term_id' ];
}
add_filter( 'product_meta_save', 'ic_assign_product_attributes', 2, 2 );
/**
* Adds product attributes to the database
*
* @param type $product_meta
* @param type $post
* @return type
*/
function ic_assign_product_attributes( $product_meta, $post, $clear_empty = true ) {
$max_attr = product_attributes_number();
if ( $max_attr > 0 ) {
$product_id = isset( $post->ID ) ? $post->ID : $post;
$attr_ids = array();
for ( $i = 1; $i <= $max_attr; $i++ ) {
if ( empty( $product_meta[ '_attribute' . $i ] ) || (isset( $product_meta[ '_attribute' . $i ][ 0 ] ) && empty( $product_meta[ '_attribute' . $i ][ 0 ] )) ) {
continue;
}
if ( !empty( $product_meta[ '_attribute-label' . $i ] ) ) {
//$label = is_array( $product_meta[ '_attribute-label' . $i ] ) ? ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ][ 0 ] ) : ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ] );
$label = ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ] );
if ( !empty( $label ) ) {
//$value = is_array( $product_meta[ '_attribute' . $i ] ) ? ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ][ 0 ] ) : ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ] );
$value = ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ] );
if ( !empty( $value ) ) {
$label_id = ic_add_product_attribute_label( $label );
if ( !empty( $label_id ) ) {
$attr_ids[] = $label_id;
if ( !is_array( $value ) ) {
$value = array( $value );
}
foreach ( $value as $val ) {
$value_id = ic_add_product_attribute_value( $label_id, $val );
$attr_ids[] = $value_id;
}
}
}
}
}
}
if ( !empty( $attr_ids ) ) {
$attr_ids = array_unique( array_map( 'intval', $attr_ids ) );
wp_set_object_terms( $product_id, $attr_ids, 'al_product-attributes' );
if ( $clear_empty ) {
ic_clear_empty_attributes();
}
}
}
return $product_meta;
}