簡體   English   中英

Wordpress Woocommerce - 使用 update_post_meta 添加產品屬性

[英]Wordpress Woocommerce - use update_post_meta to add product attributes

我客戶網站上的產品需要某些屬性,我通過產品 -> Wordpress 管理中的屬性添加了這些屬性 在我編寫的這個導入腳本中,我需要使用函數update_post_meta($post_id, $meta_key, $meta_value)來導入正確的屬性和值。

目前我有這樣的功能:

update_post_meta( $post_id, '_product_attributes', array());

但是我不確定如何正確傳遞屬性及其值?

是的,所以我自己花了一段時間才弄明白,但我終於通過編寫以下函數設法做到了這一點:

// @param int $post_id - The id of the post that you are setting the attributes for
// @param array[] $attributes - This needs to be an array containing ALL your attributes so it can insert them in one go
function wcproduct_set_attributes($post_id, $attributes) {
    $i = 0;
    // Loop through the attributes array
    foreach ($attributes as $name => $value) {
        $product_attributes[$i] = array (
            'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
            'value' => $value, // set attribute value
            'position' => 1,
            'is_visible' => 1,
            'is_variation' => 1,
            'is_taxonomy' => 0
        );

        $i++;
    }

    // Now update the post with its new attributes
    update_post_meta($post_id, '_product_attributes', $product_attributes);
}

// Example on using this function
// The attribute parameter that you pass along must contain all attributes for your product in one go
// so that the wcproduct_set_attributes function can insert them into the correct meta field.
$my_product_attributes = array('hdd_size' => $product->hdd_size, 'ram_size' => $product->ram_size);

// After inserting post
wcproduct_set_attributes($post_id, $my_product_attributes);

// Woohay done!

我希望這個功能可以幫助其他需要在 WooCommerce 中以編程方式導入多個屬性的人!

我嘗試了丹尼爾的回答,但對我不起作用。 可能是 Wordpress/Woocommerce 代碼從那以后發生了變化,或者我不太明白如何去做,但不管怎樣,這些代碼對我沒有任何幫助。 然而,在使用它作為基礎進行大量工作之后,我想出了這段代碼並將其放在我的主題的functions.php

function wcproduct_set_attributes($id) {

    $material = get_the_terms( $id, 'pa_material');

    $material = $material[0]->name;

    // Now update the post with its new attributes
    update_post_meta($id, '_material', $material);

}

// After inserting post
add_action( 'save_post_product', 'wcproduct_set_attributes', 10);

有了這個,我可以將我在 WooCommerce 安裝中設置為“材料”的內容作為自定義屬性,並將其添加到正式元數據中作為 _material。 這反過來又允許我使用另一段代碼,以便 WooCommerce 搜索功能擴展到元字段,這意味着我可以在 WooCommerce 搜索字段中搜索材料,並顯示包含該材料的所有項目。

我希望這對某人有用。

@Daniels 的答案有效,不會決定對與錯,但是如果您想將值作為分類術語添加到屬性下,您必須調整如下代碼(設置 is_taxonomy = 1)。 否則 Woocommerce 會將其視為自定義元字段(?)。 它仍然在屬性下添加值。 這僅適用於字符串。 對於數組值,必須修改代碼。

此外,它還使用@Anand 建議的 wp_set_object_terms。 我正在使用它,因為我能找到的所有文檔都讓人相信必須使用它。 但是,如果僅使用 wp_set_object_terms,則我無法在編輯產品屏幕中看到屬性。 使用來自答案和閱讀主題的信息得出了解決方案。

您將需要針對產品變體等內容調整代碼。

/*
 * Save Woocommerce custom attributes
 */

function save_wc_custom_attributes($post_id, $custom_attributes) {
    $i = 0;
    // Loop through the attributes array
    foreach ($custom_attributes as $name => $value) {
        // Relate post to a custom attribute, add term if it does not exist
        wp_set_object_terms($post_id, $value, $name, true);
        // Create product attributes array
        $product_attributes[$i] = array(
            'name' => $name, // set attribute name
            'value' => $value, // set attribute value
            'is_visible' => 1,
            'is_variation' => 0,
            'is_taxonomy' => 1
        );
        $i++;
    }
    // Now update the post with its new attributes
    update_post_meta($post_id, '_product_attributes', $product_attributes);
}

然后調用函數:

   $custom_attributes = array('pa_name_1' => $value_1, 'pa_name_2' => $value_2, 'pa_name_3' => $value_3);
   save_wc_custom_attributes($post_id, $custom_attributes);

感謝您發布代碼 Daniel & Anand。 它幫了我很大的忙。

不知道這是否是執行此操作的“正確”方法......但我需要一個函數來添加帶有日期值的 ACF 中繼器字段作為保存后的屬性,所以這是我想出的函數:

add_action( 'save_post', 'ed_save_post_function', 10, 3 );

function ed_save_post_function( $post_ID, $post, $update ) {
  //print_r($post);
  if($post->post_type == 'product')
    {
     $dates = get_field('course_dates', $post->ID);
     //print_r($dates);
     if($dates)
       {
        $date_arr = array();
        $val = '';
        $i = 0;
        foreach($dates as $d)
               {
                 if($i > 0)
                   {
                    $val .= ' | '.date('d-m-Y', strtotime($d['date']));   
                   }
               else{
                    $val .= date('d-m-Y', strtotime($d['date']));
                   } 
                 $i++;  
               }
            $entry = array(
                           'course-dates' => array(
                                                   'name' => 'Course Dates',
                                                   'value' => $val,
                                                   'position' => '0',
                                                   'is_visible' => 1,
                                                   'is_variation' => 1,
                                                   'is_taxonomy' => 0
                                                  )
                          );
                update_post_meta($post->ID, '_product_attributes', $entry);
       }
    }
}

希望這可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM