如果产品标题包含现有的“品牌",则添加为属性-Woocommerce [英] If Product Title Contains An Existing "Brand", Add As Attribute - Woocommerce
问题描述
我们正在寻找一种方法来自动添加产品属性,如果产品标题包含属性值并且该产品属性尚不存在.
We are looking for a way to automatically add a product attribute if the product title contains a value of an attribute, and the product attribute does not already exist.
我们正在寻找的功能如下:
The function we are looking for is as follows:
如果帖子"不包含属性:
然后,如果"post_title"包含"pa_brand","pa_color","pa_size","pa_gender",则添加为属性
Then, if 'post_title' Contains 'pa_brand', 'pa_color', 'pa_size', 'pa_gender' add as attribute
推荐答案
获取产品标题并检查其是否包含所需的字符串:
Get the product title and check if it contains desired string:
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids'
));
foreach ( $all_ids as $id ) {
$_product = wc_get_product( $id );
$_sku = $_product->get_sku();
$_title = $_product->get_title();
//check if product title contains "pa_brand"
if(strpos($_title, "pa_brand") != false)
{
//ADD PRODUCT ATTRIBUTE HERE
}
}
要添加产品属性,请检查: Wordpress Woocommerce -使用update_post_meta添加产品属性
To add product attribute kindly check: Wordpress Woocommerce - use update_post_meta to add product attributes
我希望此解决方案可以解决您的问题.祝你有美好的一天.
I hope this solution helps your problem. Have a good day.
这篇关于如果产品标题包含现有的“品牌",则添加为属性-Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!