-
Notifications
You must be signed in to change notification settings - Fork 64
/
add-a-upc-and-ean-meta-fields-to-variations-in-woocommerce.code-snippets.xml
57 lines (57 loc) · 2.65 KB
/
add-a-upc-and-ean-meta-fields-to-variations-in-woocommerce.code-snippets.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a code snippets export file generated by the Code Snippets WordPress plugin. -->
<!-- https://wordpress.org/plugins/code-snippets -->
<!-- To import these snippets a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Install the Code Snippets plugin using the directions provided at the above link. -->
<!-- 3. Go to 'Tools: Import' in the WordPress admin panel. -->
<!-- 4. Click on the "Code Snippets" importer in the list -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. Code Snippets will then import all of the snippets and associated information contained in this file into your site. -->
<!-- 7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets. -->
<!-- generator="Code Snippets/2.9.2" created="2017-09-07 19:37" -->
<snippets>
<snippet scope="0">
<name>Add a UPC and EAN meta fields to variations in WooCommerce</name>
<desc></desc>
<tags>upc, ean, meta, field, woocommerce, product, variations</tags>
<code>add_action( 'woocommerce_product_after_variable_attributes', 'jb_woo_variation_add_upc_and_ean_fields', 10, 3);
function jb_woo_variation_add_upc_and_ean_fields($loop, $variation_data, $variation) {
woocommerce_wp_text_input( array(
'id' => '_upc[' . $variation->ID . ']',
'label' => 'UPC',
'description' => '',
'desc_tip' => 'false',
'value' => get_post_meta( $variation->ID, '_upc', true ),
'placeholder' => '',
'wrapper_class' => 'form-row form-row-first',
'type' => 'text'
));
woocommerce_wp_text_input( array(
'id' => '_ean[' . $variation->ID . ']',
'label' => 'EAN',
'description' => '',
'desc_tip' => 'false',
'value' => get_post_meta( $variation->ID, '_ean', true ),
'placeholder' => '',
'wrapper_class' => 'form-row form-row-last',
'type' => 'text'
));
}
//save the new meta data to the variations
add_action( 'woocommerce_save_product_variation', 'jb_woo_add_unit_size_field_save' );
function jb_woo_add_unit_size_field_save($post_id){
$upc = $_POST['_upc'][ $post_id ];
if ( ! empty( $_POST['_upc'] ) ) {
update_post_meta( $post_id, '_upc', esc_attr( $upc ) );
}
$ean = $_POST['_ean'][ $post_id ];
if ( ! empty( $_POST['_ean'] ) ) {
update_post_meta( $post_id, '_ean', esc_attr( $ean ) );
}
}</code>
</snippet>
</snippets>