Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active January 15, 2023 14:18
Show Gist options
  • Save artikus11/b6b17e96346da6c7fe9858e382aaaf35 to your computer and use it in GitHub Desktop.
Save artikus11/b6b17e96346da6c7fe9858e382aaaf35 to your computer and use it in GitHub Desktop.
function art_adding_attributes( $post_id, $xml_node ) {
// Ограничиваем работу сниппета нужным импортом
$import_id = ( $_GET['id'] ?? ( isset( $_GET['import_id'] ) ? $_GET['import_id'] : 'new' ) );
$attributes_from_string = [];
if ( ( '31' === $import_id || '26' === $import_id ) && $xml_node->dimensions ) {
$sep = '/';
if ( false !== strpos( $xml_node->dimensions, 'x' ) ) {
$sep = 'x';
}
$dimensions = explode( $sep, $xml_node->dimensions );
$dimensions = convert_dimensions( $dimensions );
$a = [ 'dlina', 'shirina', 'vysota' ];
if ( count( $dimensions ) < 3 ) {
$a = [ 'vysota', 'shirina' ];
}
if ( count( $dimensions ) < 2 ) {
return;
}
$attributes_from_string = array_combine( $a, $dimensions );
awpai_set_attributes( $attributes_from_string, $post_id );
}
if ( '30' === $import_id ) {
$attributes = explode( ';', $xml_node->productoptiondescription1 );
$attributes = array_map( 'art_satinize_value', $attributes );
$keys = art_get_keys( $attributes, art_get_base_color() );
$attributes_from_string = [ 'colour' => array_unique( $keys ) ];
awpai_set_attributes( $attributes_from_string, $post_id );
}
if ( ( '41' === $import_id ) && 'Размер' === (string) $xml_node->param[2]['name'] ) {
$dimensions = $xml_node->param[2];
if ( ( false !== strpos( $dimensions, 'D' ) ) ) {
$dimensions = explode( ' ', trim( $dimensions ) );
$a = [ 'diametr' ];
if ( count( $dimensions ) === 2 ) {
unset( $dimensions[1] );
} elseif ( count( $dimensions ) === 3 ) {
unset( $dimensions[0], $dimensions[2] );
} elseif ( count( $dimensions ) === 4 ) {
$a = [ 'diametr', 'vysota' ];
unset( $dimensions[1], $dimensions[3] );
if ( mb_strpos( $xml_node->param[2], 'D' ) === 0 ) {
[ $dimensions[0], $dimensions[2] ] = [ $dimensions[2], $dimensions[0] ];
}
}
$dimensions = convert_dimensions( clear_array_element( $dimensions ) );
$attributes_from_string = array_combine( $a, $dimensions );
} elseif ( ( false === strpos( $dimensions, 'D' ) ) ) {
$dimensions = array_filter( explode( ' ', $dimensions ) );
if ( count( $dimensions ) > 4 ) {
unset( $dimensions[2], $dimensions[4], $dimensions[5], $dimensions[6] );
$a = [ 'diametr', 'vysota' ];
} else {
unset( $dimensions[1], $dimensions[3] );
$a = [ 'shirina', 'dlina' ];
}
$dimensions = convert_dimensions( $dimensions );
$attributes_from_string = array_combine( $a, $dimensions );
}
awpai_set_attributes( $attributes_from_string, $post_id );
}
}
/**
* @param $attributes_from_string
* @param $post_id
*/
function awpai_set_attributes( $attributes_from_string, $post_id ) {
if ( empty( $attributes_from_string ) ) {
return;
}
$attributes = [];
$product = wc_get_product( $post_id );
$attributes_current = $product->get_attributes();
foreach ( $attributes_from_string as $name => $value ) {
// Проверяем на наличие ключа и значения
if ( empty( $name ) || empty( $value ) ) {
continue;
}
// Превращаем занчения в массив
if ( ! is_array( $value ) ) {
$value = array_map( 'trim', explode( ',', $value ) );
}
$attribute_id = 0;
$slug = wc_sanitize_taxonomy_name( $name );
$taxonomy_name = wc_attribute_taxonomy_name( $name );
$attribute_taxonomies = wc_get_attribute_taxonomies();
// Создаем таксономию глобального атрибута, если ее еще нет
if ( ! taxonomy_exists( $taxonomy_name ) ) {
$attribute_name = wc_attribute_taxonomy_slug( $name );
// Create the taxonomy.
if ( ! in_array( $attribute_name, $attribute_taxonomies, true ) ) {
$attribute_id = wc_create_attribute(
[
'name' => $name,
'slug' => $attribute_name,
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => false,
]
);
}
register_taxonomy(
$taxonomy_name,
apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, [ 'product' ] ),
apply_filters(
'woocommerce_taxonomy_args_' . $taxonomy_name,
[
'hierarchical' => true,
'show_ui' => false,
'query_var' => true,
'rewrite' => false,
]
)
);
} else {
// Иначе, просто присваиваем нужное значение в нужную таксу
$taxonomies = wp_list_pluck( $attribute_taxonomies, 'attribute_id', 'attribute_name' );
if ( ! isset( $taxonomies[ $slug ] ) ) {
continue;
}
$attribute_id = (int) $taxonomies[ $slug ];
}
// Создаем атрибуты
$attribute = new WC_Product_Attribute();
$attribute->set_id( $attribute_id );
$attribute->set_name( $taxonomy_name );
$attribute->set_options( $value );
$attribute->set_visible( true );
$attribute->set_variation( false );
$attributes[ $taxonomy_name ] = $attribute;
}
$attributes = array_merge( $attributes_current, $attributes );
$product->set_attributes( $attributes );
$product->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment