Modifying shipping costs among multi-vendors Then to the customer location

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Multi Vendor Marketplace Plugin | WCFM Marketplace Forums WCFM – Marketplace (WooCommerce Multivendor Marketplace) Modifying shipping costs among multi-vendors Then to the customer location

Viewing 3 reply threads
  • Author
    Posts
    • #129290
      abbasi.aye
      Participant

      Hello all,

      Thanks for the great plugin. We are new to WordPress world, but we find that this is the closest plugin to our target.

      We are aiming to modify the shipping cost at the checkout page. The modifying should be to consider the store’s zone (we don’t have postal code in our country) and the customer zone. Such zone will be a drop-down menu for the customer to choose among in the check out page. Then we will have to calculate a fixed rate among the stores, and sum it up, and one shipping cost from a select store’s zone to the customer.

      To do this logic I think We need to be able to read the stores which are shown at the final purchase step (checkout page), used by the customer.

      How to get the stores from the customer cart?

      Would you kindly help us with the above logic?

    • #129432
      Sushobhan
      Keymaster

      Hi,
      Thanks for giving our plugin a try.
      You can retrieve the store info from WC cart object like the following-

      $items = wc()->cart->get_cart();
      foreach ( $items as $item => $values ) {
          $product = wc_get_product( $values['data']->get_id() );
          $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
          if ( $vendor_id ) {
              $store_user = wcfmmp_get_store( $vendor_id );
              $shop_name = $store_user->get_shop_name();
              $url = $store_user->get_shop_url();
              $address = $store_user->get_address();
          }
      }

      wcfmmp_get_store function returns a WCFMmp_Store class object. You can get all the store details from this object. I’ll suggest you visit this class once. Path: wc-multivendor-marketplace\core\class-wcfmmp-store.php
      Thank You!

    • #130249
      abbasi.aye
      Participant

      Thanks a lot of the help

      In order to achieve the above target, we have two snippets,
      1. Snippet-1 to add a drop-down menu in the checkout page to obtain the customer’s special sub-region.
      2. Snippet-2 to fetch the selected value from the previously added drop-down menu (say store it in $user_selected_field), then use it and also using the stores in the cart to modify the shipping rate.

      Can you help me finding a way to get the selected value of selected value from drop down from the checkout page class?

      =====================================================
      2. Snippet-2 Looks like:

      define( ‘WP_DEBUG’, true );

      add_filter( ‘woocommerce_shipping_method_add_rate_args’, ‘AA_shipping_extra_per_product’, 100, 2 );
      function AA_shipping_extra_per_product( $args, $shipping_method ) {
      /* Interacting with dropdown menue code
      */
      $arr_co_fields = wc()->checkout()->get_checkout_fields();//($fieldset = ‘shipping’);
      $user_selected_field = $arr_co_fields[‘billing’][‘billing_city’];//$_POST[$arr_co_fields[‘billing’][‘billing_city’]];
      //$user_selected_field = $_POST[‘dropdown’]; // I’m not sure to read selected value by customer from drop-down menu

      print_r($user_selected_field);

      /* cart info
      */
      $items = wc()->cart->get_cart();
      foreach ( $items as $item => $values ) {
      $product = wc_get_product( $values[‘data’]->get_id() );
      $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
      if ( $vendor_id ) {
      $store_user = wcfmmp_get_store( $vendor_id );
      $shop_name = $store_user->get_shop_name();
      $url = $store_user->get_shop_url();
      $address = $store_user->get_address();

      }
      }

      // decision on extra shipping rate
      $extra_ship = 0;
      if ($vendor_id == 2 && $user_selected_field =2){
      $shippnig_zone = 2
      $extra_ship +=20;
      } elseif($vendor_id == 3 && $user_selected_field =3){
      $shippnig_zone = 3
      $extra_ship +=40;
      }
      $args[‘cost’] = $extra_ship + 1;
      return $args;
      }

    • #131993
      Sushobhan
      Keymaster

      Hi,
      Check out the following snippet and build your logic on top of it-

      add_filter( 'woocommerce_checkout_fields', 'woocommerce_checkout_field_editor' );
      
      // Our hooked in function - $fields is passed via the filter!
      function woocommerce_checkout_field_editor( $fields ) {
          $fields['billing']['customer_zone_dropdown'] = array(
              'type'     => 'select',
              'required' => true,
              'label'    => __( 'Customer zone' ),
              'class'    => array( 'form-row-wide', 'address-field' ),
              'options'  => array(
                  ''      => 'Choose zone',
                  'zone1' => 'This zone add $10',
                  'zone2' => 'This zone add $20',
                  'zone3' => 'This zone add $30',
              ),
          );
          return $fields;
      }
      
      add_filter( 'woocommerce_shipping_method_add_rate_args', 'AA_shipping_extra_per_product', 100, 2 );
      
      function AA_shipping_extra_per_product( $args, $shipping_method ) {
          /* Interacting with dropdown menue code
           */
          if ( ! is_checkout() || empty($_POST['post_data']) )
              return $args;
          $arr_co_fields = wp_parse_args( wc_clean( wp_unslash( $_POST['post_data'] ) ) );
          $user_selected_field = isset( $arr_co_fields['customer_zone_dropdown'] ) ? $arr_co_fields['customer_zone_dropdown'] : '';
          $extra_cost = 0;
          if ( ! (empty( $args['package'] )) && isset( $args['package']['vendor_id'] ) ) {
              if ( $user_selected_field === 'zone1' ) {
                  $extra_cost = 10;
              } elseif ( $user_selected_field === 'zone2' ) {
                  $extra_cost = 20;
              } elseif ( $user_selected_field === 'zone3' ) {
                  $extra_cost = 30;
              }
          }
          $args['cost'] += $extra_cost;
          return $args;
      }

      Please note, it will work only for Zone shipping. Thank You!

Viewing 3 reply threads
  • You must be logged in to reply to this topic.