Search code examples
phpwoocommerceproductcartcheckout

Add dropdown to products and display the value on Woocommerce cart


I'm having trouble adding a custom dropdown to the cart meta and displaying it in the cart. The code below works for a number field that is also on the single product page, but not for the dropdown for some reason. Does anyone know why? The code is as follows:

// Dropdowns-in-ends starts here
add_action( 'woocommerce_before_add_to_cart_button', 'func_dropdown_in_ends');
function func_dropdown_in_ends() {
    printf(
        '<div class="class_dropdown_ends"><label for="id_dropdown_one_end">I andre enden av tauet</label><id = id_dropdown_one_end name=dropdown_one_end>
        <select>
        <option value="0">Ingenting</option>
        <option value="250">Øye       250,-</option>
        <option value="350">Sjakkel   350,-</option>
        <option value="400">Spleis    400,-</option>
        </select></div>' 
    );
}

//Add dropdown_one_end to cart meta when user clicks add to cart
add_filter( 'woocommerce_add_cart_item_data', 'func_add_dropdown_ene_enden', 10, 4 );
function func_add_dropdown_ene_enden( $cart_item_data, $product_id, $variation_id) {
    if( ! empty( $_POST['dropdown_ene_one_end'] ) ) {
        // Add the item data
        $cart_item_data['one_end'] = $_POST['dropdown_one_end'];
    }
    return $cart_item_data;
}

// Show dropdown_one_end in cart
add_filter( 'woocommerce_cart_item_name', 'func_cart_item_name_ene_enden', 10, 3 );
function func_cart_item_name_ene_enden( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['one_end'] ) ) {
        $name .= sprintf(
            '<p>I ene enden: %s</p>',
            esc_html( $cart_item['one_end'])
        );
    }
    return $name;
}

EDIT: As I see it the dropdown has an 'option', but also an option value. Do I have to reference them individually to get things to work?


Solution

  • There are some errors and mistakes in you code… Try the following code instead (commented):

    // Display a custom dropdown in single product pages before add_to_cart button
    add_action( 'woocommerce_before_add_to_cart_button', 'display_dropdown_in_ends');
    function display_dropdown_in_ends() {
        echo '<div class="class_dropdown_ends">
            <label for="id_dropdown_one_end">I andre enden av tauet</label>
            <select id ="id_dropdown_one_end" name="dropdown_one_end">
                <option value="0">Ingenting</option>
                <option value="250">Øye       250,-</option>
                <option value="350">Sjakkel   350,-</option>
                <option value="400">Spleis    400,-</option>
            </select>
        </div>';
    }
    
    // Add dropdown value as custom cart item data, on add to cart
    add_filter( 'woocommerce_add_cart_item_data', 'add_dropdown_value_to_cart_item_data', 10, 4 );
    function add_dropdown_value_to_cart_item_data( $cart_item_data, $product_id, $variation_id) {
        if( isset($_POST['dropdown_one_end']) && ! empty($_POST['dropdown_one_end']) ) {
            // Add the dropdown value as custom cart item data
            $cart_item_data['one_end'] = esc_attr($_POST['dropdown_one_end']);
            $cart_item_data['unique_key'] = md5(microtime().rand()); // Make each added item unique
        }
        return $cart_item_data;
    }
    
    // Cart page: Display dropdown value after the cart item name
    add_filter( 'woocommerce_cart_item_name', 'display_dropdown_value_after_cart_item_name', 10, 3 );
    function display_dropdown_value_after_cart_item_name( $name, $cart_item, $cart_item_key ) {
        if( is_cart() && isset($cart_item['one_end']) ) {
            $name .= '<p>'.__("I ene enden:") . ' ' . esc_html($cart_item['one_end']) . '</p>';
        }
        return $name;
    }
    
    // Checkout page: Display dropdown value after the cart item name
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'display_dropdown_value_after_cart_item_quantity', 10, 3 );
    function display_dropdown_value_after_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
        if( isset($cart_item['one_end']) ) {
            $item_qty .= '<p>'.__("I ene enden:") . ' ' . esc_html($cart_item['one_end']) . '</p>';
        }
        return $item_qty;
    }
    

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

    On cart page:

    enter image description here

    On checkout page:

    enter image description here