Multi Vendor Marketplace Plugin | WCFM Marketplace › Forums › WC Marketplace › Add custom dropdown (with options) to Shipping tab on add product form.
- This topic has 6 replies, 2 voices, and was last updated 4 years, 7 months ago by Sushobhan.
- AuthorPosts
- April 11, 2020 at 2:02 am #117810ashley.youngukParticipant
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?
- April 14, 2020 at 7:15 pm #119079ashley.youngukParticipant
Any ideas?
- April 18, 2020 at 2:59 pm #120282SushobhanKeymaster
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! - April 18, 2020 at 6:16 pm #120330ashley.youngukParticipant
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
- April 19, 2020 at 11:16 am #120536SushobhanKeymaster
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! - April 19, 2020 at 12:16 pm #120547ashley.youngukParticipant
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?
- April 20, 2020 at 10:13 pm #121079SushobhanKeymaster
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 websitelet me know how it goes.
- AuthorPosts
- You must be logged in to reply to this topic.