Woocommerce 以编程方式添加产品属性及其相应的值

2024-01-10

我正在构建一个获取产品的脚本,但我陷入了以编程方式添加属性的部分。所以基本上我想检查属性是否存在,如果不存在则添加它。然后检查它的价值是否存在,如果不存在,他们会添加它的价值,并将所有内容附加到我的产品上。

这是我获得产品的格式:

 [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 );

}

这似乎只是将它们添加到“产品属性”选项卡下,也 不添加条款值。


好吧,这是一项相当复杂的任务,但这是我通过查看其他人的源代码并根据需要进行修改而获得的代码。

首先,您将需要这些辅助函数。

  1. 要创建全局属性,您需要使用以下函数为每个属性(性别、车轮尺寸等)创建一个分类法:

    function create_global_attribute($name, $slug)
    {
    
        $taxonomy_name = wc_attribute_taxonomy_name( $slug );
    
        if (taxonomy_exists($taxonomy_name))
        {
            return wc_attribute_taxonomy_id_by_name($slug);
        }
    
        //logg("Creating a new Taxonomy! `".$taxonomy_name."` with name/label `".$name."` and slug `".$slug.'`');
    
        $attribute_id = wc_create_attribute( array(
            'name'         => $name,
            'slug'         => $slug,
            'type'         => 'select',
            'order_by'     => 'menu_order',
            'has_archives' => false,
        ) );
    
        //Register it as a wordpress taxonomy for just this session. Later on this will be loaded from the woocommerce taxonomy table.
        register_taxonomy(
            $taxonomy_name,
            apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
            apply_filters( 'woocommerce_taxonomy_args_' . $taxonomy_name, array(
                'labels'       => array(
                    'name' => $name,
                ),
                'hierarchical' => true,
                'show_ui'      => false,
                'query_var'    => true,
                'rewrite'      => false,
            ) )
        );
    
        //Clear caches
        delete_transient( 'wc_attribute_taxonomies' );
    
        return $attribute_id;
    }
    
  2. 现在要将这些应用到您的产品中,我假设您正在使用可变产品,每个产品都有变体?父可变产品需要有自己的attributeproperty 设置为特殊格式的属性数组。该函数创建该数组,如果未找到每个属性,则为每个属性创建全局分类法。

    //$rawDataAttributes must be in the form of array("Color"=>array("blue", "red"), "Size"=>array(12,13,14),... etc.)
    function generate_attributes_list_for_product($rawDataAttributes)
    {
        $attributes = array();
    
        $pos = 0;
    
        foreach ($rawDataAttributes as $name => $values)
        {
            if (empty($name) || empty($values)) continue;
    
            if (!is_array($values)) $values = array($values);
    
            $attribute = new WC_Product_Attribute();
            $attribute->set_id( 0 );
            $attribute->set_position($pos);
            $attribute->set_visible( true );
            $attribute->set_variation( true );
    
            $pos++;
    
            //Look for existing attribute:
            $existingTaxes = wc_get_attribute_taxonomies();
    
            //attribute_labels is in the format: array("slug" => "label / name")
            $attribute_labels = wp_list_pluck( $existingTaxes, 'attribute_label', 'attribute_name' );
            $slug = array_search( $name, $attribute_labels, true );
    
            if (!$slug)
            {
                //Not found, so create it:
                $slug = wc_sanitize_taxonomy_name($name);
                $attribute_id = create_global_attribute($name, $slug);
            }
            else
            {
                //Otherwise find it's ID
                //Taxonomies are in the format: array("slug" => 12, "slug" => 14)
                $taxonomies = wp_list_pluck($existingTaxes, 'attribute_id', 'attribute_name');
    
                if (!isset($taxonomies[$slug]))
                {
                    //logg("Could not get wc attribute ID for attribute ".$name. " (slug: ".$slug.") which should have existed!");
                    continue;
                }
    
                $attribute_id = (int)$taxonomies[$slug];
            }
    
            $taxonomy_name = wc_attribute_taxonomy_name($slug);
    
            $attribute->set_id( $attribute_id );
            $attribute->set_name( $taxonomy_name );
            $attribute->set_options($values);
    
            $attributes[] = $attribute;
        }
    
    
        return $attributes;
    }
    
  3. 我们几乎准备好将属性应用于产品,但主要的变量产品及其变体需要像任何 Wordpress 分类法一样为它们分配分类术语。因此,如果您有一个名为“车轮尺寸”的分类法,其中包含术语“28 英寸”和“30 英寸”,则父产品需要分配所有“28 英寸”和“30 英寸”术语,并且变体需要为其分配单独的术语,因此一个术语为“28 英寸”,另一个术语为“30 英寸”。为了帮助解决这个问题,我们使用此函数来获取和/或创建这些术语(如果它们不存在):

    function get_attribute_term($value, $taxonomy)
    {
        //Look if there is already a term for this attribute?
        $term = get_term_by('name', $value, $taxonomy);
    
        if (!$term)
        {
            //No, create new term.
            $term = wp_insert_term($value, $taxonomy);
            if (is_wp_error($term))
            {
                //logg("Unable to create new attribute term for ".$value." in tax ".$taxonomy."! ".$term->get_error_message());
                return array('id'=>false, 'slug'=>false);
            }
            $termId = $term['term_id'];
            $term_slug = get_term($termId, $taxonomy)->slug; // Get the term slug
        }
        else
        {
            //Yes, grab it's id and slug
            $termId = $term->term_id;
            $term_slug = $term->slug;
        }
    
        return array('id'=>$termId, 'slug'=>$term_slug);
    }
    

好吧,现在我们终于可以使用上面的内容将属性应用到我们的产品了。

对于主要变量产品:

$yourRawAttributeList = array("Gender" => array("Woman", "Man"), "Wheel Size" => array("28 Inch","30 Inch","32 Inch"));
$attribs = generate_attributes_list_for_product($yourRawAttributeList);

$p = new WC_Product_Variable($postID);

$p->set_props(array(
    'attributes'        => $attribs,
    //Set any other properties of the product here you want - price, name, etc.
));

$postID = $p->save();

if ($postID <= 0) return "Unable to create / update product!";

//Attribute Terms: These need to be set otherwise the attributes dont show on the admin backend:
foreach ($attribs as $attrib)
{
    /** @var WC_Product_Attribute $attrib */
    $tax = $attrib->get_name();
    $vals = $attrib->get_options();

    $termsToAdd = array();

    if (is_array($vals) && count($vals) > 0)
    {
        foreach ($vals as $val)
        {
            //Get or create the term if it doesnt exist:
            $term = get_attribute_term($val, $tax);

            if ($term['id']) $termsToAdd[] = $term['id'];
        }
    }

    if (count($termsToAdd) > 0)
    {
        wp_set_object_terms($postID, $termsToAdd, $tax, true);
    }
}

对于该产品的每个变体:

    //This is an array of input attributes in the form: array("Color"=>"Navy", "Size"=>"25")
$theseAttributes = array("Gender" => array("Woman"), "Wheel Size" => array("28 Inch"));

//This is the final list of attributes that we are calculating below.
$theseAttributesCalculated = array();

//logg("Want to add these attributes to the variation: ".print_r($theseAttributes, true));

$existingTax = wc_get_attribute_taxonomies();

foreach ($theseAttributes as $name => $value)
{
    if (strlen($name) == 0 || strlen($value) == 0)
    {
        //logg("Attribute array had a blank value for product variant ".$sku.': '.print_r($theseAttributes, true));
        return "Attribute array had a blank value.";
    }

    $tax = '';
    $slug = '';

    //Look for an existing taxonomy to match this attribute's $name
    //$thistax->attribute_name = slug of the taxonomy
    //$thistax->attribute_label = name of the taxonomy

    foreach ($existingTax as $thistax)
    {
        if ($thistax->attribute_label == $name)
        {
            $slug = $thistax->attribute_name;
            $tax = wc_attribute_taxonomy_name($slug);
            break;
        }
    }

    if (empty($tax))
    {
        $slug = wc_sanitize_taxonomy_name($name);
        //Taxonomy not found, so create it...
        if (create_global_attribute($name, $slug) > 0)
        {
            $tax = wc_attribute_taxonomy_name($slug);
        }
        else
        {
            //logg("Unable to create new attribute taxonomy ".$slug." for attribute ".$name."found in variable product ".$sku);
            continue;
        }
    }


    //logg("Want to add attribute ".$name. " value ".$value. " which is term ".$term_slug." (".$termId.") to post ".$parentID);

    $term = get_attribute_term($value, $tax);


    if ($term['id'])
    {
        // Set/save the attribute data in the product variation
        $theseAttributesCalculated[$tax] = $term['slug'];
    }
    else
    {
        //logg("Warning! Unable to create / get the attribute ".$value." in Taxonomy ".$tax);
    }
}

//logg("Finished gathering. Results: ".print_r($theseAttributesCalculated, true));


$variation  = new \WC_Product_Variation($postID);

$variation->set_props( array(
    'attributes'        => $theseAttributesCalculated,
));

$postID = $variation->save();

if ($postID <= 0) return "Unable to create product variation!";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Woocommerce 以编程方式添加产品属性及其相应的值 的相关文章

  • 自定义 WordPress 画廊 html 布局

    当使用默认媒体上传器在 WordPress 中创建图像库时 WordPress 将图像包装在一堆 HTML 标记中 如何在生成之前覆盖它 以便我可以输出所需的标记并更改创建图库布局的方式 目前 WordPress 生成的代码如下 div d
  • 2 使用我的代码在数组中查询

    我使用滑块来显示我的 WordPress 精选文章 它选择一个自定义类别并返回一定数量的帖子 如何将显示的第一篇帖子设为自定义帖子 我可以直接在滑块代码中添加特定帖子的 ID吗使该帖子首先出现 然后是原始查询返回的其他内容 例如 在页面上
  • WordPress - 类别和子类别的嵌套列表

    我正在尝试显示带有嵌套子类别的 WordPress 类别列表 到目前为止 我只能获取父类别列表或不包括父类别的子类别列表 但我无法将两者连接在一起 这是我想要创建的结果 Parent Category 子类别 子类别 Parent Cate
  • 将延期交货库存状态添加到 Woocommerce 可变产品下拉列表中

    我想在下拉菜单中显示可变产品的库存状态 包括 缺货 因为我网站上的大多数产品都缺货 而不是 缺货 我已经尝试过答案如何将变体库存状态添加到 Woocommerce 产品变体下拉列表中 https stackoverflow com ques
  • 使用数据绑定,如何将包含表情符号的文本绑定到标签并使其正确显示?

    我正在编写一个应用程序来连接 WordPress BuddyPress API 该应用程序将允许用户通过 API 相互发送消息 当这些消息包含表情符号时 我很难正确显示它们 以下是 API 返回的消息文本的简短示例 Hi x1f642 ho
  • 根据推荐链接自动选择联系表单 7 中的字段

    我一直在使用 Aurovrata 的这个答案 WordPress联系表单7根据url动态选择下拉字段 https stackoverflow com questions 63251548 wordpress contact form 7 d
  • Woocommerce:添加第二个电子邮件地址不起作用,除非收件人是管理员

    我尝试了多种方法来向 Woocommerce 电子邮件添加其他收件人 但它似乎仅适用于主要收件人是管理员的测试订单 这些是我尝试过的片段 如果订单的客户是管理员 则电子邮件将发送到这两个地址 如果订单包含客户电子邮件地址 则仅发送至该电子邮
  • 如何解决输入字段上的错误行高?

    如何为具有固定高度的输入字段提供其所包含文本的跨浏览器垂直对齐方式 截至目前看来line height是我最好的选择 但是 在 Chrome 或 Safari 等 webkit 浏览器上 复制 粘贴时光标会跳至顶部对齐 我注意到 WordP
  • 通过 PHP 脚本重新启动 Nginx

    我目前正在努力使我的 WordPress 插件与 nginx 兼容 该插件需要访问 conf文件在wp content uploads目录 以便它可以添加所需的规则 目前 它更新了 htaccess文件位于同一目录中 更改立即生效 无需干预
  • 在没有数据库的情况下运行 WordPress

    我一直在寻找一种将 WordPress 配置为仅使用文件系统数据库运行的方法 有点像 Java 中或内存中的 H2 任何人 仅用于演示目的 不可能 Wordpress 的要求之一是 MySQL http wordpress org abou
  • 根据产品变体术语将收件人添加到 Woocommerce 电子邮件通知

    我创建了一个 Woocommerce 插件并要求它做两件事 根据购物车中的产品变体 向特定电子邮件地址发送通知消息 电子邮件必须仅包含相关产品 不得包含其他属性的产品 例如 产品 A 具有名为 Chef 的属性 其中 Chef one 和
  • WordPress 类别链接 get_category_link(id)

    我需要链接到我的 WordPress 网站中的一个类别 下面的代码在某种程度上可以工作 我的问题是它在网址中包含 category 这不是我的永久链接结构的设计方式 有谁知道在它输出的 url 中包含 category 的方法吗 我不明白你
  • 在 WooCommerce 中检查购物车中的多个产品 ID

    我使用以下代码来检查产品 ID 是否在购物车中 如果是 则添加额外的结帐字段 add action woocommerce after order notes conditional checkout field function cond
  • PHP 7.2 计数错误

    警告 count 参数必须是数组或对象 实现 Countable in 我在以下行中收到上述错误 if 0 gt count this gt xprop 有人可以帮助我理解这一点吗 我对 PHP 还很陌生 问题显然是 this gt xpr
  • 计算帖子中使用 WordPress 短代码的次数

    我有以下 WordPress 短代码功能 function wp shortcode static i 1 return i i return return add shortcode shortcode wp shortcode 这很好用
  • WordPress 子文件夹安装重定向到根目录

    我遇到了一个奇怪的问题 我有我的主站点 mesopinions ca 它是一个 WordPress 安装 我还有几个子域 有自己的 WordPress 安装 无多站点 它与 concours mesopinions ca 子文件夹配合得很好
  • 获取php中两个日期之间的天数[关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我试图获取两个日期之间的天数 但返回的结果不正确 这是我的尝试 t time get the time Y m d g
  • 错误“RESOURCE_EXHAUSTED”的原因是什么?

    我有一个 WordPress 博客 当我登录到仪表板时 顶部突出显示了以下异常 Ga Lib Api Request Exception There was an error while contacting Google API erro
  • 如何仅将网站的特定部分放入 iframe 中?

    我只想将网站的一小部分放入 iframe 中 我该怎么做 通常 当我为网站 假设是雅虎 设置 iframe 时 它 会获取整个网站 假设我只想要网站的一小部分 我该怎么做 是否可以在网站的 iframe 上添加边距 我想在我的网站上放置一个
  • PHP简单的html dom解析器与wordpress冲突吗?

    PHP简单的html dom解析器与wordpress冲突吗 因为每当我尝试将其与此代码一起包含在我的标头中时 一切都变成空白 感谢您提前提供任何帮助 当我尝试在 HTML 文档中使用 PHP 包含时 我遇到了同样的问题 但当我使用func

随机推荐

  • 无法通过 RVM 安装 Ruby 2.0.0

    我继承了一个遗留代码库 需要使用 RVM 安装 Ruby 2 0 0 我使用的是 Mac OS X Catalina Ruby 2 0 0 的安装程序根本不起作用 以下是我的安装尝试 从 RVM 本身的安装一直到尝试安装 Ruby 2 0
  • 如何使用 Aurelia 通过单击内部锚链接保持在同一页面上?

    我正在为一个项目制定样式指南 目前我希望在锚链接上有一个基本的点击行为 以便它们滚动到相应的 ID 举个例子 a href section a 向下滚动到 div div 在 Aurelia 中 默认行为是将链接视为路由 我无法使内部链接正
  • 以编程方式调用 DockPanel-Suite 的“AutoHidden”DockContent

    我无法以编程方式显示 自动隐藏 底座 虽然在网上找不到任何答案 但如下那么问题 https stackoverflow com questions 12956147 dockpanel suite dockcontent visibilit
  • Swift 项目中链接的框架和库

    我的 iOS 应用程序播放声音AVPlayer 为此 我必须在类的顶部输入以下内容 import AVFoundation 我有 Objective C 背景 所以我去了 Xcode 项目General选项卡并添加AVFoundation单
  • 触摸移动被卡住 忽略取消触摸移动的尝试

    我正在处理触摸滑块上的触摸事件 并且不断收到以下错误 忽略使用 cancelable false 取消 touchmove 事件的尝试 例如 因为滚动正在进行中并且无法滚动 打断了 我不确定是什么导致了这个问题 我是触摸事件的新手 似乎无法
  • 如何让一个简单的Hello World在Windows中“隐形”(C/C++)

    你好 我想知道是否可以在 Windows 中使一个简单的 Hello World 程序 然后前进到实际的程序 不可见 我的意思是 当我执行该程序时 不会有任何图形指示 没有cmd打印 Hello world 没有任务栏标签 没有系统托盘图标
  • android中如何禁止点击ListView?

    我有一个 ListView 我正在用数据库中的值填充它 如果数据库为空 我将 ListView 的第一项设置为 无数据 我想禁止点击该项目 我用过ArrayAdapter 我尝试将 areAllItemsEnabled isEnabled
  • 如何在 Kusto 中创建任意大小的窗口?

    Using prev https learn microsoft com en us azure data explorer kusto query prevfunction函数我可以单独访问前几行 mytable sort by Time
  • 如何获取用户玩我的游戏的时间? Steamworks API

    如何获取用户玩我的游戏的时间 在 Unity 中使用 Steamworks API 和 C 我已经浏览了文档 但没有找到类似的内容 并认为我遗漏了一些东西 我应该使用一个简单的脚本来记录自己游戏中的比赛时间 但为时已晚 如果有人能给我一个打
  • 当Java TimerTask在Timer中被调度时,它是否已经“执行”了?

    我想澄清一些关于 TimerTask 的事情 当你有下面的代码时 timer schedule task 60000 如果任务计划在接下来的 1 分钟内运行 则任务对象是否已经在执行 因为在我的代码中的某个地方我调用了 task cance
  • 使用 HTML5 数据属性的 AJAX 响应

    我有一个工作环境 我使用 AJAX 响应来填充 HTML 元素 例如 AJAX 响应有两个 或 n 个 对象 如下所示 0 Object id 111 Name abc 1 Object id 112 Name xyz 然后 已经有两个 或
  • 错误消息无法打开包含文件:'gxall.h':没有这样的文件或目录

    我正在尝试构建一些旧的 Visual C 代码 当我这样做时 我收到此错误消息 fatal error C1083 Cannot open include file gxall h No such file or directory 我该如
  • AWS Lambda 任务在 6.00 秒后超时

    我正在使用无服务器框架 我的 Lambda 函数连接到 DynamoDB 表以更新表中的项目 表的读写容量单位为 5 并且 auto scaling 被禁用 AWS Lambda 函数分配了 128MB 内存 我使用 Jmeter 进行性能
  • C++ 中的对话框未接收到某些击键

    当我 最终 学习用 C 编写 Windows 应用程序时 我偶然发现了这一点 我有一个用它创建的对话框CreateDialog 我还有一个默认按钮 然而 每次我按下TAB or ENTER 什么也没有发生 控制焦点也没有改变 默认按钮也没有
  • 使用 TPL 时避免窗口 (WPF) 冻结

    我正在构建一个 WPF 它有一个在 sql server 中执行 sql 查询的按钮 该查询可能需要很长时间才能运行 我想使用 TPL 来做到这一点 这段代码 var result Task Factory StartNew gt comm
  • 使用 PDO 有效获取带有 WHERE 子句的 SELECT 查询返回的行数

    有关于SO的大量讨论 https stackoverflow com search q pdo 20select 20number 20of 20rows 20 5Bmysql 5D关于如何获取运行时返回的行数SELECT使用 PDO 进行
  • 在应用程序中,活动和服务是否会在同一进程中运行?

    面试问题 在一个应用程序中 Activity和Service是运行在同一个进程中还是不同的进程中 我的答案是 在同一过程中 下一个问题 如果是的话 Activity和Service是如何同时运行的 我的答案是 操作系统将负责执行 坦白说 我
  • 将 bigdecimal 转换为 double(不带指数格式)

    我正在对高十进制精度 BigDecimal 对象进行计算 我正在使用需要双精度参数的第三方库 当我尝试将其转换为 double 时 我得到指数格式的值而不是小数 BigDecimal 0 000035000000000 Double 3 5
  • Winforms MDI“桌面”区域边界

    默认的MDI父控件有一个很大的 桌面 区域 可以显示多个子窗体 用户可以将表单拖动到此桌面区域的边缘 以便大部分子表单脱离屏幕 然后 MDI 父级中会出现一个滚动条 我不喜欢这个功能 有没有办法锁定桌面区域的边缘 以便子窗体保持完全可见 我
  • Woocommerce 以编程方式添加产品属性及其相应的值

    我正在构建一个获取产品的脚本 但我陷入了以编程方式添加属性的部分 所以基本上我想检查属性是否存在 如果不存在则添加它 然后检查它的价值是否存在 如果不存在 他们会添加它的价值 并将所有内容附加到我的产品上 这是我获得产品的格式 attrib