Custom field in variations of WooCommerce

I am trying to find a solution for getting user-defined field change data in an interface. I am not sure how to get the change identifier when changing the change product list window. For example, a variable product as color contains red, blue, green. I added a custom field for the entire color combination.

Here is the code I tried ( source )

add_action('woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2); //JS to add fields for new variations add_action('woocommerce_product_after_variable_attributes_js', 'variable_fields_js'); //Save variation fields add_action('woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1); /** * Create new fields for variations * */ function variable_fields($loop, $variation_data) { ?> <tr> <td> <?php // Select woocommerce_wp_select( array( 'id' => '_select[' . $loop . ']', 'label' => __('My Select Field', 'woocommerce'), 'description' => __('Choose a value.', 'woocommerce'), 'value' => $variation_data['_select'][0], 'options' => array( 'one' => __('Option 1', 'woocommerce'), 'two' => __('Option 2', 'woocommerce'), 'three' => __('Option 3', 'woocommerce') ) ) ); ?> </td> </tr> <?php } /** * Create new fields for new variations * */ function variable_fields_js() { ?> <tr> <td> <?php // Select woocommerce_wp_select( array( 'id' => '_select[ + loop + ]', 'label' => __('My Select Field', 'woocommerce'), 'description' => __('Choose a value.', 'woocommerce'), 'value' => $variation_data['_select'][0], 'options' => array( 'one' => __('Option 1', 'woocommerce'), 'two' => __('Option 2', 'woocommerce'), 'three' => __('Option 3', 'woocommerce') ) ) ); ?> </td> </tr> <?php } /** * Save new fields for variations * */ function save_variable_fields($post_id) { if (isset($_POST['variable_sku'])) : $variable_sku = $_POST['variable_sku']; $variable_post_id = $_POST['variable_post_id']; // Text Field $_select = $_POST['_select']; for ($i = 0; $i < sizeof($variable_sku); $i++) : $variation_id = (int) $variable_post_id[$i]; if (isset($_select[$i])) { update_post_meta($variation_id, '_select', stripslashes($_select[$i])); } endfor; // Checkbox endif; } 

All this is stored correctly, but in the interface I can not get the value of the corresponding option, but I still find a solution. Like the front end, if you change the variation, changing the price at the same time. I want to show other information on top of the price variation. This can be done in woocommerce. Here is a screenshot.

Variable Content Product backend variable product

The expected result in the front enter image description here

How to show custom field data when changing a list?

+3
source share
1 answer

brasofilo,

I wrote a tutorial / example on this issue:

http://blueskysessions.com/2014/03/31/woocommerce-display-dynamic-content-per-the-selected-product-variation/

I had the same question and figured out how to do this. It is not as easy as one might think. It takes a bit of everything, but ultimately the trick is to use jQuery to show and hide content.

+2
source

Source: https://habr.com/ru/post/1269975/


All Articles