【问题标题】:If Product Title Contains An Existing "Brand", Add As Attribute - Woocommerce如果产品标题包含现有的“品牌”,则添加为属性 - Woocommerce
【发布时间】:2019-10-02 00:13:03
【问题描述】:

我们正在寻找一种方法来自动添加产品属性,如果产品标题包含属性值,并且产品属性不存在。

我们要找的函数如下:

如果'post'不包含属性

那么,如果'post_title'包含'pa_brand'、'pa_color'、'pa_size'、'pa_gender'添加为属性

【问题讨论】:

    标签: php wordpress woocommerce attributes custom-wordpress-pages


    【解决方案1】:

    获取产品标题并检查它是否包含所需的字符串:

     $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 - use update_post_meta to add product attributes

    我希望这个解决方案可以帮助您解决问题。祝你有美好的一天。

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2022-12-17
      • 2018-12-14
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多