Categories

Sunday, December 22, 2024
#919814419350 therichposts@gmail.com
DokanmultivendorWoocommerce

How To automatically add WooCommerce attributes in Dokan?

How To automatically add WooCommerce attributes 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 automatically add WooCommerce attributes 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

To automatically add WooCommerce attributes in Dokan, you can achieve this by programmatically associating attributes with products when they are created or updated by vendors. Here’s how you can do it:

Steps to Automatically Add WooCommerce Attributes in Dokan

1. Use the woocommerce_new_product or woocommerce_update_product Hook

These hooks are triggered whenever a product is created or updated.

2. Add Attributes Programmatically

Use WooCommerce functions to assign attributes to the product.

Add the following code to your theme’s functions.php file or a custom plugin:

function add_woocommerce_attributes_to_dokan_products( $product_id ) {
    // Get the product object
    $product = wc_get_product( $product_id );

    // Check if the product is being added or updated by a Dokan vendor
    if ( dokan_is_seller_product( $product_id ) ) {

        // Define the attributes
        $attributes = array(
            'pa_color' => array( 'red', 'blue', 'green' ), // Replace 'pa_color' with your attribute slug and values
            'pa_size'  => array( 'small', 'medium', 'large' ), // Replace 'pa_size' with your attribute slug and values
        );

        // Prepare the product attributes
        $product_attributes = array();

        foreach ( $attributes as $attribute_slug => $terms ) {
            // Get the attribute ID and check if it exists
            $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_slug );
            if ( ! $attribute_id ) {
                continue; // Skip if attribute does not exist
            }

            $product_attributes[ $attribute_slug ] = array(
                'name'         => 'pa_' . $attribute_slug,
                'value'        => implode( '|', $terms ),
                'position'     => 0,
                'is_visible'   => 1,
                'is_variation' => 1,
                'is_taxonomy'  => 1,
            );

            // Add terms to the attribute
            foreach ( $terms as $term ) {
                if ( ! term_exists( $term, 'pa_' . $attribute_slug ) ) {
                    wp_insert_term( $term, 'pa_' . $attribute_slug );
                }
            }
        }

        // Set the attributes for the product
        $product->set_attributes( $product_attributes );
        $product->save();
    }
}
add_action( 'woocommerce_new_product', 'add_woocommerce_attributes_to_dokan_products' );
add_action( 'woocommerce_update_product', 'add_woocommerce_attributes_to_dokan_products' );

Explanation of the Code

  1. Hook into Product Creation/Update:
    • woocommerce_new_product triggers when a new product is created.
    • woocommerce_update_product triggers when an existing product is updated.
  2. Check Vendor’s Product:
    • dokan_is_seller_product() ensures the code runs only for products created or updated by Dokan vendors.
  3. Define and Add Attributes:
    • The $attributes array contains attribute slugs and terms.
    • wc_attribute_taxonomy_id_by_name() checks if the attribute exists.
    • wp_insert_term() adds terms to the attribute taxonomy.
  4. Set Product Attributes:
    • Attributes are added to the product using $product->set_attributes().
  5. Save the Product:
    • save() ensures the changes are saved.

Additional Notes

  • Replace pa_color and pa_size with your custom attribute slugs.
  • Ensure the attributes are already created in WooCommerce (from Products > Attributes).
  • Test the code in a staging environment before deploying to a live site.

Let me know if you have any questions or need further assistance!

Guys if you will have any kind of query then feel free to comment below.

Thanks

Jassa

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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