For some reason, my WooCommerce isn't showing labels nor placeholders for custom fields. No matter whether I'm adding the fields manually through functions.php (code below) or using plugins like 'WooCommerce Mailchimp', the fields shows up alright but there's no label next to it.
What might be causing this type of error?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
Change the key from shipping_phone to something else.
It seems like some key names are reserved even if they are not shown in the checkout fields list: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
Related
I cant figure out how to reorder the additional fields, on the checkout page in WooCommerce.
I have added one extra field to the WooCommerce additional information section. I would like to show the time field first then the order notes below it.
This is the code that I am using:
add_filter( 'woocommerce-additional-fields', 'custom_order_fields', 20, 1 );
function custom_order_fields( $fields ) {
$fields['order_comments']['priority'] = 80;
$fields['woocommerce-delivery-time-field']['priority'] = 70;
return $fields;
}
However, this does not have the desired result. Can someone tell me what I'm doing wrong?
If you want to show your custom field first, and then the order notes.
You can either use:
// Add 'delivery time' field before 'order comments'
function filter_woocommerce_checkout_fields( $fields ) {
// Get 'order comments' field
$order_comments = $fields['order']['order_comments'];
// Unset 'order comments' field
unset( $fields['order']['order_comments'] );
// Add 'delivery time' field
$fields['order']['delivery_time'] = array(
'label' => __( 'Delivery time', 'woocommerce' ),
'required' => true,
'type' => 'text',
'class' => array( 'form-row-wide' ),
);
// Add 'order comments' field
$fields['order']['order_comments'] = $order_comments;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'filter_woocommerce_checkout_fields', 10, 1 );
OR use the woocommerce_before_order_notes action hook
function action_woocommerce_before_order_notes( $checkout ) {
// Add field
woocommerce_form_field( 'delivery_time', array(
'type' => 'text',
'class' => array( 'form-row form-row-wide' ),
'label' => __( 'Delivery time', 'woocommerce' ),
'required' => true,
), $checkout->get_value( 'delivery_time' ) );
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
You would need to add the field to the WooCommerce Custom Field first before you set the priority likeso.
add_action('woocommerce_checkout_fields', 'add_woocommerce_additional_fields');
// Function to add field
function add_woocommerce_additional_fields( $fields ) {
$fields['order']['delivery_time'] = array(
'type' => 'text',
'label' => __('Delivery time', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
// You can set your priority here
// Just higher than it a bit
$fields['order']['order_comments']['priority'] = 80;
$fields['order']['delivery_time']['priority'] = 70;
return $fields;
}
You can check here for more information on ordering of fields in Woocommerce.
I want to make a checkbox that needs to be clicked before a product can be added to the cart.
add_action( 'woocommerce_before_add_to_cart_button', 'add_privacy_policy', 9 );
function add_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'I\'ve read and accept the Privacy Policy',
));
}
This is what I have so far. I now need to create an action that fires after I clicked the add to cart button but before the product is actually placed into the cart. If the box is not ticked I want to make it so that it outputs an error and the product is not added to the cart.
As I don't really know woocommerce I wanted to know whether there's such an action and if not, how to create the same effect.
Just add the follows code snippet -
function add_privacy_policy_validation( $passed ) {
if ( !isset( $_REQUEST['privacy_policy'] ) ) {
wc_add_notice( __( 'Please accept privacy policy', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_privacy_policy_validation', 99 );
I want to edit my billing address at my website, where i need to add and delete some others in my account page, which code shall I edit?
Thank you in advanced
Can you please check below code you can add new custom field example.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing']['billing_options'] = array(
'label' => __('Custom Field', 'woocommerce'), // Add custom field label
'placeholder' => _x('Custom Field', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('own-css-name') // add class name
);
return $fields;
}
To delete an existing field, country for example:
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
function custom_override_billing_fields( $fields ) {
unset($fields['billing_country']);
return $fields;
}
maybe the best way use woocommerce_default_address_fields - is it ?
for example as
#5447232
add_filter( 'woocommerce_default_address_fields', 'add_MyNewOption_address' );
function add_MyNewOption_address( $fields ) {
$fields['MyNewOption'] = array(
'label' => __('Custom Field', 'woocommerce'), // Add custom field label
'placeholder' => _x('Custom Field', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('own-css-name') // add class name
);
return $fields;
}
I am trying to rename the field text on checkout page woocommerce this is the code which i am using.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_1']['label'] = 'Address (No PO Boxes)';
return $fields;
}
Now what the issue is while uploading page Address (No PO Boxes) is showing but the page load fully it start shows Address again i am confused why it is showing like that
I got an answer i update the label in woocommerce/includes/class-ec-countries.php. There you can see the code on line no.509 code will be in array
'address_1' => array(
'label' => __( 'Address', 'woocommerce' ),
'placeholder' => _x( 'Street address', 'placeholder', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide', 'address-field' )
),
i changes the Address with my required text and it is now working awesome
I have added custom text box field in my add product page in woocommerce. Now i want to make it required (compulsory). I tried it by passing argument "required"=>true. but it is not working. Please see below code
woocommerce_wp_text_input(
array(
'id' => 'special_price',
'label' => __( 'Wholesaler Price *', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'required' => 'true',
'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
)
);
but it is not making textbox compulsory. Please can anyone tell how can i do this?
For the required HTML5 attribute and other custom attributes woocommerce_wp_text_input() function has custom_attributes option.
woocommerce_wp_text_input(
array(
'id' => 'special_price',
'label' => __( 'Wholesaler Price *', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'custom_attributes' => array( 'required' => 'required' ),
'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
)
);
You can modify the following code as per your need.
// Validate when adding to cart
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_custom', 10, 3 );
/ validation
function woocommerce_add_to_cart_validation_custom($passed, $product_id, $qty){
global $woocommerce;
$option = ''; // your custom field's name
if( isset($_POST[sanitize_title($option)]) && $_POST[sanitize_title($option)] == '' )
$passed = false;
if (!$passed)
$woocommerce->add_error( sprintf( __('"%s" is a required field.', 'woocommerce'), $option) );
return $passed;
}
For even more options while adding a product in the cart you may find How to add a custom text box value to cart session array in Woocommerce my this answer helpful.