我正在构建一个获取产品的脚本,但我陷入了以编程方式添加属性的部分。所以基本上我想检查属性是否存在,如果不存在则添加它。然后检查它的价值是否存在,如果不存在,他们会添加它的价值,并将所有内容附加到我的产品上。
这是我获得产品的格式:
[attributes] => Array
(
[0] => stdClass Object
(
[id] => 332
[name] => Gender
[value] => Woman
)
[1] => stdClass Object
(
[id] => 333
[name] => Wheel Size
[value] => 28 Inch
)
[2] => stdClass Object
(
[id] => 334
[name] => Frame height
[value] => 56 cm
)
现在我已经尝试了两个版本,我将粘贴两个代码示例。
版本1:
foreach($product->attributes as $attribute) {
$slug = 'pa_' . strtolower($attribute->name);
$attribute_name = $attribute->name;
$attribute_value = $attribute->value;
$permalinks = get_option( 'woocommerce_permalinks' );
$taxonomy_data = array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'labels' => array(
'name' => $attribute_name
),
'show_ui' => false,
'query_var' => true,
'sort' => false,
'public' => true,
'show_in_nav_menus' => false,
'capabilities' => array(
'manage_terms' => 'manage_product_terms',
'edit_terms' => 'edit_product_terms',
'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms',
)
);
register_taxonomy( $slug, array('product'), $taxonomy_data );
// @end_Debug
}
该版本仅添加了以某种方式附加到产品的属性,
但它们在 WordPress 管理员的“属性”选项卡下不可见。
我稍后想要按这些属性进行过滤,所以我需要
与 Woocommerce 属性的连接。
版本2:
foreach($product->attributes as $attribute) {
$slug = 'pa_' . strtolower($attribute->name);
$attribute_name = $attribute->name;
$attribute_value = $attribute->value;
$table = $wpdb->prefix . 'woocommerce_attribute_taxonomies';
$attr = $wpdb->get_results( "SELECT * FROM {$table} WHERE attribute_name LIKE '%{$attribute_name}%'", OBJECT );
if(count($attr) > 0) {
//
}
else {
/** create taxonomy */
$data = array(
'attribute_label' => $attribute_name,
'attribute_name' => $slug,
'attribute_type' => 'select',
'attribute_orderby' => 'menu_order',
'attribute_public' => 0, // Enable archives ==> true (or 1)
);
$results = $wpdb->insert( "{$wpdb->prefix}woocommerce_attribute_taxonomies", $data );
if ( is_wp_error( $results ) ) {
return new WP_Error( 'cannot_create_attribute', $results->get_error_message(), array( 'status' => 400 ) );
}
$id = $wpdb->insert_id;
do_action('woocommerce_attribute_added', $id, $data);
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
/* end_Create */
}
// Clean attribute name to get the taxonomy
$taxonomy = 'pa_' . wc_sanitize_taxonomy_name( $attribute_name );
$option_term_ids = array(); // Initializing
// Loop through defined attribute data options (terms values)
if( term_exists( $attribute_value, $taxonomy ) ){
// Save the possible option value for the attribute which will be used for variation later
wp_set_object_terms( $product_id, $attribute_value, $taxonomy, true );
// Get the term ID
$option_term_ids[] = get_term_by( 'name', $attribute_value, $taxonomy )->term_id;
}
// Loop through defined attribute data
$attributes[$taxonomy] = array(
'name' => $taxonomy,
'value' => $option_term_ids, // Need to be term IDs
'position' => '1',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
);
// Save the meta entry for product attributes
update_post_meta( $product_id, '_product_attributes', $attributes );
}
这似乎只是将它们添加到“产品属性”选项卡下,也
不添加条款值。