Reputation: 49
On the checkout page, I would like to add a custom field if the contents of the cart weighs over 1000kg.
Is there a way of adding an if statement (to form-checkout.php?) which gets the cart weight and then I can add in a select field?
Upvotes: 2
Views: 178
Reputation: 254286
First read the official related documentation to customize checkout fields or this documentation too.
Here is an example that will add a custom billing select field when cart weight is over 1000 Kg (1 Ton) in checkout page:
add_filter( 'woocommerce_checkout_fields' , 'customizing_checkout_fields', 10, 1 );
function customizing_checkout_fields( $fields ) {
if( WC()->cart->get_cart_contents_weight() > 1000 ) {
// Custom Select field
$fields['billing']['billing_custom'] = array(
'type' => 'select',
'label' => __("Cart weight over 1 Ton", "woocommerce"),
'class' => array('form-row-wide'),
'options' => array(
'' => __("Choose an option please…", "woocommerce"),
'option-1' => __("Option 1", "woocommerce"),
'option-2' => __("Option 1", "woocommerce"),
'option-3' => __("Option 1", "woocommerce"),
),
'priority' => '120',
'required' => true,
);
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Upvotes: 1