Last active
April 17, 2022 01:50
-
-
Save sinebeef/c8af5a9bc10f1ec1bfa633315948346b to your computer and use it in GitHub Desktop.
woocommerce variation custom field, with backend textbox and saving meta, frontend display on change with jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add Variation Settings | |
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 ); | |
// Save Variation Settings | |
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 ); | |
/** | |
* Create new fields for variations | |
* | |
*/ | |
function variation_settings_fields( $loop, $variation_data, $variation ) { | |
// Text Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_text_field[' . $variation->ID . ']', | |
'label' => __( 'My Text Field', 'woocommerce' ), | |
'placeholder' => 'http://', | |
'desc_tip' => 'true', | |
'description' => __( 'Enter the custom value here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, '_text_field', true ) | |
) | |
); | |
} | |
/** | |
* Save new fields for variations | |
* | |
*/ | |
function save_variation_settings_fields( $post_id ) { | |
// Text Field | |
$text_field = $_POST['_text_field'][ $post_id ]; | |
if( ! empty( $text_field ) ) { | |
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) ); | |
} | |
} | |
// Add New Variation Settings | |
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' ); | |
/** | |
* Add custom fields for variations | |
* | |
*/ | |
function load_variation_settings_fields( $variations ) { | |
// duplicate the line for each field | |
$variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true ); | |
return $variations; | |
} | |
/* theme-child/woocommerce/single-product/add-to-cart/variation.php | |
<?php | |
/** | |
* Single variation display | |
* | |
* This is a javascript-based template for single variations (see https://codex.wordpress.org/Javascript_Reference/wp.template). | |
* The values will be dynamically replaced after selecting attributes. | |
* | |
* @see https://docs.woocommerce.com/document/template-structure/ | |
* @package WooCommerce/Templates | |
* @version 2.5.0 | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
?> | |
<script type="text/template" id="tmpl-variation-template"> | |
<div class="woocommerce-variation-custom-text-field">{{{ data.variation.text_field }}}</div> | |
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div> | |
<div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div> | |
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div> | |
</script> | |
<script type="text/template" id="tmpl-unavailable-variation-template"> | |
<p><?php esc_html_e( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ); ?></p> | |
</script> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment