Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Yorlinq/b1ff65121a805fbdbdf4478633122ef8 to your computer and use it in GitHub Desktop.
Save Yorlinq/b1ff65121a805fbdbdf4478633122ef8 to your computer and use it in GitHub Desktop.
Add 'EAN code' field to simple products and product variations - WooCommerce
/*
* Add our Custom Fields to simple products
*/
function yl_woo_add_custom_fields() {
global $woocommerce, $product, $post;
$product = wc_get_product( get_the_id() );
if ($product->is_type( 'simple' )) {
echo '<div class="options_group">';
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_ean_code',
'label' => __( 'EAN code', 'yorlinq' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Enter the official EAN code.", 'yorlinq' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
}
}
// General Tab
//add_action( 'woocommerce_product_options_pricing', 'yl_woo_add_custom_fields' ); // After pricing fields
//add_action( 'woocommerce_product_options_downloads', 'yl_woo_add_custom_fields' ); // After downloadable file fields and only visible when it's a downloable product
//add_action( 'woocommerce_product_options_tax', 'yl_woo_add_custom_fields' ); // After tax fields
//add_action( 'woocommerce_product_options_general_product_data', 'yl_woo_add_custom_fields' ); // After all General default fields
// Inventory tab
add_action( 'woocommerce_product_options_sku', 'yl_woo_add_custom_fields' ); // After SKU field
//add_action( 'woocommerce_product_options_stock', 'yl_woo_add_custom_fields' ); // After Manage Stock field
//add_action( 'woocommerce_product_options_stock_fields', 'yl_woo_add_custom_fields' ); // After Manage Stock field but only visible is checked
//add_action( 'woocommerce_product_options_stock_status', 'yl_woo_add_custom_fields' ); // After Stock Status field
//add_action( 'woocommerce_product_options_sold_individually', 'yl_woo_add_custom_fields' ); // After Sold Individually field
//add_action( 'woocommerce_product_options_inventory_product_data', 'yl_woo_add_custom_fields' ); // After all Inventory default fields
// Shipping tab
//add_action( 'woocommerce_product_options_dimensions', 'yl_woo_add_custom_fields' ); // After Dimensions field
//add_action( 'woocommerce_product_options_shipping', 'yl_woo_add_custom_fields' ); // After all Shipping default fields
// Linked Products tab
//add_action( 'woocommerce_product_options_related', 'yl_woo_add_custom_fields' ); // After all Linked Products default fields
// Attributes tab
//add_action( 'woocommerce_product_options_attributes', 'yl_woo_add_custom_fields' ); // After all Attributes default fields
// Advanced tab
//add_action( 'woocommerce_product_options_reviews', 'yl_woo_add_custom_fields' ); // After Enable Reviews field
//add_action( 'woocommerce_product_options_advanced', 'yl_woo_add_custom_fields' ); // After all Advanced default fields
/*
* Save our simple product fields
*/
function yl_woo_add_custom_fields_save( $post_id ){
global $product;
$product = wc_get_product( get_the_id() );
if ($product->is_type('simple')) {
// Number Field
$woocommerce_ean_code = $_POST['_ean_code'];
update_post_meta( $post_id, '_ean_code', esc_attr( $woocommerce_ean_code ) );
}
}
add_action( 'woocommerce_process_product_meta', 'yl_woo_add_custom_fields_save' );
/*
* Add our Custom Fields to variable products
*/
function yl_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-first">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_ean_code[' . $variation->ID . ']',
'label' => __( 'EAN code', 'yorlinq' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Enter the official EAN code, for each product variation individually.", "yorlinq" ),
'type' => 'number',
'value' => get_post_meta( $variation->ID, '_variable_ean_code', true )
)
);
// Add extra custom fields here as necessary...
echo '</div>';
}
// Variations tab
add_action( 'woocommerce_variation_options', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After variation Enabled/Downloadable/Virtual/Manage Stock checkboxes
//add_action( 'woocommerce_variation_options_pricing', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After Price fields
//add_action( 'woocommerce_variation_options_inventory', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After Manage Stock fields
//add_action( 'woocommerce_variation_options_dimensions', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After Weight/Dimension fields
//add_action( 'woocommerce_variation_options_tax', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After Shipping/Tax Class fields
//add_action( 'woocommerce_variation_options_download', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After Download fields
//add_action( 'woocommerce_product_after_variable_attributes', 'yl_woo_add_custom_variation_fields', 10, 3 ); // After all Variation fields
/*
* Save our variable product fields
*/
function yl_woo_add_custom_variation_fields_save( $post_id ){
// Text Field
$woocommerce_variable_ean_code = $_POST['_variable_ean_code'][ $post_id ];
update_post_meta( $post_id, '_variable_ean_code', esc_attr( $woocommerce_variable_ean_code ) );
}
add_action( 'woocommerce_save_product_variation', 'yl_woo_add_custom_variation_fields_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment