Created
September 20, 2017 16:24
-
-
Save psaikali/922650969249cc17bd7f61481930f189 to your computer and use it in GitHub Desktop.
Ajouter un champ "Code-barre" aux produits WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Ajouter un champ "Code-barre" aux produits WooCommerce | |
* Ce nouveau champ s'affichera, ici, dans l'onglet "Inventaire" dans le back-office d'un produit WooCommerce | |
*/ | |
function msk_add_barcode_product_fields() { ?> | |
<div class="options_group"> | |
<?php | |
// On ajoute un champ input de type "text" | |
// Pour une liste complète des types de champs gérés nativement par WooCommerce : https://mosaika.fr/ajouter-champs-meta-produits-woocommerce/ | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'product_barcode', | |
'label' => __('Code-barre du produit', 'mosaika'), | |
'placeholder' => __('Indiquez le code-barre', 'mosaika'), | |
) | |
); ?> | |
</div> | |
<?php } | |
add_action('woocommerce_product_options_inventory_product_data', 'msk_add_barcode_product_fields'); | |
/** | |
* Enregistrer la valeur du champ "Code-barre" quand on enregistre un produit | |
*/ | |
function msk_save_commission_product_fields_data($product_id, $post, $update) { | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; | |
// | |
if ($post->post_type == 'product') { | |
$product = wc_get_product($product_id); | |
if (isset($_POST['product_barcode'])) { | |
$product_barcode = wc_clean($_POST['product_barcode']); | |
$product->update_meta_data('product_barcode', $product_barcode); | |
} | |
$product->save(); | |
} | |
} | |
add_action('save_post', 'msk_save_commission_product_fields_data', 10, 3); | |
/** | |
* Afficher le code-barre dans un template du thème | |
* Par exemple, on pourrait l'ajouter dans l'onglet "Description" en éditant le templace woocommerce/single-product/tabs/description.php | |
* Les 3 lignes ci-dessous permettent de récupérer le code-barre du produit | |
*/ | |
/* | |
global $product; | |
$product_barcode = $product->get_meta('product_barcode', true); | |
echo $product_barcode; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment