Created
May 25, 2020 20:45
-
-
Save yanknudtskov/321349662671eccef51a96a02dcb6223 to your computer and use it in GitHub Desktop.
WooCommerce Add Custom Fields to Products
This file contains 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
<?php | |
// For variations | |
add_action( 'woocommerce_variation_options_pricing', 'yanco_add_custom_field_to_variations', 10, 3 ); | |
function yanco_add_custom_field_to_variations( $loop, $variation_data, $variation ) { | |
woocommerce_wp_text_input( array( | |
'id' => 'custom_field[' . $loop . ']', | |
'class' => 'short', | |
'label' => __( 'Custom Field', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'custom_field', true ) | |
) | |
); | |
} | |
add_action( 'woocommerce_save_product_variation', 'yanco_save_custom_field_variations', 10, 2 ); | |
function yanco_save_custom_field_variations( $variation_id, $i ) { | |
$custom_field = $_POST['custom_field'][$i]; | |
if ( isset( $custom_field ) ) { | |
update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) ); | |
} | |
} | |
add_filter( 'woocommerce_available_variation', 'yanco_add_custom_field_variation_data' ); | |
function yanco_add_custom_field_variation_data( $variations ) { | |
$variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>'; | |
return $variations; | |
} | |
// For Singular Product | |
add_action( 'woocommerce_product_options_pricing', 'yanco_add_custom_field_to_products' ); | |
function yanco_add_custom_field_to_products() { | |
woocommerce_wp_text_input( array( | |
'id' => 'custom-field-id', | |
'class' => 'short wc_input_price', | |
'label' => __( 'Custom Field', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')', | |
'data_type' => 'price', | |
)); | |
} | |
add_action( 'save_post_product', 'yanco_save_custom_field' ); | |
function yanco_save_custom_field( $product_id ) { | |
global $pagenow, $typenow; | |
if ( 'post.php' !== $pagenow || 'product' !== $typenow ) { | |
return; | |
} | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
if ( isset( $_POST['custom-field-id'] ) ) { | |
if ( $_POST['custom-field-id'] ) { | |
update_post_meta( $product_id, 'custom-field-meta-key', $_POST['custom-field-id'] ); | |
} | |
} else { | |
delete_post_meta( $product_id, 'custom-field-meta-key' ); | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'yanco_display_custom_field', 9 ); | |
function yanco_display_custom_field() { | |
global $product; | |
if ( $product->get_type() <> 'variable' && $custom_field_value = get_post_meta( $product->get_id(), 'custom-field-meta-key', true ) ) { | |
echo '<div class="woocommerce_custom_field">'; | |
_e( 'Custom Field: ', 'woocommerce' ); | |
echo '<span>' . wc_price( $custom_field_value ) . '</span>'; | |
echo '</div>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment