samedi 10 août 2019

Add a custom checkbox to woocommerce quickedit in admin panel

I tried to create a checkbox in the product edit site in woocommerce, wich allows me to enable/disable a custom badge banner showed only on specific products. I got it working in the normal edit page, but also want to show the checkbox in the product quickedit panel.

I got it working in the product edit page with the code showing below. This was pretty simple as it was working out of the box.

/* 1. Add new checkbox to product edit page (General tab) */
add_action('woocommerce_product_options_general_product_data', 'bbloomer_add_badge_checkbox_to_products');
function bbloomer_add_badge_checkbox_to_products() {
    woocommerce_wp_checkbox(array(
        'id' => 'custom_badge',
        'class' => '',
        'label' => 'Echt SPECHTWERK'
            )
    );
}
/* 2. Save checkbox via custom field */
add_action('save_post', 'bbloomer_save_badge_checkbox_to_post_meta');
function bbloomer_save_badge_checkbox_to_post_meta($product_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    if (isset($_POST['custom_badge'])) {
        update_post_meta($product_id, 'custom_badge', $_POST['custom_badge']);
    } else
        delete_post_meta($product_id, 'custom_badge');
}
// 3. Display badge @ single product page if checkbox checked
add_action('woocommerce_single_product_summary', 'bbloomer_display_badge_if_checkbox', 6);
function bbloomer_display_badge_if_checkbox() {
    global $product;
    if (get_post_meta($product->get_id(), 'custom_badge', true)) {
        ?> <div class="echt-spechtwerk-badge">
                    <img class="advantages-symbols" src="<?php echo get_bloginfo('wpurl') . '/wp-content/uploads/echt-SPECHTWERK-V6.svg' ?>">                    
                </div>
                <?php
    }
}







Aucun commentaire:

Enregistrer un commentaire