How can I save a custom field of an attribute in Woocommerce?

后端 未结 4 1970
野趣味
野趣味 2021-01-26 07:21

Old

I am trying to create a custom field in the product attributes in Woocommerce. This to be able to select if an attribute is highlighted or not. For example:

4条回答
  •  既然无缘
    2021-01-26 07:42

    @Jesús Magallón's

    Answer need little modifications. Remove the function get_attribute_highlighted() and replace this line

       $value = get_attribute_highlighted($attribute->get_name(), $i);
        
    

    TO

        global $post;
        $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : $post->ID ;
        $value = get_post_meta($post_id , "attribute_description".$i, true);
    

    It will show ajax saved value instance of empty;

    And Change hooks

    add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
    

    TO

    add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 0);
    

提交回复
热议问题