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.
Related
I use the below to add custom html or a text in WooCommerce single product pages, after short description:
function my_content( $content ) {
$content .= '<div class="custom_content">Custom content!</div>';
return $content;
}
add_filter('woocommerce_short_description', 'my_content', 10, 2);
How to restrict this on product pages that contain a specific product tag (not a product category)?
You can use has_term() WordPress conditional function, to restrict your custom content to be displayed only for products that have specific product tag(s) as follows (defining below in the function your product tag(s) term(s)):
add_filter('woocommerce_short_description', 'my_content', 10, 2);
function my_content( $content ) {
// Here define your specific product tag terms (can be Ids, slugs or names)
$product_tags = array( 'Garden', 'Kitchen' );
if( has_term( $product_tags, 'product_tag', get_the_ID() ) ) {
$content .= '<div class="custom_content">' . __("Custom text!", "") . '</div>';
}
return $content;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
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'm trying to find a way to place custom text before availability info (out of stock, in stock etc.) on woocommerce single product page.
I am using something like that:
add_filter( 'woocommerce_get_availability', 'change_product_availability_display' );
add_filter( 'unknown filter', 'change_product_availability_display' );
function change_product_availability_display( $availability ) {
// Additional text
$text = __('Availability:');
// returning the text before the availability
return $text . ' ' . $availability;
}
Its's based on: Add a custom text before the price display in WooCommerce.
In case of price filter named in my code "unknown filter" was woocommerce_cart_item_price. I've looked for this kind of filter for availability/stock item, but can't find it.
Maybe someone could review this code and help me to find this "unknown_filter" or have other idea how I can put custom text before availability info?
I have revisited your code as your are trying to merge a string with an array. Also I have added a 2nd hooked function that will allow to display your "labeled" availability to items in cart and checkout.
The code:
add_filter( 'woocommerce_get_availability', 'add_label_to_availability_display' );
function add_label_to_availability_display( $availability ) {
if( is_product() || is_cart() || is_checkout() ){
$label = __( 'Availability', 'woocommerce' ) . ': ';
$availability['availability'] = $label . $availability['availability'];
}
return $availability;
}
add_filter( 'woocommerce_cart_item_name', 'add_availability_below_cart_item_name', 10, 3);
function add_availability_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$availability = $cart_item['data']->get_availability();
return $item_name . '<br>' . $availability['availability'];
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
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 pasted this code in function.php, but it didn't rename the product page tab as it supposed to (http://www.noushasasart.com/product/harsh-bark/)
function woo_remove_product_tabs($tabs) {
unset($tabs['reviews']); // Remove the reviews tab
$tabs['description']['title'] = __('Additional Information'); // Rename the
description tab
return $tabs;
}
how can I solve this?
You have forgotten the filter hook:
add_filter( 'woocommerce_product_tabs', 'woo_customize_tabs', 100, 1 );
function woo_customize_tabs( $tabs ) {
unset($tabs['reviews']); // Remove the reviews tab
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
return $tabs;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works.
Update (related to your comment) | Rename product description heading (inside the tab):
To rename description heading use woocommerce_product_description_heading filter hook this way:
add_filter( 'woocommerce_product_description_heading', 'rename_product_description_heading', 10, 1 );
function rename_product_description_heading( $heading ) {
return __( 'Additional Information', 'woocommerce' );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works.
Official related documentation: Editing product data tabs