当前位置: 首页 > 知识库问答 >
问题:

如何使用已经存在的产品属性,而不使用WooCommerce中的WC_product_属性类创建新的产品属性?

侯兴为
2023-03-14

我试图创建一个Wordpress插件,以编程方式创建变量产品,但问题是,我想使用我以前从管理仪表板手动定义的属性。我需要将属性分配给我正在创建的产品。

以下是我正在使用的代码(不是我的,我从以下答案中得到:在WooCommerce中以编程方式创建一个可变产品和两个新属性):

function addProduct(){
    //Create main product
    $product = new WC_Product_Variable();

    //Create the attribute object
    $attribute = new WC_Product_Attribute();

    //pa_size tax id
    $attribute->set_id( 0 ); // -> SET to 0

    //pa_size slug
    $attribute->set_name( 'Couleur' ); // -> removed 'pa_' prefix

    //Set terms slugs
    $attribute->set_options( array(
        'Noir'
    ) );

    $attribute->set_position( 0 );

    //If enabled
    $attribute->set_visible( 1 );

    //If we are going to use attribute in order to generate variations
    $attribute->set_variation( 1 );

    $product->set_attributes(array($attribute));

    //Save main product to get its id
    $id = $product->save();

    $variation = new WC_Product_Variation();
    $variation->set_regular_price(10);
    $variation->set_parent_id($id);

    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'Couleur' => 'Noir' // -> removed 'pa_' prefix
    ));

    //Save variation, returns variation id
    echo get_permalink ( $variation->save() );


    // echo get_permalink( $id ); // -> returns a link to check the newly created product   
}

产品被正确地创建,但是代码创建了一个新属性,并将其称为“Couleur”,而不是使用我已经定义的“Couleur”属性。

提前谢谢。

共有2个答案

段干子晋
2023-03-14

WooCommerce允许您使用全局属性和产品级(本地或自定义)属性。

全局属性可用于所有产品。删除该属性的术语时,所有使用该属性和该术语的产品都将删除该术语。相反,当您添加新术语时,它将适用于所有其他产品。

全局属性您可以在Wordpress仪表板中找到它们

而产品级别(本地或自定义)属性仅为该产品定义,其他产品将不可用。

产品级属性只能在“属性”部分的产品编辑页面中找到。

您可以在此处找到更详细的信息:

  • 在WooCommerce中管理产品属性
  • 产品变化:全局或局部属性-为什么使用一个与其他

解决方案

以编程方式,您需要向每个属性slug添加前缀pa

您还将id设置为零:$属性-

// get a product attribute ID by name.
$attribute_id = wc_attribute_taxonomy_id_by_name( 'Couleur' );
$attribute->set_id( $attribute_id );

不要用这个答案,你应该用这个。

有用链接

  • 在WooCommerce中以编程方式创建一个可变产品和两个新属性

鲁淇
2023-03-14

多亏了Vincenzo Di Gaetano,问题才得以解决。

这是工作代码:

function add_product(){
    //Create main product
    $product = new WC_Product_Variable();

    //Create the attribute object
    $attribute = new WC_Product_Attribute();

    // get a product attribute ID by name.
    $attribute_id = wc_attribute_taxonomy_id_by_name( 'Couleur' );
    $attribute->set_id( $attribute_id );

    //pa_size slug
    $attribute->set_name( 'pa_couleur' ); // -> removed 'pa_' prefix

    //Set terms slugs
    $attribute->set_options( array(
        'noir'
    ) );

    $attribute->set_position( 0 );

    //If enabled
    $attribute->set_visible( 1 );

    //If we are going to use attribute in order to generate variations
    $attribute->set_variation( 1 );

    $product->set_attributes(array($attribute));

    //Save main product to get its id
    $id = $product->save();

    $variation = new WC_Product_Variation();
    $variation->set_regular_price(10);
    $variation->set_parent_id($id);

    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'pa_couleur' => 'noir' // -> removed 'pa_' prefix
    ));

    //Save variation, returns variation id
    echo get_permalink ( $variation->save() );


    // echo get_permalink( $id ); // -> returns a link to check the newly created product   
}
 类似资料:
  • 我正在查询如下所示的帖子: 这将返回product类型的所有post。但我在wooCommerce中创建的产品上有自定义属性,但我似乎在任何地方都找不到这些信息。 我尝试使用: 我还尝试: 当我print_r这些我得到了很多信息但我不能看到的产品属性从WooCommerce任何地方 下图显示了如何在产品上设置此信息。 查询帖子后如何访问此信息?特别是我需要提取的颜色和大小。

  • 我正在尝试向现有的WooCommerce产品添加一个具有多个值的新属性。 这是我正在使用的代码。我把它简化了一点,使它更清楚。 该属性显示在产品的属性列表中。然而,条款没有添加。 数据库中也有这些术语: 我做错了什么?我做了很多研究,读了一些其他的问题,但它们对我没有帮助。 在WooCommerce中以编程方式创建新产品属性

  • 每次根据其他几个字段的内容保存产品时,我都希望更新产品在我的商店中的属性。 然而,我正在努力让我的属性正确出现。 最初,我试图使用以下功能根据这个问题的公认答案更新产品: 这将添加到了分类法的可用术语列表中,但在我检查产品属性时,它们不可见。 接下来,我尝试了上述问题中第二个答案的变体: 这也无法更新产品的属性。 在这一点上,我不知道下一步该尝试什么。使用操作似乎是有效的,因为这个术语确实被添加到

  • 在WooCommerce中,我试图使用以下代码将产品类别转换为产品属性: Is的部分工作原理与我在后端产品中需要看到的一样

  • 如何从插件为WooCommerce创建属性?我只发现: 从这个问题 但这种方法需要某些产品的id。我需要生成一些未附加到任何产品的属性。

  • 我正在使用此代码添加自定义属性 这个代码的结果我得到了添加只是产品属性名称没有术语值... 我找了很多,但没有得到任何答案。