|
| 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>/** |
| 19 | + * Render the Global Trade Identification Number (GTIN) meta field. |
| 20 | + */ |
| 21 | +function woocommerce_render_gtin_field() { |
| 22 | + $input = array( |
| 23 | + 'id' => '_gtin', |
| 24 | + 'label' => sprintf( |
| 25 | + '<abbr title="%1$s">%2$s</abbr>', |
| 26 | + _x( 'Global Trade Identification Number', 'field label', 'my-theme' ), |
| 27 | + _x( 'GTIN', 'abbreviated field label', 'my-theme' ) |
| 28 | + ), |
| 29 | + 'value' => get_post_meta( get_the_ID(), '_gtin', true ), |
| 30 | + 'desc_tip' => true, |
| 31 | + 'description' => __( 'Enter the Global Trade Identification Number (UPC, EAN, ISBN, etc.)', 'my-theme' ), |
| 32 | + ); |
| 33 | +?> |
| 34 | + |
| 35 | + <div id="gtin_attr" class="options_group"> |
| 36 | + <?php woocommerce_wp_text_input( $input ); ?> |
| 37 | + </div> |
| 38 | + |
| 39 | +<?php |
| 40 | +} |
| 41 | + |
| 42 | +add_action( 'woocommerce_product_options_inventory_product_data', 'woocommerce_render_gtin_field' ); |
| 43 | + |
| 44 | +/** |
| 45 | + * Save the product's GTIN number, if provided. |
| 46 | + * |
| 47 | + * @param int $product_id The ID of the product being saved. |
| 48 | + */ |
| 49 | +function woocommerce_save_gtin_field( $product_id ) { |
| 50 | + if ( |
| 51 | + ! isset( $_POST['_gtin'], $_POST['woocommerce_meta_nonce'] ) |
| 52 | + || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 53 | + || ! current_user_can( 'edit_products' ) |
| 54 | + || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) |
| 55 | + ) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $gtin = sanitize_text_field( $_POST['_gtin'] ); |
| 60 | + |
| 61 | + update_post_meta( $product_id, '_gtin', $gtin ); |
| 62 | +} |
| 63 | + |
| 64 | +add_action( 'woocommerce_process_product_meta','woocommerce_save_gtin_field' );</code> |
| 65 | + </snippet> |
| 66 | +</snippets> |
0 commit comments