checkout page field rename in woocommerce - php

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

Related

How to reorder additional fields on checkout page in WooCommerce

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.

Trying to place a custom field after the total section in the checkout page in woocommerce

This code correctly works but does not place the custom field where I want it. In the add_action I would like to use woocommerce_review_order_after_order_total as the location attribute to place it on the page. I got the hook name from this visual guide https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/ However this hook (seen in a comment in the code below breaks my code and gives me the following php error.
Fatal error: Uncaught Error: Call to a member function get_value() on string in /Users/anderskitson/Local Sites/river-cafe/app/public/wp-content/themes/salient-child/functions.php on line 96
Hopefully someone can give me hand. Thanks
/**
* Add custom field to the checkout page
*/
add_action('woocommerce_after_order_notes', 'custom_checkout_field');
/* add_action('woocommerce_review_order_after_order_total', 'custom_checkout_field');*/
/* ^ This is the code that is breaking */
function custom_checkout_field($checkout)
{
echo '<div id="custom_checkout_field"><h2>' . __('New Heading') . '</h2>';
woocommerce_form_field(
'custom_field_name',
array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Custom Additional Field') ,
'placeholder' => __('New Custom Field') ,
) ,
$checkout->get_value('custom_field_name')
);
echo '</div>';
}
Remove $checkout->get_value('custom_field_name') so that it's just:
woocommerce_form_field(
'custom_field_name',
array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Custom Additional Field') ,
'placeholder' => __('New Custom Field') ,
)
);
Any user value should be filled automatically, or it will simply be blank.

Custom new tab in Woocommerce my account pages

This question is about my woocommerce shop. I am trying to add new tab in my-account page (ticket tab). Eeverything going right, but when I hit the "ticket" tab I getting "404 not found" error!
I thought it should be work, but its no working.
Here is changes log:
add ticket code to woocommerce/includes/wc-template-functions.php
if ( ! function_exists( 'woocommerce_account_ticket' ) ) {
/**
* My Account > Ticket template.
*/
function woocommerce_account_ticket() {
wc_get_template( 'myaccount/ticket.php' );
}
}
add ticket code to woocommerce/includes/wc-template-hooks.php
add_action( 'woocommerce_account_ticket_endpoint', 'woocommerce_account_ticket' );
add ticket code to woocommerce/includes/admin/settings/class-wc-settings-accounts.php
array(
'title' => __( 'Ticket', 'woocommerce' ),
'desc' => __( 'Endpoint for the "My account → ticket" page.', 'woocommerce' ),
'id' => 'woocommerce_myaccount_ticket_endpoint',
'type' => 'text',
'default' => 'ticket',
'desc_tip' => true,
),
add ticket code to woocommerce/includes/wc-account-functions.php
function wc_get_account_menu_items() {
$endpoints = array(
'ticket' => get_option( 'woocommerce_myaccount_ticket_endpoint', 'ticket' ),
);
$items = array(
'ticket' => __( 'Ticket', 'woocommerce' ),
);
add ticket code to woocommerce/includes/class-wc-query.php
'ticket' => get_option( 'woocommerce_myaccount_ticket_endpoint', 'ticket' ),
I don't want to update my woocommerce plugin, so there is no problem about playing with main plugin codes.

WooCommerce field labels not shown

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/

How can i make custom field value required ( compulsory ) in woocommerce product page when adding product

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.

Categories