WooCommerce以编程方式创建产品自定义属性字段?

时间:2022-05-26 08:07:08

I am trying to add Product Custom Attributes programatically under the URL field as shown in the figure: WooCommerce以编程方式创建产品自定义属性字段?

我试图在URL字段下以编程方式添加产品自定义属性,如图所示:

I have been able to do it using the following code using transition_post_status action:

我已经能够使用以下代码使用transition_post_status操作来执行此操作:

add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);

function wpa_120062_new_product($new_status, $old_status, $post){

    if( function_exists( 'wc_get_attribute_taxonomies' ) && ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) ) {

        $defaults = array();

        foreach ( $attribute_taxonomies as $key=>$tax ) {


                $name = wc_attribute_taxonomy_name( $tax->attribute_name );

                $value= get_post_meta( $post->ID , '_product_attributes');

                $defaults[ $name ] = array (
                    'name' => $name,

                    'position' => $key+1,
                    'is_visible' => 1,
                    'is_variation' => 1,
                    'is_taxonomy' => 1,
                );

            update_post_meta( $post->ID , '_product_attributes', $defaults );

        }
    }
}

But the problem here is that transition_post_status hook doesn't work great as it sometimes doesn't complete the loading of fields completely.

但问题是transition_post_status钩子不能很好地工作,因为它有时不能完全加载字段。

I have also tried to use wp action but no success.

我也试过使用wp动作但没有成功。

Can someone suggest how can I make this code work but using a different Hook?

有人可以建议我如何使这个代码工作,但使用不同的钩子?

1 个解决方案

#1


1  

There isn"t another hook for this purpose that you could use. But I have added to your function the missing variable global $post and a conditional that filters only new created published products.

没有为此目的提供另一个钩子,你可以使用。但是我已经在你的函数中添加了缺失的变量global $ post和一个条件,它只过滤新创建的已发布产品。

add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);
function wpa_120062_new_product($new_status, $old_status, $post){

    global $post;
    if( $old_status != 'publish' && $new_status == 'publish' && !empty($post->ID) 
    && in_array( $post->post_type, array( 'product') ) ) {

        if( function_exists( 'wc_get_attribute_taxonomies' ) && ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) ) {

            $defaults = array();
            foreach ( $attribute_taxonomies as $key=>$tax ) {
                $name = wc_attribute_taxonomy_name( $tax->attribute_name );
                $value= get_post_meta( $post->ID , '_product_attributes');
                $defaults[ $name ] = array (
                    'name' => $name,
                    'position' => $key+1,
                    'is_visible' => 1,
                    'is_variation' => 1,
                    'is_taxonomy' => 1,
                );
                update_post_meta( $post->ID , '_product_attributes', $defaults );
            }
        }
    }
}

In addition (if needed, but I am not sure) you could try to use wp_loaded hook to trigger transition_post_status once, because this hook is fired once WordPress, all plugins, and the theme are fully loaded and instantiated. It can be done this way:

另外(如果需要,但我不确定)你可以尝试使用wp_loaded钩子来触发transition_post_status一次,因为一旦WordPress,所有插件和主题被完全加载和实例化,就会触发此钩子。它可以这样做:

if( function_exists( 'wpa_120062_new_product' ) {
    add_action( 'wp_loaded', 'my_wp_is_loaded' );
    function my_wp_is_loaded(){
        do_action ( 'transition_post_status' );
    }
}

#1


1  

There isn"t another hook for this purpose that you could use. But I have added to your function the missing variable global $post and a conditional that filters only new created published products.

没有为此目的提供另一个钩子,你可以使用。但是我已经在你的函数中添加了缺失的变量global $ post和一个条件,它只过滤新创建的已发布产品。

add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);
function wpa_120062_new_product($new_status, $old_status, $post){

    global $post;
    if( $old_status != 'publish' && $new_status == 'publish' && !empty($post->ID) 
    && in_array( $post->post_type, array( 'product') ) ) {

        if( function_exists( 'wc_get_attribute_taxonomies' ) && ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) ) {

            $defaults = array();
            foreach ( $attribute_taxonomies as $key=>$tax ) {
                $name = wc_attribute_taxonomy_name( $tax->attribute_name );
                $value= get_post_meta( $post->ID , '_product_attributes');
                $defaults[ $name ] = array (
                    'name' => $name,
                    'position' => $key+1,
                    'is_visible' => 1,
                    'is_variation' => 1,
                    'is_taxonomy' => 1,
                );
                update_post_meta( $post->ID , '_product_attributes', $defaults );
            }
        }
    }
}

In addition (if needed, but I am not sure) you could try to use wp_loaded hook to trigger transition_post_status once, because this hook is fired once WordPress, all plugins, and the theme are fully loaded and instantiated. It can be done this way:

另外(如果需要,但我不确定)你可以尝试使用wp_loaded钩子来触发transition_post_status一次,因为一旦WordPress,所有插件和主题被完全加载和实例化,就会触发此钩子。它可以这样做:

if( function_exists( 'wpa_120062_new_product' ) {
    add_action( 'wp_loaded', 'my_wp_is_loaded' );
    function my_wp_is_loaded(){
        do_action ( 'transition_post_status' );
    }
}