Alex
Alex

Reputation: 13

Add custom checkbox to woocommerce_checkout_order_review

I have been scratching my head all day. I would like to add a custom checkbox within order review before placing order. Here is my code:

add_action( 'woocommerce_checkout_order_review', 'my_checkbox' );

function my_checkbox( $checkout ) {
    echo '<div class="my_split_checkbox"><h2>' . __('Split Order', 'woocommerce') . '</h2>';

    woocommerce_form_field( 'my_split_checkbox', array(
        'type'     => 'checkbox',
        'class'    => array('checkbox_field'),
        'label'    => __('Split Order', 'woocommerce'),
        'required' => false,
    ), $checkout->get_value( 'my_split_checkbox' ));

    echo '</div>';
}

but the page doesn't fully load. However if I replace the hook by

add_action( 'woocommerce_after_checkout_billing_form', 'my_checkbox' );

then the chekbox shows at the end of billing details with no issues. How can I get the textbox to show within checkout order review? Ideally after table .woocommerce-checkout-review-order-table.

Upvotes: 1

Views: 7375

Answers (1)

CodeMascot
CodeMascot

Reputation: 855

You should use any of those below hooks instead of woocommerce_checkout_order_review to show the checkbox field based on your priority-

woocommerce_review_order_after_cart_contents
woocommerce_review_order_before_shipping
woocommerce_review_order_after_shipping
woocommerce_review_order_before_order_total
woocommerce_review_order_after_order_total

For further information go to woocommerce/templates/checkout/review-order.php. If you already copied the templates folder as woocommerce to your theme directory then may be you'll find the review-order.php there. And also you need to remove $checkout variable as well as , $checkout->get_value( 'my_split_checkbox' ). Cause those hooks do not pass any parameter. Please check the review-order.php, you'll get an overview.

So your whole code will be like below-

add_action( 'woocommerce_checkout_order_review', 'my_checkbox' );

function my_checkbox() {
    echo '<div class="my_split_checkbox"><h2>' . __('Split Order', 'woocommerce') . '</h2>';

    woocommerce_form_field( 'my_split_checkbox', array(
        'type'     => 'checkbox',
        'class'    => array('checkbox_field'),
        'label'    => __('Split Order', 'woocommerce'),
        'required' => false,
    ));

    echo '</div>';
}

Hope that helps.

Upvotes: 4

Related Questions