If Product Title Contains An Existing “Brand”, Add As Attribute - Woocommerce

∥☆過路亽.° 提交于 2020-01-17 14:45:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!