WooCommerce: 如何以编程方式将产品变体属性设置为产品分类属性项?

huangapple go评论59阅读模式
英文:

WooCommerce: How to Programatically Set Product Variation Attributes as Product Taxonomy Attribute Items?

问题

我正在寻找一种以编程方式设置WooCommerce产品的产品变化属性的方法,而不是使用自定义属性,而是使用产品属性分类法。我理解这是设置自定义值的传统方法:

$variation->set_attributes( 
    array( 
        'Format' => 'Hardcover',
        'Condition' => 'New' 
    )
);

然而,我不想设置自定义产品属性。我想将产品变化属性设置为现有产品属性分类法的值,如下所示:

$variation->set_attributes( 
    array( 
        'pa_format' => $woo_product_meta['format'], // 这是一个等于'Hardcover'的字符串,
        'pa_condition' => $woo_product_meta['condition'] // 这是一个等于'New'的字符串
    )
);

这种语法不起作用。我正在使用产品属性分类法作为我的产品属性,如下所示:

// 设置产品属性
$attributes = array();

// 添加格式属性
$attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_format' ) );
$attribute->set_name( 'pa_format' );

// 以编程方式获取现有配置的术语
$attribute_format_terms = get_terms( array( 'taxonomy' => 'pa_format' ) );
$attribute_format_term_id = false;
foreach ( $attribute_format_terms as $term ) {
    if ( $term->name == $woo_product_meta['format'] ) {
        $attribute_format_term_id = $term->term_id;
    } 
}
$attribute->set_options( array( $attribute_format_term_id ) );
$attribute->set_position( 0 );
$attribute->set_visible( true );
$attribute->set_variation( true );
$attributes[] = $attribute;
                
// 添加条件属性
$attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_condition' ) );
$attribute->set_name( 'pa_condition' );

// 以编程方式获取现有配置的术语
$attribute_condition_terms = get_terms( array( 'taxonomy' => 'pa_condition' ) );
$attribute_condition_term_id = false;
foreach ( $attribute_condition_terms as $term ) {
    if ( $term->name == $woo_product_meta['condition'] ) {
        $attribute_condition_term_id = $term->term_id;
    } 
}
$attribute->set_options( array( $attribute_condition_term_id ) );
$attribute->set_position( 1 );
$attribute->set_visible( true );
$attribute->set_variation( true );
$attributes[] = $attribute;
                
$product->set_attributes( $attributes );

在这种情况下,$woo_product_meta['condition'] 和 $woo_product_meta['format'] 都返回来自表单输入的字符串。Condition 将是 'New' 或 'Used',而 Format 将是 'Hardcover' 或 'Paperback'。我期望的结果是使这段代码工作:

// 创建产品变化
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product->get_id() );

$variation->set_attributes( 
    array( 
        'pa_format' => $woo_product_meta['format'],
        'pa_condition' => $woo_product_meta['condition'] 
    )
);

以一种方式工作,将产品变化属性设置为它们各自的WooCommerce产品属性分类法值。例如,如果 $woo_product_meta['format'] 等于 'Hardcover',那么脚本应该访问 'pa_format' 的配置术语,并将 'pa_format' 的产品变化属性设置为此值。对于 Condition 也是一样的。

我已经尝试了一切,甚至使用了AI来帮助,但都没有成功。我不太确定我是否在提出正确的问题并提供正确的细节,所以如果您需要澄清和额外的信息,请告诉我。非常感谢您在这个问题上的帮助!

英文:

I am looking for a way to programmatically set product variation attributes on a WooCommerce product using product attribute taxonomies instead of custom attributes. I understand this is the traditional way to set custom values:

$variation->set_attributes( 
	array( 
		'Format' => 'Hardcover',
		'Condition' => 'New' 
	)
);

However, I don't want to set custom product attributes. I want to set the product variation attributes as existing product attributes taxonomy values like so:

$variation->set_attributes( 
	array( 
		'pa_format' => $woo_product_meta['format'] // this is a string equal to 'Hardcover',
		'pa_condition' => $woo_product_meta['condition'] // this is a string equal to 'New'
	)
);

This syntax does not work. I am using product attribute taxonomies as my product attributes like so:

// Set product attributes
$attributes = array();

// add the format attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_format' ) );
$attribute->set_name( 'pa_format' );

// get existing configured terms programatically
$attribute_format_terms = get_terms( array( 'taxonomy' => 'pa_format' ) );
$attribute_format_term_id = false;
foreach ( $attribute_format_terms as $term ) {
	if ( $term->name == $woo_product_meta['format'] ) {
		$attribute_format_term_id = $term->term_id;
	} 
}
$attribute->set_options( array( $attribute_format_term_id ) );
$attribute->set_position( 0 );
$attribute->set_visible( true );
$attribute->set_variation( true );
$attributes[] = $attribute;
				
// add the condition attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_condition' ) );
$attribute->set_name( 'pa_condition' );
// get existing configured terms programatically
$attribute_condition_terms = get_terms( array( 'taxonomy' => 'pa_condition' ) );
$attribute_condition_term_id = false;
foreach ( $attribute_condition_terms as $term ) {
	if ( $term->name == $woo_product_meta['condition'] ) {
		$attribute_condition_term_id = $term->term_id;
	} 
}
$attribute->set_options( array( $attribute_condition_term_id ) );
$attribute->set_position( 1 );
$attribute->set_visible( true );
$attribute->set_variation( true );
$attributes[] = $attribute;
				
$product->set_attributes( $attributes );

And in this case, $woo_product_meta['condition'] and $woo_product_meta['format'] both return strings from a form input. Condition will either be 'New' or 'Used' and Format will either be 'Hardcover' or 'Paperback'. My desired outcome is to have this code:

// create product variation
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product->get_id() );

$variation->set_attributes( 
	array( 
		'pa_format' => $woo_product_meta['format'],
		'pa_condition' => $woo_product_meta['condition'] 
	)
);

Work in a way that sets the product variation attributes to their respective WooCommerce product attribute taxonomy values. For example, if $woo_product_meta['format'] is equal to 'Hardcover', then the script should access the configured terms for 'pa_format' and set the product variation attribute for pa_format equal to this value. Same thing for Condition.

I've tried everything and even used AI to help to no avail. I'm not entirely sure I am asking the right questions and providing the correct details, so please let me know if you need clarity and additional information. Thank you so much for your help in this matter!

答案1

得分: 1

终于找到了解决方法,我想在这里分享一下,以防未来有人需要。

在获取您的属性分类(分配给父产品的分类)时,使用以下方法非常重要:

$term = get_term_by('name', $taxonomy_item_name, $taxonomy);

然后,在设置您的变体属性时,非常重要的是不要使用术语名称($term->name)或ID($term->term_id),而是使用slug($term->slug),如下所示:

$variation->set_attributes( 
	array( 
		'pa_format' => $term->slug,
		'pa_condition' => $term->slug
	)
);

我找到的每个在线论坛和文档都明确指出,只有ID或易读的名称才能起作用,但在我的情况下,两者都不起作用,并且都返回(在调试日志中):

PHP Warning: Undefined array key "{术语名称或ID}" in /wp-content/plugins/woocommerce/src/Internal/ProductAttributesLookup/LookupDataStore.php on line 459

所以在一时兴起测试slug后,我意识到对于这种情况它完美地起作用。在尝试为产品设置默认属性时,使用slug也是正确的。以下是实现这一目标的工作代码:

$product->update_meta_data( '_default_attributes', array( 'pa_taxonomy' => $term->slug ) );

希望这对您有所帮助!

英文:

Finally found a solution to this and I want to share it in case it helps someone else in the future.

It is essential to get your attribute taxonomy (the one assigned to your parent product) using the following:

$term = get_term_by('name', $taxonomy_item_name, $taxonomy);

Then, when setting your variation attributes, it is essential to not use the term name ($term->name) or ID ($term->term_id) and instead use the slug ($term->slug), like so:

$variation->set_attributes( 
	array( 
		'pa_format' => $term->slug,
		'pa_condition' => $term->slug
	)
);

Every online forum and documentation piece I could find made it clear that only the ID or human-readable name will work, but in my case, neither worked, and both returned (in the debug log):

PHP Warning: Undefined array key "{term name or ID}" in /wp-content/plugins/woocommerce/src/Internal/ProductAttributesLookup/LookupDataStore.php on line 459

So after testing the slug on a whim, I realized it works perfectly for this case. Using the slug is also true when trying to set default attributes for a product. Here is the working code to achieve that:

$product->update_meta_data( '_default_attributes', array( 'pa_taxonomy' => $term->slug ) );

Hope this helps!

huangapple
  • 本文由 发表于 2023年6月16日 07:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定