Home Dokanmultivendor How to allow vendors to create brands on their dashboard while adding products in Dokan?

How to allow vendors to create brands on their dashboard while adding products in Dokan?

by therichpost
0 comments
How to allow vendors to create brands on their dashboard while adding products in Dokan?

Hello guys how are you? Welcome back to my blog therichpost.com. Guys today in this post, I will tell you How to allow vendors to create brands on their dashboard while adding products in Dokan?

Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks
  4. Dokan

Here is the working steps and please follow carefully:

To allow vendors to create brands directly from their dashboard while adding products in Dokan Lite and Dokan Pro, you can implement the following steps. This approach involves enabling and integrating a “brand taxonomy” that vendors can interact with.


Step 1: Install a WooCommerce Brand Plugin

  1. Use a Plugin to Add Brand Functionality:
  • Install a plugin like Perfect Brands for WooCommerce or WooCommerce Brands. These plugins create a “Brand” taxonomy that can be associated with products.
  1. Verify Compatibility:
  • Ensure the brand plugin integrates with Dokan and is available for vendors to use.

Step 2: Enable Brand Taxonomy for Vendors

After setting up the brand taxonomy, vendors must be able to assign or create brands while adding products.

  1. Check Dokan Pro Settings:
  • Go to Dokan > Settings > Selling Options.
  • Ensure that Enable Product Add/Edit for vendors is enabled.
  1. Enable Vendor Access to Taxonomies:
  • If the brand plugin does not automatically show the “Brand” field in the vendor dashboard, you’ll need to enable or customize this functionality using the following code in your theme’s functions.php file:
   // Add brand taxonomy to Dokan product form
   add_filter('dokan_product_taxonomies', 'add_brand_taxonomy_to_dokan', 10, 1);

   function add_brand_taxonomy_to_dokan($taxonomies) {
       $taxonomies[] = 'pa_brand'; // Replace 'pa_brand' with your brand taxonomy slug
       return $taxonomies;
   }

This allows the “Brand” taxonomy to appear in the product creation form for vendors.


Step 3: Allow Vendors to Create Brands

To allow vendors to create new brands from their dashboard:

  1. Enable Add-New Option for Taxonomy:
  • Use the following custom code to let vendors create new terms (brands) directly from their product edit/add page:
   // Allow vendors to add new brands in product creation
   add_action('dokan_new_product_added', 'allow_vendor_to_create_brand', 10, 2);

   function allow_vendor_to_create_brand($product_id, $data) {
       if (isset($data['pa_brand'])) {
           $brand_name = sanitize_text_field($data['pa_brand']);

           // Check if the brand already exists
           if (!term_exists($brand_name, 'pa_brand')) {
               wp_insert_term($brand_name, 'pa_brand');
           }
       }
   }
  • Replace 'pa_brand' with the actual taxonomy slug of your brand if it’s different.
  1. Dynamic Dropdown in Product Form:
    Ensure that the brand field on the vendor product form allows a dropdown for existing brands and a text box for new brands. You might need to customize the product template. Copy the Dokan product template file (templates/products/new-product.php) to your child theme and modify it to include the brand creation field.

Step 4: Add Brand Field to Product Edit Page

If the brand field doesn’t appear on the product edit page for vendors by default, follow these steps:

  1. Customize Dokan Product Edit Form:
  • Add the following code to your child theme’s functions.php file:
   // Display brand field in Dokan product edit form
   add_action('dokan_product_edit_after_pricing', 'dokan_add_brand_field');

   function dokan_add_brand_field($post_id) {
       $product = wc_get_product($post_id);
       $brand = get_the_terms($post_id, 'pa_brand');

       ?>
       <div class="dokan-form-group">
           <label for="product_brand" class="form-label"><?php esc_html_e('Brand', 'dokan'); ?></label>
           <select name="pa_brand" class="dokan-form-control">
               <?php
               // Get all available brands
               $terms = get_terms(['taxonomy' => 'pa_brand', 'hide_empty' => false]);

               foreach ($terms as $term) {
                   echo sprintf(
                       '<option value="%s" %s>%s</option>',
                       esc_attr($term->slug),
                       selected(isset($brand[0]) && $brand[0]->term_id == $term->term_id, true, false),
                       esc_html($term->name)
                   );
               }
               ?>
           </select>
       </div>
       <?php
   }
  1. Save Brand on Product Update:
  • Hook into Dokan’s product update action to save the selected or newly created brand:
   add_action('dokan_process_product_meta', 'save_product_brand_meta', 10, 2);

   function save_product_brand_meta($post_id, $data) {
       if (isset($data['pa_brand'])) {
           wp_set_object_terms($post_id, $data['pa_brand'], 'pa_brand');
       }
   }
How to allow vendors to create brands on their dashboard while adding products in Dokan?
How to allow vendors to create brands on their dashboard while adding products in Dokan?

Step 5: Test Vendor Dashboard

  1. Log in as a vendor and try adding a product.
  2. Verify the following:
  • The “Brand” dropdown appears and lists existing brands.
  • Vendors can create a new brand if the desired brand doesn’t exist.

Step 6: Optional: Enhance with Plugins

If you want to streamline this process without coding, consider plugins like:

  • Dokan Vendor Add-ons: Some add-ons allow vendors to add more taxonomy-related fields.
  • Custom Product Tabs: Use these to add extra fields for vendors.

Let me know if you’d like help implementing any of these steps! 😊 Feel free to comment below.

Ajay

Thanks

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.