问题
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:
If 'post' does not contain attribute:
Then, if 'post_title' Contains 'pa_brand', 'pa_color', 'pa_size', 'pa_gender' add as attribute
回答1:
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
}
}
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.
来源:https://stackoverflow.com/questions/58193752/if-product-title-contains-an-existing-brand-add-as-attribute-woocommerce