0

I want to add Assembly options at the checkout page like this https://imgur.com/a/WrzWCiO

Anyone knows how to do it without plugin? Thanks

add_action('woocommerce_after_order_notes', 'custom_checkout_fields');

function custom_checkout_fields($checkout) {
    echo '<div id="custom_checkout_fields"><h2>' . __('Additional Services') . '</h2>';

woocommerce_form_field('assembly_option', array(
        'type' => 'select',
        'class' => array('form-row-wide'),
        'label' => __('Assembly Service'),
        'required' => false,
        'options' => array(
            '' => __('None'),
            'individual_item' => __('Assembly for an Individual Item (+$200)'),
            'set' => __('Assembly for a Set (+$600)'),
        )
    ), $checkout->get_value('assembly_option'));

    woocommerce_form_field('trash_removal_option', array(
        'type' => 'select',
        'class' => array('form-row-wide'),
        'label' => __('Trash Removal Service'),
        'required' => false,
        'options' => array(
            '' => __('None'),
            'individual_item' => __('Trash Removal for an Individual Item (+$50)'),
            'set' => __('Trash Removal for a Set (+$150)'),
        )
    ), $checkout->get_value('trash_removal_option'));

    echo '</div>';
}


add_action('woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta');

function custom_checkout_field_update_order_meta($order) {
    if (!empty($_POST['assembly_option'])) {
        $order->update_meta_data('assembly_option', sanitize_text_field($_POST['assembly_option']));
    }
    if (!empty($_POST['trash_removal_option'])) {
        $order->update_meta_data('trash_removal_option', sanitize_text_field($_POST['trash_removal_option']));
    }
}


add_action('woocommerce_cart_calculate_fees', 'add_assembly_trash_removal_cost');

function add_assembly_trash_removal_cost() {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $assembly_option = WC()->session->get('chosen_assembly_option');
    $trash_removal_option = WC()->session->get('chosen_trash_removal_option');

    if ($assembly_option) {
        switch ($assembly_option) {
            case 'individual_item':
                $assembly_cost = 200;
                break;
            case 'set':
                $assembly_cost = 600;
                break;
            default:
                $assembly_cost = 0;
        }
        WC()->cart->add_fee(__('Assembly Service', 'text-domain'), $assembly_cost);
    }

    if ($trash_removal_option) {
        switch ($trash_removal_option) {
            case 'individual_item':
                $trash_removal_cost = 50;
                break;
            case 'set':
                $trash_removal_cost = 150;
                break;
            default:
                $trash_removal_cost = 0;
        }
        WC()->cart->add_fee(__('Trash Removal Service', 'text-domain'), $trash_removal_cost);
    }
}


add_action('woocommerce_checkout_update_order_review', 'update_session_with_selected_options');

function update_session_with_selected_options($posted_data) {
    if (isset($posted_data['assembly_option'])) {
        WC()->session->set('chosen_assembly_option', $posted_data['assembly_option']);
    }
    if (isset($posted_data['trash_removal_option'])) {
        WC()->session->set('chosen_trash_removal_option', $posted_data['trash_removal_option']);
    }
}
1
  • You'd have to write your own plugin to record this info as order data at checkout. Commented Jul 22 at 18:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.