Assign Dokan vendor shipping address to store address in Woocommerce

这一生的挚爱 提交于 2021-01-24 13:22:08

问题


I am using Dokan and Woocommerce plug-in for my wordpress site.

I want all vendors ship their products to store address not customer address. After that I want to ship products from store to their address.

But always shipping address is shown to vendors, is customer address and not mine address. I have disabled vendor shipping in Dokan but still shipping address is customers address and not mine.

How can I change shipping address shown to vendors to my store address? I checked Woocommerce setting and did not find any solution. Should I change some codes or install more plug-ins?

Thanks alot


回答1:


The user role for Dokan vendors is "vendor", so you can target this user role and:

  • In cart and checkout pages, set the shipping address to the shop base
  • On order submit save as shipping address the shop base

The code:

// Utility function that outputs the shop base address in an array
function get_shop_base_address() {
    $country_state = explode( ':', get_option( 'woocommerce_default_country' ) );

    return array(
        'address1'  => get_option( 'woocommerce_store_address' ),
        'address2' => get_option( 'woocommerce_store_address_2' ),
        'city'     => get_option( 'woocommerce_store_city' ),
        'postcode' => get_option( 'woocommerce_store_postcode' ),
        'state'    => $country_state[0],
        'country'  => isset($country_state[1]) ? $country_state[1] : '',
    );
}


// Set vendor shipping address to the shop base in Cart and checkout
add_action( 'template_redirect', 'set_vendor_shipping_address', 10 );
function set_vendor_shipping_address() {
    if( ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) &&
    if( current_user_can( 'vendor' ) ) {

        // Get shop base country/state
        $address = get_shop_base_address();

        // Set customer shipping
        WC()->customer->set_shipping_address_1( $address['address1'] );
        WC()->customer->set_shipping_address_2( $address['address2'] );
        WC()->customer->set_shipping_city( $address['city'] );
        WC()->customer->set_shipping_postcode( $address['postcode'] );
        WC()->customer->set_shipping_state( $address['state'] );
        WC()->customer->set_shipping_country( $address['country'] );
    }
}


// Save vendor order shipping address to the shop base
add_action( 'woocommerce_checkout_create_order', 'save_vendor_shipping_address', 10, 2 );
function save_vendor_shipping_address( $order, $data ) {
    if( current_user_can( 'vendor' ) ) {

        // Get shop base country/state
        $address = get_shop_base_address();

        // Set customer shipping
        $order->set_shipping_address_1( $address['address1'] );
        $order->set_shipping_address_2( $address['address2'] );
        $order->set_shipping_city( $address['city'] );
        $order->set_shipping_postcode( $address['postcode'] );
        $order->set_shipping_state( $address['state'] );
        $order->set_shipping_country( $address['country'] );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.



来源:https://stackoverflow.com/questions/53605849/assign-dokan-vendor-shipping-address-to-store-address-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!