I found a way to change the placeholder but not the label of the checkout input fields in woocommerce.
I would like to change the label for the adress_field_2.
Here is my attempt that did not change anything. I tried [label] and [label_class] but that did not work...
add_filter( 'woocommerce_default_address_fields', 'new_checkout_field_label', 10, 1 );
function new_checkout_field_label( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( '', 'woocommerce' );
$address_fields['address_2']['label'] = __( 'Apt, Unit, Etc (optional)', 'woocommerce' );
return $address_fields;
}
Your code works and the correct related html is there. But it's hidden from the tag class screen-reader-text by a CSS rule.
To make the <label> visible, you need to remove the class from the <label> tag adding this line:
$address_fields['address_2']['label_class'] = array(); // No label class
So in your code:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( '', 'woocommerce' );
$address_fields['address_2']['label'] = __( 'Apt, Unit, Etc (optional)', 'woocommerce' );
$address_fields['address_2']['label_class'] = array(); // No label class
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related
Trying to add text (best inside a div) next to the title/label for a variation (i.e. SIZE) without editing the woocommerce page template.
Pseudocode:
function my_text_after_variation (){
if attribute pa_size exists {
echo label for pa_size;
echo <div> MY TEXT </div>;
}else{
return;
}};
Every help is highly welcome.
To display a custom text just after a specific attribute label name, you will use this a custom function hooked in woocommerce_attribute_label dedicated filter hook, this way:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3 );
function custom_attribute_label( $label, $name, $product ) {
$taxonomy = 'pa_'.$name;
if( $taxonomy == 'pa_size' )
$label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';
return $label;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
If you can not edit the woocommerce page template, try to use jQuery.
For example:
$('.pa_size label').after('<span class="some-text">some text</text>');
If you need to use variable text, you can create object with this variable using 'wp_localize_script' and get it from this object:
function.php
// Localize the script with your data
$data_array = array(
'some_text' => __( 'Some text to insert', 'my-domain' ),
);
wp_localize_script( 'some_handle', 'object_name', $data_array );
jQuery:
$('.pa_size label').after(object_name.some_text);
I was trying to add new column to the orders page of the customer's recent orders table, the new column should show the details of the product like product name & description.
Tried different solution, was fortunate to add column name but not able to add the value to the column.
Here is my code:
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns' );
function new_orders_columns( $columns = array() ) {
// Hide the columns
if( isset($columns['order-total']) ) {
unset( $columns['order-status'] );
unset( $columns['order-total'] );
unset( $columns['order-actions'] );
}
// Add new columns
//this is my custom column order details
$columns['order-detail'] = __( 'Details', 'woocommerce' );
$columns['order-status'] = __( 'Status', 'woocommerce' );
$columns['order-total'] = __( 'Total', 'woocommerce' );
$columns['order-actions'] = __( ' ');
return $columns;
}
Screenshot of the result:
You are very near…
1) Insert a new column in a specific location (another way):
add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-details'] = __( 'Details', 'woocommerce' ); // <== New column
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
return $ordered_columns;
}
Code goes in function.php file of your active child theme (or active theme).
tested and works.
2) Display data in this new column (editing myaccount/orders.php template):
You will need to override through your active child theme, the myaccount/orders.php WooCommerce template.
At line 56 (just after 'order-number' code) you will insert the following and you will be able to add some specific content:
<?php elseif ( 'order-details' === $column_id ) : ?>
<?php
// HERE GOES YOUR CUSTOM CODE (DISPLAYED CONTENT)
?>
You can use the available WC_Order object $order…
How to get WooCommerce order details
Accepted answer is only partially correct. Adding the column via filter is fine, but populating the column is better done via a action hook. This is clean and matches best with column being added via filter. While editing the orders.php page via child theme works, it will lead to future effort to maintain after updates to woocommerce change this page making child theme version out of date.
To add your content to the column create an action hook along side the filter in functions.php. (or better yet in a plugin!)
add_action( 'woocommerce_my_account_my_orders_column_order-detail' , 'your_function_name2',10,1 );
//'woocommerce_my_account_my_orders_column_<column_id>'
function your_function_name2( $order ) {
// do stuff, ex: get_post_meta( $post->ID, 'key', true );
}
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns',10,1 );
function new_orders_columns( $columns) {
// Add new column. Kept simple to play nice with other filters.
$columns['order-detail'] = __( 'Details', 'woocommerce' );
return $columns;
}
see this similar question, best answer by Misha
Try to use this code
<?php
// ------------------
// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error
function bbloomer_add_premium_support_endpoint() {
add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'bbloomer_add_premium_support_endpoint' );
// ------------------
// 2. Add new query var
function bbloomer_premium_support_query_vars( $vars ) {
$vars[] = 'premium-support';
return $vars;
}
add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
function bbloomer_add_premium_support_link_my_account( $items ) {
$items['premium-support'] = 'Premium Support';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );
// ------------------
// 4. Add content to the new endpoint
function bbloomer_premium_support_content() {
echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
echo do_shortcode( ' /* your shortcode here */ ' );
}
add_action( 'woocommerce_account_premium-support_endpoint', 'bbloomer_premium_support_content' );
I want to change the add to cart text on product archives on specific categories only. For example, on preorder category i want instead of add to cart text, to be Preorder. I don't know how to identify Preorder category in the below function.
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // < 2.1
function woo_archive_custom_cart_button_text() {
return __( 'Preorder', 'woocommerce' );
}
Update: add_to_cart_text hook is obsolete & deprecated. It is replaced in Woocommerce 3+ by woocommerce_product_add_to_cart_text filter hook.
It can be 2 different things (as your question is not so clear)…
1) To target products on a specific product category archive pages you should use the conditional function is_product_category() this way:
add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
// Only for a specific product category archive pages
if( is_product_category( array('preorder') ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
Code goes in function.php file of the active child theme (or active theme).
2) To target a specific product category on Woocommerce archives pages you will use has term() this way:
add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
// Only for a specific product category
if( has_term( array('preorder'), 'product_cat' ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
For single product pages you will use additionally this:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
// Only for a specific product category
if( has_term( array('preorder'), 'product_cat' ) )
$text = __( 'Preorder', 'woocommerce' );
return $text;
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
Note: All filter hooked functions needs to return the main argument if you set some conditions, so in this case the argument $text…
Related answer: Targeting product terms from a custom taxonomy in WooCommerce
Related docs: Woocommerce Conditional Tags
You can use has_term() with condition. Updated code is as below.
Solution- 1. Using filter add_to_cart_text
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );
function woo_archive_custom_cart_button_text() {
global $product;
if(has_term('your-special-category', 'product_cat', $product->get_id())){
$text = __( 'Preorder', 'woocommerce' );
}
return $text;
}
Solution- 2. Using filter woocommerce_product_add_to_cart_text
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );
function woo_archive_custom_cart_button_text() {
global $product;
if(has_term('your-special-category', 'product_cat', $product->get_id())){
$text = __( 'Preorder', 'woocommerce' );
}
return $text;
}
where your-special-category will your category for which you want to replace add to cart text.
I'm trying to change the fields placeholder of "billing address 2" in the checkout page, using the following code:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',9999 );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['placeholder']="dssfsd";
return $fields;
}
it changes only for a moment than return back to it's defualt value.
see the following video (10s):
https://www.youtube.com/watch?v=-qOZ67gFQ98
The only way to get this is using woocommerce_default_address_fields, but it will change both billing and shipping placeholder for Address 2 checkout fields:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
Now if you want to change only the Address 2 billing field placeholder you will:
Unset default Address 2 field placeholder (for both shipping and billing fields)
Customize only billing Address 2 field placeholder
Put back shipping Address 2 field placeholder
Here is the code on two hooked function:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
// Remove labels for "address 2" shipping fields
unset($address_fields['address_2']['placeholder']);
return $address_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields', 90, 1 );
function custom_override_checkout_fields( $fields ) {
// Add custom billing "address 2" label
$fields['billing']['billing_address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
// Put back shipping "address 2" label
$fields['shipping']['shipping_address_2']['placeholder'] = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );
return $fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
I'm trying to inject some text in my description ending.
Is it possible with filter?
Or do i need to do this via child theme?
Been trying to find the hook for description but can only find one for short description.
Example:
This is a description.
Just some sample text to fill the description out.
What i want is to inject "This is the last line in the description" So the hole description would look like this.
This is a description.
Just some sample text to fill the description out.
This is the last line in the description
The code i have for injecting text before short description is this:
add_filter( 'woocommerce_short_description', 'single_product_short_descriptions', 10, 1 );
function single_product_short_descriptions( $post_excerpt ){
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
You can use this custom function hooked in the_content filter hook this way:
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';
// Inserting the custom content at the end
$content .= $custom_content;
}
return $content;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Addition - Force product description when is empty (if you want this custom text to be displayed):
add_filter( 'woocommerce_product_tabs', 'force_description_product_tabs' );
function force_description_product_tabs( $tabs ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab',
);
return $tabs;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.