How to implement pickup points in Dokan Multi-Vendor WooCommerce using custom code?

How to implement pickup points in Dokan Multi-Vendor WooCommerce using custom code?

Hello guys how are you? Welcome back to my blog therichpost.com. Guys today in this post, I will tell you How to implement pickup points in Dokan Multi-Vendor WooCommerce using custom code?

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:

If you want to implement pickup points in Dokan Multi-Vendor WooCommerce using custom code, follow this approach:


1️⃣ Add Pickup Points as a Custom Shipping Method

Dokan allows vendors to set up their own shipping methods. We’ll create a custom pickup point shipping method.

Step 1: Create the Pickup Point Shipping Method

Add this code to your theme’s functions.php file:

add_filter('woocommerce_shipping_methods', 'custom_add_pickup_point_method');

function custom_add_pickup_point_method($methods) {
    $methods['custom_pickup_point'] = 'WC_Custom_Pickup_Point_Shipping';
    return $methods;
}

class WC_Custom_Pickup_Point_Shipping extends WC_Shipping_Method {
    
    public function __construct($instance_id = 0) {
        $this->id                 = 'custom_pickup_point';
        $this->instance_id        = absint($instance_id);
        $this->method_title       = __('Pickup Point', 'woocommerce');
        $this->method_description = __('Allow customers to pick up from a designated point', 'woocommerce');
        $this->enabled            = "yes";
        $this->title              = __('Pickup Point', 'woocommerce');
        $this->supports           = array('shipping-zones', 'instance-settings', 'instance-settings-modal');
        $this->init();
    }

    public function init() {
        $this->instance_form_fields = array(
            'title' => array(
                'title'       => __('Title', 'woocommerce'),
                'type'        => 'text',
                'description' => __('Title to display during checkout.', 'woocommerce'),
                'default'     => __('Pickup Point', 'woocommerce'),
            ),
        );
    }

    public function calculate_shipping($package = array()) {
        $rate = array(
            'label'   => $this->title,
            'cost'    => 0,
            'package' => $package,
        );
        $this->add_rate($rate);
    }
}

2️⃣ Allow Vendors to Set Their Pickup Points

Since Dokan vendors need a way to define their pickup locations, add custom fields in their store settings.

Step 2: Add Custom Fields in Vendor Dashboard

Add this code to allow vendors to enter pickup locations:

add_action('dokan_settings_form_bottom', 'custom_dokan_pickup_point_field', 10, 2);

function custom_dokan_pickup_point_field($current_user, $profile_info) {
    $pickup_points = isset($profile_info['pickup_points']) ? $profile_info['pickup_points'] : '';
    ?>
    <div class="dokan-form-group">
        <label for="pickup_points"><?php esc_html_e('Pickup Points', 'dokan'); ?></label>
        <textarea name="pickup_points" id="pickup_points" class="dokan-form-control"><?php echo esc_textarea($pickup_points); ?></textarea>
        <p class="help"><?php esc_html_e('Enter pickup points, one per line.', 'dokan'); ?></p>
    </div>
    <?php
}

add_action('dokan_store_profile_saved', 'custom_dokan_save_pickup_point_field', 10, 2);

function custom_dokan_save_pickup_point_field($store_id, $dokan_settings) {
    if (isset($_POST['pickup_points'])) {
        $dokan_settings['pickup_points'] = sanitize_textarea_field($_POST['pickup_points']);
    }
    update_user_meta($store_id, 'dokan_profile_settings', $dokan_settings);
}

3️⃣ Show Pickup Points at Checkout

We need to modify the checkout page to display vendor-specific pickup points.

Step 3: Modify Checkout Fields

Add this code to display pickup locations at checkout:

add_filter('woocommerce_checkout_fields', 'custom_add_pickup_point_checkout_field');

function custom_add_pickup_point_checkout_field($fields) {
    $fields['shipping']['pickup_point'] = array(
        'type'     => 'select',
        'label'    => __('Select Pickup Point', 'woocommerce'),
        'options'  => custom_get_vendor_pickup_points(),
        'required' => true,
    );
    return $fields;
}

function custom_get_vendor_pickup_points() {
    $pickup_options = array();
    $cart_items = WC()->cart->get_cart();
    
    foreach ($cart_items as $item) {
        $vendor_id = get_post_field('post_author', $item['product_id']);
        $vendor_settings = get_user_meta($vendor_id, 'dokan_profile_settings', true);
        
        if (!empty($vendor_settings['pickup_points'])) {
            $points = explode("\n", $vendor_settings['pickup_points']);
            foreach ($points as $point) {
                $pickup_options[trim($point)] = trim($point);
            }
        }
    }

    return $pickup_options;
}

4️⃣ Display Pickup Point in Order Details

Once an order is placed, we need to store and display the pickup point in the order.

Step 4: Save and Show Pickup Point in Order Details

add_action('woocommerce_checkout_update_order_meta', 'custom_save_pickup_point_order_meta');

function custom_save_pickup_point_order_meta($order_id) {
    if (!empty($_POST['pickup_point'])) {
        update_post_meta($order_id, '_pickup_point', sanitize_text_field($_POST['pickup_point']));
    }
}

add_action('woocommerce_admin_order_data_after_shipping_address', 'custom_display_pickup_point_admin_order');

function custom_display_pickup_point_admin_order($order) {
    $pickup_point = get_post_meta($order->get_id(), '_pickup_point', true);
    if ($pickup_point) {
        echo '<p><strong>' . __('Pickup Point:', 'woocommerce') . '</strong> ' . esc_html($pickup_point) . '</p>';
    }
}

5️⃣ Notify Customers About Pickup Point

We should also include the pickup point in the customer email.

Step 5: Add Pickup Point to Emails

add_action('woocommerce_email_order_meta', 'custom_add_pickup_point_to_email', 10, 4);

function custom_add_pickup_point_to_email($order, $sent_to_admin, $plain_text, $email) {
    $pickup_point = get_post_meta($order->get_id(), '_pickup_point', true);
    if ($pickup_point) {
        echo '<p><strong>' . __('Pickup Point:', 'woocommerce') . '</strong> ' . esc_html($pickup_point) . '</p>';
    }
}

Final Steps

Vendors can set pickup points in their Dokan dashboard
Customers can select a pickup point at checkout
Orders will save the pickup point and show it in the admin panel
Emails will include the selected pickup point


🎯 Next Steps

  • Enhance UI: Add a Google Maps API for better pickup point selection.
  • Vendor-Specific Pickup Points: Allow vendors to set different pickup points per product.
  • Order Status Update: Notify customers when the order is ready for pickup.

Guys if you have query then feel free to comment below.

Ajay

Thanks