WooCommerce : add custom fields to product variations

后端 未结 2 1628
日久生厌
日久生厌 2021-01-21 19:02

I need help adding custom fields to my Product Variations and Simple Product pages. I tried using RemiCorson\'s info (http://www.remicorson.com/woocommerce-custom-fields-for-var

2条回答
  •  一向
    一向 (楼主)
    2021-01-21 19:20

    I have never needed to bother with woocommerce_product_after_variable_attributes_js, you just need to add the input and then deal with saving it.

    Another thing that has changed since Remi's article was published is that the WooCommerce variation meta data is no longer printed in a

    element... and is now a
    element. This is important for how you structure your new content.

    Here's is how you'd add a meta field to a variation:

    // regular variable products
    add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );
    add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );
    
    
    /*
     * Add new inputs to each variation
     *
     * @param string $loop
     * @param array $variation_data
     * @return print HTML
     */
    function add_to_variations_metabox( $loop, $variation_data, $variation ){
    
        $custom = get_post_meta( $variation->ID, '_custom', true ); ?>
    
            

    提交回复
    热议问题