Skip to content

Commit ec12ad2

Browse files
authored
Add Global Trade Identification Numbers (GTINs) to WooCommerce products Add
1 parent e15e256 commit ec12ad2

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- This is a code snippets export file generated by the Code Snippets WordPress plugin. -->
3+
<!-- https://wordpress.org/plugins/code-snippets -->
4+
<!-- To import these snippets a WordPress site follow these steps: -->
5+
<!-- 1. Log in to that site as an administrator. -->
6+
<!-- 2. Install the Code Snippets plugin using the directions provided at the above link. -->
7+
<!-- 3. Go to 'Tools: Import' in the WordPress admin panel. -->
8+
<!-- 4. Click on the "Code Snippets" importer in the list -->
9+
<!-- 5. Upload this file using the form provided on that page. -->
10+
<!-- 6. Code Snippets will then import all of the snippets and associated information contained in this file into your site. -->
11+
<!-- 7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets. -->
12+
<!-- generator="Code Snippets/2.9.4" created="2018-01-05 17:34" -->
13+
<snippets>
14+
<snippet scope="1">
15+
<name>Add Global Trade Identification Numbers (GTINs) to WooCommerce products</name>
16+
<desc></desc>
17+
<tags>gtin, woocommerce, field, custom, upc, ean, isbn</tags>
18+
<code>/**&#13;
19+
* Render the Global Trade Identification Number (GTIN) meta field.&#13;
20+
*/&#13;
21+
function woocommerce_render_gtin_field() {&#13;
22+
$input = array(&#13;
23+
'id' =&gt; '_gtin',&#13;
24+
'label' =&gt; sprintf(&#13;
25+
'&lt;abbr title="%1$s"&gt;%2$s&lt;/abbr&gt;',&#13;
26+
_x( 'Global Trade Identification Number', 'field label', 'my-theme' ),&#13;
27+
_x( 'GTIN', 'abbreviated field label', 'my-theme' )&#13;
28+
),&#13;
29+
'value' =&gt; get_post_meta( get_the_ID(), '_gtin', true ),&#13;
30+
'desc_tip' =&gt; true,&#13;
31+
'description' =&gt; __( 'Enter the Global Trade Identification Number (UPC, EAN, ISBN, etc.)', 'my-theme' ),&#13;
32+
);&#13;
33+
?&gt;&#13;
34+
&#13;
35+
&lt;div id="gtin_attr" class="options_group"&gt;&#13;
36+
&lt;?php woocommerce_wp_text_input( $input ); ?&gt;&#13;
37+
&lt;/div&gt;&#13;
38+
&#13;
39+
&lt;?php&#13;
40+
}&#13;
41+
&#13;
42+
add_action( 'woocommerce_product_options_inventory_product_data', 'woocommerce_render_gtin_field' );&#13;
43+
&#13;
44+
/**&#13;
45+
* Save the product's GTIN number, if provided.&#13;
46+
*&#13;
47+
* @param int $product_id The ID of the product being saved.&#13;
48+
*/&#13;
49+
function woocommerce_save_gtin_field( $product_id ) {&#13;
50+
if (&#13;
51+
! isset( $_POST['_gtin'], $_POST['woocommerce_meta_nonce'] )&#13;
52+
|| ( defined( 'DOING_AJAX' ) &amp;&amp; DOING_AJAX )&#13;
53+
|| ! current_user_can( 'edit_products' )&#13;
54+
|| ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' )&#13;
55+
) {&#13;
56+
return;&#13;
57+
}&#13;
58+
&#13;
59+
$gtin = sanitize_text_field( $_POST['_gtin'] );&#13;
60+
&#13;
61+
update_post_meta( $product_id, '_gtin', $gtin );&#13;
62+
}&#13;
63+
&#13;
64+
add_action( 'woocommerce_process_product_meta','woocommerce_save_gtin_field' );</code>
65+
</snippet>
66+
</snippets>

0 commit comments

Comments
 (0)