Add custom dropdown (with options) to Shipping tab on add product form.

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 WC Marketplace Add custom dropdown (with options) to Shipping tab on add product form.

Viewing 6 reply threads
  • Author
    Posts
    • #117810
      ashley.younguk
      Participant

      Hi

      I would like to add a custom field (dropdown) to the shipping tab on the add product form called ‘Shipping Method’ with the following options, Royal Mail, DPD, Other Courier. I would then like to display whatever is entered here on the cart page next to the shipping cost.

      Can you help?

    • #119079
      ashley.younguk
      Participant

      Any ideas?

    • #120282
      Sushobhan
      Keymaster

      Hello,
      You can use the following snippet to add the shipping method field under shipping tab and saving that data in product meta-

      add_filter( 'wcfm_product_manage_fields_shipping', function($shipping_fields, $product_id) {
          if ( apply_filters( 'wcfm_is_allow_shipping', true ) ) {
              $shipping_method_options = array(
                  'royal_mail' => 'Royal Mail',
                  'dpd'        => 'DPD',
                  'other'      => 'Other Courier'
              );
              $shipping_method = get_post_meta( $product_id, 'wcfm_custom_shipping_method', true );
              $shipping_fields['wcfm_custom_shipping_method'] = array( 'label' => __( 'Shipping method', 'wc-frontend-manager' ), 'type' => 'select', 'options' => $shipping_method_options, 'class' => 'wcfm-select wcfm_ele simple variable booking', 'label_class' => 'wcfm_title', 'value' => $shipping_method );
          }
          return $shipping_fields;
      }, 10, 2 );
      
      add_action( 'after_wcfm_products_manage_meta_save', function( $product_id, $form_data ) {
          if ( apply_filters( 'wcfm_is_allow_shipping', true ) ) {
              if ( isset( $form_data['wcfm_custom_shipping_method'] ) ) {
                  update_post_meta( $product_id, 'wcfm_custom_shipping_method', $form_data['wcfm_custom_shipping_method'] );
              }
          }
      }, 150, 2 );

      there is another part of your query that involves showing this method under shipping cost. Now if there is more than 1 product in the cart and both have different method set, what you want to show then? Do you have a layout in mind?
      Let me know.
      Thanks!

    • #120330
      ashley.younguk
      Participant

      Great! Thank you – are you able to put this into a shortcode so that I can place it anywhere? With this format:

      Postage: Royal Mail

      Thank you

    • #120536
      Sushobhan
      Keymaster

      Sorry I didn’t get your requirement. There are many possibilities and you provide too little information. And the example you shared makes it more confusing (Postage: Royal Mail).
      Our current code adds a dropdown field under Shipping tab. Now I can write a shortcode where you need to provide the product id and it will show the corresponding shipping method like this [postage product_id=”25″] //output a string value- Royal Mail/DPD /Other Courier
      But if you want this field in dropdown and like to save the value, then it will not be possible. Because then it can be used anywhere in the website and thus we could not determine which saving hook we’ll use to save that data.
      Please specify your requirements in more detail.
      Thanks!

    • #120547
      ashley.younguk
      Participant

      Hi

      I use elementor to build the product pages, so a shortcode that I can place in the elementor template that will automatically see what product/vendor it is and then display the field as text like ‘Postage: Royal Mail’ would be perfect. Is that possible?

    • #121079
      Sushobhan
      Keymaster

      Hello,
      Use the following snippet-

      function get_product_shipping_method( $attr ) {
          global $wcfm, $post;
          $product = '';
          if ( ! empty( $attr['id'] ) ) {
              $product = wc_get_product( absint( $attr['id'] ) );
          }
          if ( ! $product && is_product() ) {
              $product = wc_get_product();
          }
          if ( ! $product )
              return '';
      
          $shipping_method = wcfm_get_post_meta( $product->get_id(), 'wcfm_custom_shipping_method' );
          return $shipping_method ? 'Postage: ' . ucwords( str_replace( '_', ' ', $shipping_method ) ) : '';
      }
      
      add_shortcode( 'shipping_method', 'get_product_shipping_method' );

      Usage –
      [shipping_method] //inside a single product page
      [shipping_method id=”PRODUCT_ID”] //anywhere in the website

      let me know how it goes.

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