Set billing_address_2 label in Woocommerce checkout - woocommerce

I'm trying to set / show a label for the billing_address_2 field on the Woocommerce checkout page, but can't find a way to do this. Does anyone know a solution?
The code below (which works fine on other fields) does not do the job.
add_filter( 'woocommerce_checkout_fields' , 'custom_rename_wc_checkout_fields' );
function custom_rename_wc_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['label'] = 'Building number';
return $fields;
}

Updating this question with an answer for the sake of future readers.
As of WooCommerce 3.5.1, commit 87054ece9a4c05db72e139730ed1764c63fab635 adds the 'screen-reader-text' label_class to both Billing & Shipping Address 2 fields for accessibility purposes as per Issue #21182. This class will keep the label hidden unless it is also changed (e.g. make it blank, like the Address 1 field).
Here's how I updated my functions.php to get the billing/shipping_address_2 labels back (I'm using the separate billing/shipping filters as I have made changes to other fields I didn't include in the code for brevity).
// Billing Fields.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_address_2']['label'] = 'Address 2';
$fields['billing_address_2']['label_class'] = '';
return $fields;
}
// Shipping Fields.
add_filter( 'woocommerce_shipping_fields', 'custom_woocommerce_shipping_fields' );
function custom_woocommerce_shipping_fields( $fields ) {
$fields['shipping_address_2']['label'] = 'Address 2';
$fields['shipping_address_2']['label_class'] = '';
return $fields;
}

Related

Customize woocommerce order tracking page

in the default order tracking page for wooocommerce there are two fields { ordreid and email }
.
I want to make email field unrequired
I tried with this code in function.php file but it wasn't successful :(
add_filter( 'woocommerce-order-tracking', 'ts_unrequire_wc_email_field');
function ts_unrequire_wc_email_field( $fields ) {
$fields['order_email']['required'] = false;
return $fields;
}
any help will be useful .. and i hope that you are all safe during the breakout of covid-19 <3**

Woocommerce Checkout: Add placeholder in country dropdown [duplicate]

This question already has answers here:
WooCommerce: Set country by default in checkout page
(5 answers)
Closed 2 years ago.
in the checkout of my Woocommerce-Shop is a dropdown to choose your country.
On default the USA is automatically selected already.
How to just have a placeholder with “select your country” instead?
I couldn’t find any solution to this topic somebody have any idea?
I changed the other Placeholders which aren't Dropdowns but are inside the same form:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name*';
$fields['billing']['billing_last_name']['placeholder'] = 'Last Name*';
$fields['billing']['billing_city']['placeholder'] = 'Town / City*';
$fields['billing']['billing_postcode']['placeholder'] = 'ZIP*';
$fields['billing']['billing_email']['placeholder'] = 'Email Address*';
return $fields;
}
My poor solution for now is: I created a new "Country in the Dropdown List" which I just named: select your country* and then just selected it on default instead of the USA. But the problem is here that the system thinks a real country is chosen already so it's not a mandatory field anymore and also the fact that it just doesn't look usual for the user when they choose their country:
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
I would appreciate any help or tips!
This is working for my side try this
// Change the default country and state on checkout page.
// This works for a new session.
add_filter( 'default_checkout_country', 'xa_set_default_checkout_country' );
add_filter( 'default_checkout_state', 'xa_set_default_checkout_state' );
function xa_set_default_checkout_country() {
// Returns empty country by default.
return null;
// Returns India as default country.
// return 'IN';
}
function xa_set_default_checkout_state() {
// Returns empty state by default.
return null;
// Returns Madhya Pradesh as default state.
// return 'MP';
}
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
Just go to WooCommerce > Settings.
On General tab you will find Default customer location. Set it to No location by default
This was one of the first results that popped up for me when searching how to set a placeholder for a select field on the Woocommerce checkout page, so for any future visitors:
I have a company select field that I populate dynamically with values from the database. In order to set a placeholder value, I just add an option with a blank value.
add_filter( 'woocommerce_checkout_fields' , 'populate_company_field' );
function populate_company_field($fields) {
$results = get_results_from_database();
$options[''] = __('Choose your company', 'gs'); //add blank value = placeholder
foreach ($results as $result) {
if (!empty($result->company)) {
$options[$result->ID] = $result->company;
}
}
$fields['billing']['company']['options'] = $options;
return $fields;
}

WooCommerce not letting me add custom column to shop_order post type

I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.
add_filter('manage_edit-shop_order_columns', 'add_sales_column');
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1
How to solve this?
I encountered a similar issue when adding a custom column to the WooCommerce Orders overview page from my theme's functions.php file. I was able to get a custom column to show up by increasing the filter priority above the default value of 10. Try replacing your code with the following:
add_filter('manage_edit-shop_order_columns', 'add_sales_column', 11);
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Tested on WP 3.9.1 and WC 2.1.12.
Check out the WordPress Codex entry on add_filter for more details on the behavior of filters using the $priority parameter.
The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.
add_action( 'plugins_loaded', 'setup_so_22237380' );
function setup_so_22237380()
{
// Just to make clear how the filters work
$posttype = "shop_order";
// Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
// Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' );
}
function column_set_so_22237380( $columns )
{
$columns['order_sales'] = "Sales";
return $columns;
}
function column_display_so_22237380( $column_name, $post_id )
{
if ( 'order_sales' != $column_name )
return;
$sales_information = 'Your custom get_order_sales_information($post_id)';
if ( $sales_information )
echo "<strong style='color:#f00;'> $sales_information </strong>";
}
function column_sort_so_22237380( $columns )
{
$columns['order_sales'] = 'order_sales';
return $columns;
}
Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.
It will working fine if you include all the order hook on
add_action( 'admin_init', 'setup_all_order_column_hook' );
function setup_all_order_column_hook(){
//Just to make clear how the filters work
$posttype = "shop_order";
//Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
//Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column",column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns",'column_sort_so_22237380' );
}

Hide Shipping Options Woocommerce

So I'm trying to hide certain ship methods in Woocommerce based on a product tag. The main problem I face is my own lack PHP knowledge so I frankensteined the following code together with the help of some very friendly folks:
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function abve to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flar_rate" with the shipping option that yu want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
I understand that this code is by no means universal and thus the variables will be different from case to case but perhaps someone can tell me if something looks off in the code. Much appreciated
No doubt you have sorted this by now but your code was a good start for me ... and since I sorted it I have published it below. Your problem was that woocommerce doesn't have the product_tag in the cart array so you have to go and get it.
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_tag', array( "fields" => "names" ) );
if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
You can use shipping class of Woocommerce to achieve this.
Here is the step by step instructions.
Create a shipping class (woocommerce->settings->shipping->shipping classes).
Associate this shipping class with products (that you wand to hide shipping methods for)
Use this snippet. (copy and pate to function.php).
For more information about the snippet is available here.

Translate product category|tag title for JigoShop with qTranslate

qTranslate creates additional language fields for products pages in JigoShop but not also for category|tags product as it does for posts.
If I put in the title of a menu item <!--:en-->title<!--:--><!--:fr-->title<!--:--> i'll get the translation I want. But when creting a new category|tag title the <!--:--> is striped out. How can I enable comments tags for cat|tag title?
Another option is to use [:en]Title[:fr]Titre in the same title field when creating a new category|tag product. On the admin panel I see the proper text for the language selected but for end user i see [:en]Title[:fr]Titre.
I found this link https://wordpress.stackexchange.com/questions/28165/translating-a-custom-taxonomy and according to this answer http://www.qianqin.de/qtranslate/forum/viewtopic.php? f=4&t=2045&start=0#p7380 I addet in functions.php
add_action('jigoshop_add_form', 'qtrans_modifyTermFormFor');
add_action('jigoshop_edit_form', 'qtrans_modifyTermFormFor');
Did not work. I don't see aditional translations fields for categories|tags in JigoShop.
The basic question is:
How do I translate product categories|tags in JigoShop using qTranslate?
I was having very similar problem. I found a solution here: http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=3&t=4064&start=10#p12940
Basically, all you have to do is to rename all the categories as suggested in the answer above, then add to your theme's functions.php file:
function p_filter_categories( $categories ) {
if ( is_array( $categories ) ) {
foreach ( $categories as $i => $cat ) {
$categories[ $i ]->name = __( $cat->name );
}
} else {
$categories->name = __( $categories->name );
}
return $categories;
}
add_filter( 'get_the_categories', 'p_filter_categories', 10 );
add_filter( 'get_the_terms', 'p_filter_categories', 10 );
add_filter( 'get_term', 'p_filter_categories', 10 );
Hope that helps!
Not the ideal solution but works.
In JigoShop/edit product category/name:
[:en] Big [:fr] Grand
In functions.php
function translate_q ($echo) {
if (function_exists('qtrans_split')) {
$selectLanguage = qtrans_split($echo);
return $selectLanguage[qtrans_getLanguage()];
} else {
return $echo;
}
}
qtrans_split and qtrans_getLanguage are functions created by qTranslate.
From JigoShop plugin directory I opened jigoshop_template_functions.php, I grabbed from jigoshop_breadcrumb() function all the echos in $echo variable and at the end I have:
echo function_exists('translate_q') ? translate_q($echo) : $echo;
You will have to do the same thing in other places in JigoShop. I posted here to be a starting point.

Resources