Woocommerce for Brands that Includes Logo / Image, Name of Company/Brand, URL

Home » Snippets » Woocommerce for Brands that Includes Logo / Image, Name of Company/Brand, URL
0

Created with:

Visibility: 

public

Creator: Ridge Lawrence

Customize With AI
					<?php
// Register the taxonomy
function wpturbo_register_brands_taxonomy() {
    $labels = array(
        'name'              => __( 'Brands', 'wpturbo' ),
        'singular_name'     => __( 'Brand', 'wpturbo' ),
        'search_items'      => __( 'Search Brands', 'wpturbo' ),
        'all_items'         => __( 'All Brands', 'wpturbo' ),
        'parent_item'       => __( 'Parent Brand', 'wpturbo' ),
        'parent_item_colon' => __( 'Parent Brand:', 'wpturbo' ),
        'edit_item'         => __( 'Edit Brand', 'wpturbo' ),
        'update_item'       => __( 'Update Brand', 'wpturbo' ),
        'add_new_item'      => __( 'Add New Brand', 'wpturbo' ),
        'new_item_name'     => __( 'New Brand Name', 'wpturbo' ),
        'menu_name'         => __( 'Brands', 'wpturbo' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'brands' ),
        'meta_box_cb' => 'wpturbo_brand_meta_box',
    );
    register_taxonomy( 'product_brand', 'product', $args );
}

add_action( 'init', 'wpturbo_register_brands_taxonomy' );

// Add the meta box for additional fields
function wpturbo_brand_meta_box() {
    add_meta_box( 'wpturbo_brand_meta_box', __( 'Brand Details', 'wpturbo' ), 'wpturbo_brand_meta_box_callback', 'product_brand' );
}
add_filter( 'manage_edit-product_brand_columns', 'wpturbo_brand_columns' );
function wpturbo_brand_columns( $columns ) {
    $columns['brand_logo'] = __( 'Logo', 'wpturbo' );
    return $columns;
}
    // Callback function for the meta box
function wpturbo_brand_meta_box_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'wpturbo_brand_meta_box_nonce' );
    $brand_logo = get_term_meta( $post->ID, 'brand_logo', true );
    $brand_url = get_term_meta( $post->ID, 'brand_url', true );
    $brand_name = get_term_meta( $post->ID, 'brand_name', true );
    ?>
    <div class="form-field">
        <label for="brand_name"><?php _e( 'Name', 'wpturbo' ); ?></label>
        <input type="text" name="brand_name" id="brand_name" value="<?php echo esc_attr( $brand_name ); ?>">
    </div>
    <div class="form-field">
        <label for="brand_url"><?php _e( 'URL', 'wpturbo' ); ?></label>
        <input type="text" name="brand_url" id="brand_url" value="<?php echo esc_attr( $brand_url ); ?>">
    </div>
    <div class="form-field">
        <label for="brand_logo"><?php _e( 'Logo', 'wpturbo' ); ?></label>
        <?php if ( $brand_logo ) : ?>
            <br>
            <img src="<?php echo esc_url( $brand_logo ); ?>" alt="<?php echo esc_attr( $brand_name ); ?>" style="max-width: 100%;">
        <?php endif; ?>
        <br>
        <input type="text" name="brand_logo" id="brand_logo" value="<?php echo esc_attr( $brand_logo ); ?>">
        <button class="button" id="upload_logo_button"><?php _e( 'Upload', 'wpturbo' ); ?></button>
        <script>
            jQuery(document).ready(function($) {
                $('#upload_logo_button').click(function() {
                    var customUploader = wp.media({
                        title: '<?php _e( 'Upload Logo', 'wpturbo' ); ?>',
                        button: {
                            text: '<?php _e( 'Use as Logo', 'wpturbo' ); ?>'
                        },
                        multiple: false
                    })
                    .on('select', function() {
                        var attachment = customUploader.state().get('selection').first().toJSON();
                        $('#brand_logo').val(attachment.url);
                    })
                    .open();
                });
            });
        </script>
    </div>
    <?php
}

// Save the additional fields
add_action( 'edited_product_brand', 'wpturbo_save_brand_meta', 10, 2 );
add_action( 'create_product_brand', 'wpturbo_save_brand_meta', 10, 2 );
function wpturbo_save_brand_meta( $term_id, $tt_id ) {
    if ( isset( $_POST['brand_name'] ) ) {
        $brand_name = sanitize_text_field( $_POST['brand_name'] );
        update_term_meta( $term_id, 'brand_name', $brand_name );
    }
    if ( isset( $_POST['brand_url'] ) ) {
        $brand_url = sanitize_text_field( $_POST['brand_url'] );
        update_term_meta( $term_id, 'brand_url', $brand_url );
    }
    if ( isset( $_POST['brand_logo'] ) ) {
        $brand_logo = sanitize_text_field( $_POST['brand_logo'] );
        update_term_meta( $term_id, 'brand_logo', $brand_logo );
    }
}

				

This code snippet includes everything you need to create a "Brands" taxonomy for WooCommerce products.

After adding this code to your functions.php file, a new taxonomy "Brands" will be available in your WordPress admin dashboard. You can add brands just as you would any other taxonomy, but in addition to the name, you'll also be prompted to upload an image/logo, specify the name of the company/brand and add a URL.

Depending on how your theme is set up, you may need to modify the templates that display product information to include the new brand field.

Note that this code includes image uploading functionality that uses WordPress' media uploader, and validates the user input to sanitize any user-supplied data. Also, it includes a prefix in all function names as specified in the best practices section.

Register an account to save your snippets or go Pro to get more features.