I have managed to change the "backorder allowed" text on catalog and product pages using this solution.
However, I would also like to change the "backorder allowed" text on cart page (and possibly on checkout, though I haven't seen it on checkout). The accepted solution for a similar question doesn't work (anymore).
Adding this to my functions.php worked for me:
/* --- Out of stock (Backorder) message ON CART PAGE --- */
function custom_cart_item_backorder_notification( $html, $product_id ){
$html = '<p class="backorder_notification">' . esc_html__( 'My custom text', 'storefront' ) . '</p>';
return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
Related
On my woocommerce site I want to add the text "Only available in stores" to all my product's short description, and also have that same text be default for all future products I add.
I had a search but the solutions seemed way to complex for me. Is there some code I can paste in the functions.php?
Thanks!
You can solve this problem easily. You need to some code push to theme functions.php or use code snippets plugin. This code only works when the WooCommerce product short description is empty.
function wp_woocommerce_short_description_if_empty(){
global $post;
if (empty($post->post_excerpt)) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'Your Custom Message Here.';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
add_action('woocommerce_single_product_summary', 'wp_woocommerce_short_description_if_empty', 21);
You can also try this for your default product description, you can Add Custom Text before/after the Product Short Description
add_filter( 'woocommerce_short_description', 'woo_add_text_after_excerpt_single_product', 20, 1 );
function woo_add_text_after_excerpt_single_product( $post_excerpt ){
/* Method 1: Add Custom Text before the Product Short Description on product page */
/* $content= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $content.'<br>'.$post_excerpt;
*/
/* Method 2: Add Custom Text after the Product Short Description on product page */
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $post_excerpt;
}
Note: Add Custom Text before the Product Short Description on the product page - code is commented so you can uncomment accordingly.
The following works for the Product page but it does not work for the Shop page.
<?php
add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
{
return '<span class="onsale">PUT YOUR TEXT</span>';
}
Please suggest modifications.
Thanks!
Use This
add_filter( 'woocommerce_sale_flash', 'wooc_custom_replace_sale_text' );
function wooc_custom_replace_sale_text( $html ) {
return str_replace( __( 'Sale!', 'woocommerce' ), __( 'Your Text', 'woocommerce' ), $html );
}
I tried your code and it works perfectly for the shop page as well. You may try increasing the priority or it can be a conflict with some other plugin or theme.
You may also check the following file to confirm it has applied the woocommerce_sale_flash filter
woocommerce\templates\loop\sale-flash.php
I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.
Example:
You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/"
When you click on the button, it adds "-sample" to the URL
"http://www.example.com/shop/product-1-sample/"
How can I achieve this?
Thanks
For woocommerce 3+ (only):
In woocommerce 3 you will use woocommerce_after_shop_loop_item action hook instead, as the hook woocommerce_after_add_to_cart_button will not work anymore.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = $product->get_permalink();
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Before woocommerce 3:
This is possible using hook woocommerce_after_add_to_cart_button to add your additional button on product pages, using this custom function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = get_permalink( get_the_id() );
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
This code goes on function.php file of your active child theme or theme.
This code is tested and fully functional.
Based on this: Add a button after add to cart and redirect it to some custom link in WooCommerce
And this: PHP - How to remove all specific characters at the end of a string?
It's been a long time since the original question, but here's a recent update that works for me, WooCommerce 6.3.1:
/* WooCommerce customization */
add_action( 'woocommerce_after_shop_loop_item', 'custom_select_link', 11 );
function custom_select_link() {
global $product;
// Custom "Select" button.
echo '<a class="custom-button" href="' . esc_url( get_permalink( $product->id ) ) . '"><button class="custom-button"> </button></a>';
}
This answer cites an answer in wordpress.stackexchange.
I am learning more and more php as i go along. I sorta get the gist of how php hooks work (actions, filters)
I was attempting to add a filter to my child functions page:
add_filter('woocommerce_booking_single_check_availability_text', 'wc_change_button_text');
function wc_change_button_text() {
return __( 'Add to cart', 'woocommerce' );
}
You could use WooCommerce filter hooks to change these, but I wouldn't recommend it because it'll take too long and create code bloat.
Here's what I would recommend: https://wordpress.org/plugins/woocommerce-multilingual/
I'm sure you may have already found a solution, but for anyone else that stumbles across this post, simply add this snippet to your child theme's functions file.
add_filter( 'woocommerce_loop_add_to_cart_link',
'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("Change Text Here", "woocommerce");
$button = '<a class="book-now button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Tested and works 5.4.1
I am using Wordpress and the Genesis framework for a site. I'm using a child theme (Ayoshop - not that it matters much) for the theme. I would like to customize the search results page by removing the 'post info' area where it shows the date, author, and 'leave a comment' link, and instead show the featured image for that post. The theme is using the search.php page from the Genesis theme, so I'm not really sure how to proceed in how to customize it.
Here is the code from the Genesis theme search.php:
add_action( 'genesis_before_loop', 'genesis_do_search_title' );
/**
* Echo the title with the search term.
*
* #since 1.9.0
*/
function genesis_do_search_title() {
$title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s %s</h1></div>', apply_filters( 'genesis_search_title_text', __( 'Search Results for:', 'genesis' ) ), get_search_query() );
echo apply_filters( 'genesis_search_title_output', $title ) . "\n";
}
genesis();
It actually did matter that it was the Ayoshop theme, there was a custom filter that was added in a file called theme-tweaks.php that removed the original post info and added a custom post info, so I needed to remove that custom action.
All of the changes were done in the functions.php file.
I made sure to remove the genesis_post_info, and then removed the custom action that Ayoshop added.
remove_action( 'genesis_before_post_content', 'genesis_post_info' );
remove_action( 'genesis_before_post_content', 'ayo_post_info' );
I then added an action to add the image to the post.
add_action ( 'genesis_before_post_content', 'jl_post_info' );
function jl_post_info()
if ( has_post_thumbnail() ) {
printf( '<div class="post-info">' . get_the_post_thumbnail() . '</div>');
}
}