Search code examples
woocommercehook-woocommercewoocommerce-theming

Add dropdown list to woocommerce cart and retrieve value on checkout?


I'd like to modify cart.php to contain a dropdown list of "checkout agents" and then retrieve that data when checking out to redirect to the correct agent (different URLs).

I already have cart.php in the child theme. I have created a cart-custom-checkout.php page to handle retrieving the cart contents and redirecting (wp_redirect) to an agents URL (I tested a single agent by building the URL from the cart contents and it works great).

I enabled the custom checkout via modifying the child theme functions.php as follows:

// enable custom checkout for woocommerce
add_shortcode( 'cart_custom_checkout', 'cart_custom_checkout_function' );
function cart_custom_checkout_function() {
        wc_get_template('cart/cart-custom-checkout.php');
}

Then used that shortcode on the checkout page.

Now that everything works like I want in concept, the final step is to create the dropdown list in the cart.php so one of several agents can be chosen. From there I then need to somehow retrieve that in the cart-custom-checkout.php page. If it were part of the WC() cart data or something like that would be great.

Can someone help explain how I can do what I'm wanting to do?

TIA!!

Update: I've gone a little further and added my dropddown list as follows:

add_action('woocommerce_after_cart_totals', 'woocommerce_cart_add_resellers');
function woocommerce_cart_add_resellers() {
    wc_get_template('cart/cart-add-reseller.php');
}

The cart-add-reseller.php contents are:

<div style="margin-top:5px">
    <select name="agent">
    <option value="Agent1">Use Agent1</option>
    <option value="Agent2">Use Agent2</option>  
    </select>
</div>

Now just need to get what agent value is in the cart-custom-checkout.php page. How do I do that?

Update2: I am not getting any $_POST data when clicking the proceeded to checkout button.

// enable custom checkout for woocommerce
add_shortcode( 'cart_custom_checkout', 'cart_custom_checkout_function' );
function cart_custom_checkout_function() {
    var_dump($_POST);
    wc_get_template('cart/cart-custom-checkout.php');
}

It's empty.

Update 3: I've check $post which just gives me the checkout page itself with the shortcode in it, $_POST is empty, $_SESSIONS is empty, $_GET is empty. I'm not sure where the $_POST data went, maybe some javascript or something like that which I know nothing about. If I could just know a filter or action or something to give me the $_POST data, it will have the value I need. So I give up for now.

Any help would be appreciated.


Solution

  • If I fully understand what you have, if you were to add the following in functions.php, it will add the required field to the cart page and allow it to be accessible via $_POST on the checkout page. This essential replaces the default Proceed to Checkout button, with a new one that includes the select field and a post action.

    add_action( 'woocommerce_proceed_to_checkout', 'add_agent_before_proceed_to_checkout', 15 );
    function add_agent_before_proceed_to_checkout() {
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    ?>
        <form id="checkout_form" method="POST" action="<?php echo esc_url ( wc_get_checkout_url() ); ?>">
            <div style="margin-top:5px">
                <select name="agent">
                    <option value selected="selected">-- Select an Agent --</option>
                    <option value="Agent1">Use Agent1</option>
                    <option value="Agent2">Use Agent2</option>  
                </select>
            </div>
            <button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
    esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
        </form>
    <?php
    }
    

    The reason what you had added to cart didn't work, is the Proceed to Checkout button (woocommerce\templates\cart\proceed-to-checkout-button.php) by default doesn't do a post action, it is actually a link. Also the spot you were adding your input was not within a form so wouldn't have posted the value.