Skip to content

Instantly share code, notes, and snippets.

@zakirsajib
Created April 20, 2022 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakirsajib/6ea77f7877ed293e6fe9aed905e05233 to your computer and use it in GitHub Desktop.
Save zakirsajib/6ea77f7877ed293e6fe9aed905e05233 to your computer and use it in GitHub Desktop.
Adding vendor signup option in checkout page (Dokan multivendor, WooCommerce)
/*
* Adding vendor signup option in checkout page (Dokan multivendor, WooCommerce).
* In checkout page, we are providing custom fields, so if a customer interested to become a vendor, they must fill out his
* store name and store url.
*
* These 2 fields will be saved into User's database and integrated to Dokan framework.
*/
add_action('woocommerce_after_checkout_billing_form', 'fluf_vendor_registration_checkout');
function fluf_vendor_registration_checkout($checkout) {
woocommerce_form_field('custom_field_name_one', array(
'type' => 'checkbox',
'class' => array(
'vendor-choice-checkout form-row-wide'
) ,
'label' => __('I\'d like to sell clothes free of charge') ,
),
$checkout->get_value('custom_field_name_one'));
echo '<div class="store-fields">';
woocommerce_form_field('store_name', array(
'type' => 'text',
'required' => true,
'class' => array(
'store-name-checkout form-row-wide'
) ,
'label' => __('Store Name') ,
),
$checkout->get_value('store_name'));
woocommerce_form_field('seller_url', array(
'type' => 'text',
'required' => true,
'class' => array(
'store-url-checkout form-row-wide'
) ,
'label' => __('Store URL') ,
),
$checkout->get_value('seller_url'));
echo '</div>';
}
add_action( 'woocommerce_checkout_process', 'fluf_validate_new_checkout_field' );
function fluf_validate_new_checkout_field() {
if ( ! $_POST['store_name'] ) {
wc_add_notice( 'Please enter your Store Name', 'error' );
}
if ( ! $_POST['seller_url'] ) {
wc_add_notice( 'Please enter your Store URL', 'error' );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'fluf_save_new_checkout_field' );
function fluf_save_new_checkout_field( $order_id ) {
if ( $_POST['store_name'] ) update_post_meta( $order_id, '_store_name', esc_attr( $_POST['store_name'] ) );
if ( $_POST['seller_url'] ) update_post_meta( $order_id, '_seller_url', esc_attr( $_POST['seller_url'] ) );
}
add_action( 'woocommerce_thankyou', 'fluf_change_role_on_purchase' );
function fluf_change_role_on_purchase( $order_id ) {
// get order object and items
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_seller_url', true ) ) echo '<p><strong>seller url:</strong> ' . get_post_meta( $order_id, '_seller_url', true ) . '</p>';
if ( get_post_meta( $order_id, '_store_name', true ) ) echo '<p><strong>store name:</strong> ' . get_post_meta( $order_id, '_store_name', true ) . '</p>';
$seller_url = get_post_meta( $order_id, '_seller_url', true );
$store_name = get_post_meta( $order_id, '_store_name', true );
$data = array();
if( isset( $seller_url ) ) {
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if( $product_id == $item['product_id'] && $order->user_id ) {
$user = new WP_User( $order->user_id );
// Remove old role
//$user->remove_role( 'customer' );
$data['shopurl'] = $seller_url;
$data['shopname'] = $store_name;
// Add new role
$user->add_role( 'seller' );
dokan_user_update_to_seller($user, $data);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment