• Resolved mayank29gupta

    (@mayank29gupta)


    I am using the following code to add a custom product badge in woocommerce. It adds a free shipping badge on products page. The problem I am facing lately is that for some reason, the free shipping badge disappears after some time. I am not sure if its because of wordpress update or something else. On the product edit page in admin section, I have noticed that checkbox is also unchecked by itself after some time. Could somebody please help?

    // 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’ => ‘Show Custom Badge’
    )
    );
    }
    
    // —————————————–
    // 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’ );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Product Badge in Woocommerce’ is closed to new replies.