-
Notifications
You must be signed in to change notification settings - Fork 64
/
add-global-trade-identification-numbers-gtins-to-woocommerce-products.code-snippets.xml
66 lines (66 loc) · 2.86 KB
/
add-global-trade-identification-numbers-gtins-to-woocommerce-products.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
58
59
60
61
62
63
64
65
66
<?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.4" created="2018-01-05 17:34" -->
<snippets>
<snippet scope="1">
<name>Add Global Trade Identification Numbers (GTINs) to WooCommerce products</name>
<desc></desc>
<tags>gtin, woocommerce, field, custom, upc, ean, isbn</tags>
<code>/**
* Render the Global Trade Identification Number (GTIN) meta field.
*/
function woocommerce_render_gtin_field() {
$input = array(
'id' => '_gtin',
'label' => sprintf(
'<abbr title="%1$s">%2$s</abbr>',
_x( 'Global Trade Identification Number', 'field label', 'my-theme' ),
_x( 'GTIN', 'abbreviated field label', 'my-theme' )
),
'value' => get_post_meta( get_the_ID(), '_gtin', true ),
'desc_tip' => true,
'description' => __( 'Enter the Global Trade Identification Number (UPC, EAN, ISBN, etc.)', 'my-theme' ),
);
?>
<div id="gtin_attr" class="options_group">
<?php woocommerce_wp_text_input( $input ); ?>
</div>
<?php
}
add_action( 'woocommerce_product_options_inventory_product_data', 'woocommerce_render_gtin_field' );
/**
* Save the product's GTIN number, if provided.
*
* @param int $product_id The ID of the product being saved.
*/
function woocommerce_save_gtin_field( $product_id ) {
if (
! isset( $_POST['_gtin'], $_POST['woocommerce_meta_nonce'] )
|| ( defined( 'DOING_AJAX' ) && DOING_AJAX )
|| ! current_user_can( 'edit_products' )
|| ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' )
) {
return;
}
$gtin = sanitize_text_field( $_POST['_gtin'] );
update_post_meta( $product_id, '_gtin', $gtin );
}
add_action( 'woocommerce_process_product_meta','woocommerce_save_gtin_field' );</code>
</snippet>
</snippets>