douyi8732 2017-08-02 10:00
浏览 487

在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

  • 写回答

3条回答 默认 最新

  • dongtiao2066 2017-08-02 10:31
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制