How to add 2 select fields (combobox) to woocommerce checkout page? - wordpress

I am developing an online store that offers local delivery days according to zip code. I have limited the zip codes with a selection using this snippet
add_filter ('woocommerce_default_address_fields', 'custom_override_default_postcode_field');
function custom_override_default_postcode_field ($address_fields) {
// Zip codes
$postcode_array = array (
'28001' => '28001',
'28002' => '28002',
'28923' => '28923'
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
I am using the plugin woocommerce checkout field editor and I've added field "delivery days", which is a select field with the values ​​Monday, Tuesday, Wednesday ...
Now, I want to make the second field dependent on the first, according to my client's specifications, for example:
Zip code 28001 / Delivery Days Monday and Wednesday
Zip code 28002 / Delivery days Tuesday and Thursday
Zip code 28923 / Delivery Days Friday and Sunday
That is, when the buyer chooses a postal code, it only shows him to choose the corresponding days.
If there is a way to do that without the plugin I think it will be better
Thanks a lot for your help,
Luis

This required a lots of customization. Try the below code. code goes to your active theme functions.php file.
add_filter ('woocommerce_default_address_fields', 'custom_override_default_postcode_field');
function custom_override_default_postcode_field ($address_fields) {
// Zip codes
$postcode_array = array (
'28001' => '28001',
'28002' => '28002',
'28923' => '28923'
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
You can add a new dropdown field Delivery days using the woocommerce_checkout_fields filter hook.
add_filter( 'woocommerce_checkout_fields' , 'custom_delivery_days_woocommerce_billing_fields' );
function custom_delivery_days_woocommerce_billing_fields( $fields ) {
$fields['billing']['delivery_days'] = array(
'label' => __('Delivery days', 'woocommerce'),
'placeholder' => _x('dropdown', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 95,
'type' => 'custom_select',
'options' => array(
'monday' => array(
'label' => __('Monday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28001',
),
'tuesday' => array(
'label' => __('Tuesday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28002',
),
'wednesday' => array(
'label' => __('Wednesday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28001',
),
'thursday' => array(
'label' => __('Thursday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28002',
),
'friday' => array(
'label' => __('Friday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28923',
),
'sunday' => array(
'label' => __('Sunday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28923',
)
)
);
return $fields;
}
Now as per your requirement I will use data attr for show hide select option based on another select option. but in woocommerce default select dropdown, there is now a way to add data attr to select. so for this, I created the custom_select type so we can add data attr. so as you can see in above woocommerce_checkout_fields filter hook i set 'type' => 'custom_select',.
You can add new your custom field type using the woocommerce_form_field filter hook.
add_filter( 'woocommerce_form_field', 'add_custom_select', 10 , 4 );
function add_custom_select( $field, $key, $args, $value ){
if( $args['type'] == 'custom_select' ){
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
}
$field = '';
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
if ( '' === $option_key ) {
// If we have a blank option, select2 needs a placeholder.
if ( empty( $args['placeholder'] ) ) {
$args['placeholder'] = $option_text['label'] ? $option_text['label'] : __( 'Choose an option', 'woocommerce' );
}
$custom_attributes[] = 'data-allow_clear="true"';
}
$options .= '<option value="' . esc_attr( $option_key ) . '" ' . selected( $value, $option_key, false ) . ' data-'.$option_text['data_attr_name'].'="'.$option_text['data_attr_value'].'" >' . esc_html( $option_text['label'] ) . '</option>';
}
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '">
' . $options . '
</select>';
}
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
if ( ! empty( $field ) ) {
$field_html = '';
if ( $args['label'] && 'checkbox' !== $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . wp_kses_post( $args['label'] ) . $required . '</label>';
}
$field_html .= '<span class="woocommerce-input-wrapper">' . $field;
if ( $args['description'] ) {
$field_html .= '<span class="description" id="' . esc_attr( $args['id'] ) . '-description" aria-hidden="true">' . wp_kses_post( $args['description'] ) . '</span>';
}
$field_html .= '</span>';
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$field = sprintf( $field_container, $container_class, $container_id, $field_html );
}
return $field;
}
return $field;
}
Now on the billing_postcode change, you have to get the billing_postcode value that you need to compare with delivery_days dropdown option data-post-code and based on that you can show hide.
function add_custom_js(){
?>
<script type="text/javascript">
(function($){
$('#billing_postcode').on('change', function(){
var postcode = $(this).val();
var days = $('#delivery_days');
var i = 1;
$('option', days).filter(function(){
if ( $(this).attr('data-post-code') === postcode ) {
$(this).show();
if( i == 1 ){
$("#delivery_days").val($(this).val());
}
i++;
} else {
$(this).hide();
}
});
});
$('#billing_postcode').trigger('change');
})(jQuery);
</script>
<?php
}
add_action( 'wp_footer', 'add_custom_js', 10, 1 );
Tested and works.

Related

Show multiple different custom fields in each variation in WooCommerce with a linked pdf file

In this post I was able to see the code that made a custom field with a link work very well.
Display a selected variation custom field in WooCommerce as a pdf linked file
My problem is that I need to have three fields in each variation with different links each downloading a different datasheet.
I've tried this code, but I can't get it to save the custom fields when saving the variation:
// 1. Add custom field input # Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'ficha[' . $loop . ']',
'class' => 'short',
'label' => __( 'ficha', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'ficha', true )
) );
woocommerce_wp_text_input( array(
'id' => 'diagrama[' . $loop . ']',
'class' => 'short',
'label' => __( 'diagrama', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'diagrama', true )
) );
woocommerce_wp_text_input( array(
'id' => 'esquema[' . $loop . ']',
'class' => 'short',
'label' => __( 'esquema', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
) );
}
// -----------------------------------------
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i )
{
$custom_field = $_POST['ficha'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'ficha', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['diagrama'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'diagrama', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'esquema', esc_attr( $custom_field ) );
}
// -----------------------------------------
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'add_custom_field_value_to_variation_data', 10, 3 );
function add_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
$variation_data['custom_field'] = $variation->get_meta('custom_field');
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Ficha tecnica", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Diagrama", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/05/visibility-e1653592380924.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Esquema", "woocommerce") . '</div>';
}
return $variation_data;
}

WooCommerce variable Meta box product pricing

I am creating a multi-vendor site using WCFM at the moment. I'm trying to recreate the UI from a plugin I saw somewhere but integrating the metabox I made with WooCommerce is the challenging part.
Essentially I want the vendors to just enter the prices for variations than to do it manually by adding the attribute then save it as a variation etc. I have all the metabox plugins. This is the code I have for the recreated metabox:
<?php
add_filter( 'rwmb_meta_boxes', 'yb_flower_price_box' );
function yb_flower_price_box( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Flower Price by Grams', 'yb' ),
'id' => 'flower-price-by-grams',
'post_types' => ['flower', 'product', 'product_variation'],
'storage_type' => 'custom_table',
'table' => 'Flower price',
'fields' => [
[
'name' => __( '1 gram', 'yb' ),
'id' => $prefix . '1_sm7qy7uyf5',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '2 grams ', 'yb' ),
'id' => $prefix . '2_btw3zbak44j',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '3.5 grams', 'yb' ),
'id' => $prefix . '3.5',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '7 grams', 'yb' ),
'id' => $prefix . '7g',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '14 grams', 'yb' ),
'id' => $prefix . '14_q6tlu8qi78',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '28 grams ', 'yb' ),
'id' => $prefix . '28_ufjrtw5u8oc',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
],
'validation' => [
'rules' => [
$prefix . '2_btw3zbak44j' => [
'number' => true,
],
$prefix . '3' => [
'number' => true,
],
],
],
];
return $meta_boxes;
}
This is one of the code snippets from the plugin I'm trying to recreate:
/**
* Product Prices metabox
*
* Adds a product prices metabox to all of the above custom post types
*
* #since 1.0.0
*/
function wp_dispensary_product_prices_metabox() {
// Add metabox.
add_meta_box(
'wp_dispensary_product_prices',
__( 'Product prices', 'wp-dispensary' ),
'wp_dispensary_product_prices_metabox_content',
'products',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'wp_dispensary_product_prices_metabox' );
/**
* Product Prices
*
* #return void
*/
function wp_dispensary_product_prices_metabox_content() {
global $post;
/** Noncename needed to verify where the data originated */
$string = '<input type="hidden" name="wpd_product_prices_meta_noncename" id="wpd_product_prices_meta_noncename" value="' .
wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />';
// Get product prices.
$product_prices = wpd_product_prices();
// Loop through product prices.
foreach ( $product_prices as $key=>$value ) {
$string .= '<div class="input-field product-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
// Get concentrates prices.
$concentrates_prices = wpd_product_prices( 'concentrates' );
// Loop through concentrates prices.
foreach ( $concentrates_prices as $key=>$value ) {
$string .= '<div class="input-field concentrates-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
// Get flower prices.
$flower_prices = wpd_product_prices( 'flowers' );
// Loop through flower prices.
foreach ( $flower_prices as $key=>$value ) {
$string .= '<div class="input-field flower-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
echo $string;
}
/**
* Save the Metabox Data
*
* #param int $post_id
* #param object $post
* #return void
*/
function wp_dispensary_product_prices_metabox_save( $post_id, $post ) {
/**
* Verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times
*/
if (
! isset( $_POST['wpd_product_prices_meta_noncename'] ) ||
! wp_verify_nonce( $_POST['wpd_product_prices_meta_noncename'], plugin_basename( __FILE__ ) )
) {
return $post->ID;
}
/** Is the user allowed to edit the post or page? */
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return $post->ID;
}
/**
* OK, we're authenticated: we need to find and save the data
* We'll put it into an array to make it easier to loop though.
*/
$prices_meta = array();
if ( '' != filter_input( INPUT_POST, 'price_each' ) ) {
$prices_meta['price_each'] = esc_html( $_POST['price_each'] );
}
if ( '' != filter_input( INPUT_POST, 'price_per_pack' ) ) {
$prices_meta['price_per_pack'] = esc_html( $_POST['price_per_pack'] );
}
if ( '' != filter_input( INPUT_POST, 'units_per_pack' ) ) {
$prices_meta['units_per_pack'] = esc_html( $_POST['units_per_pack'] );
}
if ( '' != filter_input( INPUT_POST, 'price_half_gram' ) ) {
$prices_meta['price_half_gram'] = esc_html( $_POST['price_half_gram'] );
}
if ( '' != filter_input( INPUT_POST, 'price_gram' ) ) {
$prices_meta['price_gram'] = esc_html( $_POST['price_gram'] );
}
if ( '' != filter_input( INPUT_POST, 'price_two_grams' ) ) {
$prices_meta['price_two_grams'] = esc_html( $_POST['price_two_grams'] );
}
if ( '' != filter_input( INPUT_POST, 'price_eighth' ) ) {
$prices_meta['price_eighth'] = esc_html( $_POST['price_eighth'] );
}
if ( '' != filter_input( INPUT_POST, 'price_five_grams' ) ) {
$prices_meta['price_five_grams'] = esc_html( $_POST['price_five_grams'] );
}
if ( '' != filter_input( INPUT_POST, 'price_quarter_ounce' ) ) {
$prices_meta['price_quarter_ounce'] = esc_html( $_POST['price_quarter_ounce'] );
}
if ( '' != filter_input( INPUT_POST, 'price_half_ounce' ) ) {
$prices_meta['price_half_ounce'] = esc_html( $_POST['price_half_ounce'] );
}
if ( '' != filter_input( INPUT_POST, 'price_ounce' ) ) {
$prices_meta['price_ounce'] = esc_html( $_POST['price_ounce'] );
}
// Save $prices_meta as metadata.
foreach ( $prices_meta as $key => $value ) {
// Bail on post revisions.
if ( 'revision' === $post->post_type ) {
return;
}
$value = implode( ',', (array) $value );
// Check for meta value and either update or add the metadata.
if ( get_post_meta( $post->ID, $key, false ) ) {
update_post_meta( $post->ID, $key, $value );
} else {
add_post_meta( $post->ID, $key, $value );
}
// Delete the metavalue if blank.
if ( ! $value ) {
delete_post_meta( $post->ID, $key );
}
}
}
add_action( 'save_post', 'wp_dispensary_product_prices_metabox_save', 1, 2 );
I am a newbie and I would greatly appreciate any help or a point in the right direction. Thank you.

Validate the add to cart button in Wordpress

I am new here and looking for some help. I have used the below code in order to change the add to cart button with quantity. The dropdown starts from 0 to 20. So I would like to add a validation to the button when someone clicks on add button with a 0 qty selected. So can you please help me in validating the 0?
Code used for the dropdown:
function woocommerce_quantity_input() {
global $product;
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:5px;', $product )
);
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 0;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 20;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$options .= '<option value="' . $count . '">' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
Here is the documentation you can check the how wordpress sanitization work
https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/
https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data
https://developer.wordpress.org/plugins/security/securing-input/

Show order notes with author and date on WooCommerce admin orders list

Using "Display back Order Notes in Admin orders list on WooCommerce 3.3" answer code, I am able to add Order notes column in Admin orders list, but it only shows that status changed from One status to another.
Now I would like also to show author of this change and date when it's happened, just, just like in order edit pages.
Any suggestions?
It is not necessary to use global $post, $the_order; with manage_shop_order_posts_custom_column. This is because there is a 2nd parameter that contains the $post_id
$latest_note contains more then only the note, also the author and the date are available among others
According to the rules of the art, add CSS via a stylesheet, not via admin_head
It is always nice to see if you have found code and you want to add extra functionality,
you first try before asking for help
To answer your question, apply the following
function custom_shop_order_column( $columns ) {
$ordered_columns = array();
foreach( $columns as $key => $column ){
$ordered_columns[$key] = $column;
if( 'order_date' == $key ){
$ordered_columns['order_notes'] = __( 'Notes', 'woocommerce');
}
}
return $ordered_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );
function custom_shop_order_list_column_content( $column, $post_id ) {
// Get $order object
$order = wc_get_order( $post_id );
if ( $column == 'order_notes' ) {
if ( $order->get_customer_note() ) {
echo '<span class="note-on customer tips" data-tip="' . wc_sanitize_tooltip( $order->get_customer_note() ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
}
// Retrieves the amount of comments a post has.
$amount_of_comments = get_comments_number( $post_id );
if ( $amount_of_comments > 0 ) {
$latest_notes = wc_get_order_notes( array(
'order_id' => $post_id,
'limit' => 1,
'orderby' => 'date_created_gmt',
) );
$latest_note = current( $latest_notes );
// Content
$content = $latest_note->content;
// Added by
$added_by = $latest_note->added_by;
// Date created - https://www.php.net/manual/en/function.date.php
$date_created = $latest_note->date_created->date('j F Y - g:i:s');
if ( isset( $content ) && $amount_of_comments == 1 ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( 'Author: ' . $added_by . '<br/>' . 'Date: ' . $date_created . '<br/>' . $content ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} elseif ( isset( $content ) ) {
// translators: %d: notes count
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( 'Author: ' . $added_by . '<br/>' . 'Date: ' . $date_created . '<br/>' . $content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $amount_of_comments - 1 ), 'woocommerce' ), $amount_of_comments - 1 ) . '</small>' ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
// translators: %d: notes count
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $amount_of_comments, 'woocommerce' ), $amount_of_comments ) ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );

Customize WooCommerce Quantity Selector

I'm trying to change the default +/- quantity selector into a drop down menu with a given amount of number options (i.e. 1-10).
Anyone know how to accomplish this?
Let me know if you'd like me to post any of the code related to this.
I'd like to do this too. So far I found that the quantity markup is generated in woocommerce/templates/single-product/add-to-cart/quantity.php. You could make a copy of this file in a minimal mirror of the woocommerce/templates directory structure in your theme folder, e.g. in this case copy it to yourtheme/woocommerce/single-product/add-to-cart. There you can edit it without altering the plug-in and risk it being overwritten when the plug-in is updated.
There is a plugin that will do this for you called WooCommerce Advanced Product Quantities, it's free and will allow you to set Minimum, Maximum and Step values for all product quantity inputs. Set rules on a per product, per category/tag or site wide.
http://wordpress.org/plugins/woocommerce-incremental-product-quantities/
It also works with WooCommerce Thumbnail Input Quantities which will put those quantity boxes in all of your product thumbnail boxes.
http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/
Enjoy! Full disclosure, I'm the plugin author.
I have not tried this, but I found this code http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html
/* Change Product Quantity Input to Dropdown */
function woocommerce_quantity_input() {
global $product;
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product )
);
if (!empty($defaults['min_value']))
$min = $defaults['min_value'];
else $min = 1;
if (!empty($defaults['max_value']))
$max = $defaults['max_value'];
else $max = 20;
if (!empty($defaults['step']))
$step = $defaults['step'];
else $step = 1;
$options = '';
for($count = $min;$count <= $max;$count = $count+$step){
$options .= '<option value="' . $count . '">' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
You need to override the template "quantity-input.php" to do so add a file with the name as "quantity-input.php" in your-theme/woocommerce/global/ folder then you can make the changes in that file, now wordpress will use your file to display quantity input html.
Paste this in your function.php file
The dropdown list works both on the product page and in the shopping cart.
Image
function woocommerce_quantity_input($args = array(), $product = null, $echo = true) {
if (is_null($product)) {
$product = $GLOBALS['product'];
}
// Default values
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters('woocommerce_quantity_input_max', -1, $product),
'min_value' => apply_filters('woocommerce_quantity_input_min', 0, $product),
'step' => 1,
);
$args = apply_filters('woocommerce_quantity_input_args', wp_parse_args($args, $defaults), $product);
$args['min_value'] = max($args['min_value'], 0);
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;
if (
'' !== $args['max_value'] && $args['max_value'] < $args['min_value']
) {
$args['max_value'] = $args['min_value'];
}
$options = '';
// Add loop
for ($count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step']) {
// Cart item quantity defined?
if ('' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value']) {
$selected = 'selected';
} else {
$selected = '';
}
$options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
}
$html = '<div><div class="quantity form-select-wrapper"><select class="form- select" name="' . $args['input_name'] . '">' . $options . '</select></div><!--/.form-select-wrapper --></div>';
if ($echo) {
echo $html;
} else {
return $html;
}
}
Hi paste this in your function.php file
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
} else {
$defaults = array(
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'step' => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
'max_value' => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
'style' => apply_filters( 'cw_woocommerce_quantity_style', 'float:left;', $product )
);
}
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 15;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
}
echo '<div class="cw_quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" class="cw_qty">' . $options . '</select></div>';
}

Resources