在wordpress中向产品添加多个属性值

I have comma separated string with attribute values. For example

  1. Attributes
    • Color- Red
    • Type - Shockproof, Fashion, third value,

Products are imported from thirdparty software. Also are attributes etc.

Everything is importing fine as there is only one attribute value like Color- Red. When there is more attribute values, then on product edit page in attribute section shows only last value. In this case third value.

My code is here:

foreach ($my_product_attributes as $key => $value) {
    $key = 'pa_' . $key;

    $commas = substr_count($value, ",");
    if($commas >= 1){
        $attribute_values = explode(",", $value);
        foreach($attribute_values as $attribute){
            wp_set_object_terms($p_id, $attribute, $key, false);
            $thedata[sanitize_title($key)] = Array(
                'name' => wc_clean($key),
                'value' => $attribute,
                'postion' => '0',
                'is_visible' => '1',
                'is_variation' => '0',
                'is_taxonomy' => '1'
            );
            update_post_meta($p_id, '_product_attributes', $thedata);
        }
    }

I know core problem of my code, but i dont know where to fix it, and how

You are probably overwriting the 'value' key in your array with the foreach as you are using the same key for the array.

You could try creating an array of the attributes and adding that to the data array.

foreach($attribute_values as $attribute){
    $attribute_array[] = $attribute;
}

$thedata[sanitize_title($key)] = Array(
    'name' => wc_clean($key),
    'value' => $attribute_array,
    'postion' => '0',
    'is_visible' => '1',
    'is_variation' => '0',
    'is_taxonomy' => '1'
);

You will need to change the page this is displayed on to iterate through the attributes though.

the problem with your code is that you are calling update_post_meta on each attribute value for the same key. These replace the attributes set earlier for the same keys. Try this (untested) code:

foreach ($my_product_attributes as $key => $value)
{
    $key = 'pa_' . $key;

    $commas = substr_count($value, ",");
    if($commas >= 1){
        $attribute_values = explode(",", $value);
        foreach($attribute_values as $attribute)
        {
            //set object term for whatever reason you need.
            wp_set_object_terms($p_id, $attribute, $key, false);
        }                
    }
    else
        $attribute_values = $value;

    $thedata[sanitize_title($key)] = Array(
            'name' => wc_clean($key),
            'value' => $attribute_values,
            'postion' => '0',
            'is_visible' => '1',
            'is_variation' => '0',
            'is_taxonomy' => '1'
        );
    update_post_meta($p_id, '_product_attributes', $thedata);
}

So, i overthinked here. Here's code that worked me

foreach ($my_product_attributes as $key => $value) {
    $key = 'pa_' . $key;
    $attribute_values = explode(",", $value);

    wp_set_object_terms($p_id, $attribute_values, $key, false);
    $thedata[sanitize_title($key)] = Array(
        'name' => wc_clean($key),
        'value' => $attribute_values,
        'postion' => '0',
        'is_visible' => '1',
        'is_variation' => '0',
        'is_taxonomy' => '1'
    );
    update_post_meta($p_id, '_product_attributes', $thedata);
}