I want to add a product short description by default whenever is the new product is being created. All the products will have the same short description, so there is no point keep copying and pasting it. So it should just be there when I click on the add a new product.
I would appreciate any help.
add_filter( 'woocommerce_short_description',
'single_product_short_description', 10, 1 );
function single_product_short_description( $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;
}
I found the above code but couldn't get it to work :(
Thank you.
Regards,
Emre.
Add this code inside your themes function.php
Change the short description content as per your need - "Here goes your short desc."
add_filter( 'wp_insert_post_data' , 'cdx_add_product_short_desc' , '99', 1 );
function cdx_add_product_short_desc( $data )
{
//only for product post type
if($data['post_type'] == 'product' ) {
//only if short description is not present
if( '' == trim($data['post_excerpt']) ):
$short_desc = 'Here goes your short desc.';
$data['post_excerpt'] = $short_desc ;
endif;
}
// Returns the modified data.
return $data;
}
This will work to automatically override anything put into a product's short description field on the front-end only. It will not add the text to the backend field itself, which is good because it keeps it globalized if you need to change it later.
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
function filter_woocommerce_short_description( $post_excerpt ) {
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}
Related
I have code to changes the custom message on all product with 2 different messages. Please take a look.
add_filter('woocommerce_empty_price_html', 'show_alert_info_if_no_price');
function show_alert_info_if_no_price ($product){
if (is_product()) {
global $product;
$price = $product->get_price();
if ($price == '') {
ob_start();
// return for the product page
return '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
} else {
// otherwise return short text as kontak kami
return 'Contact us';
}
}
}
Now I have a problem on related product. I need the related product price will appear like :
//short text as kontak kami
Now I am stuck how to add another code when using hooked.
$woocommerce_loop['name'] != 'related').
any help will appreciated!
There is an mistake in your code, because this hook is only executed for empty prices. So going to check in the hook again for an empty price and if it isn't, running an else condition is useless.
To display a different text on the single product page for related products you can use global $woocommerce_loop
So you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
global $woocommerce_loop;
// True on a single product page
if ( is_product() ) {
$html = '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
// Related
if ( $woocommerce_loop['name'] == 'related' ) {
$html = __( 'Some other text', 'woocommerce' );
}
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
By default WooCommerce shows the attribute of a variable product in the title and I'm using this code to show the attribute below the title in the cart and checkout pages:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
But that doesn't work in My Account page, users see the full product name with no attribute.
To fix it I'm using the code below to show the attribute in the product title:
function show_attributes_outside_title_1( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_2( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
But I'd like to show the attribute below the title (or a new column), it's easier to read and goes with the same desing you see in the cart and checkout pages.
There is some confusion in the initial part of the question.
You say you want to show the attribute under the product title on the cart and checkout page but then return __return_false, do you intend to do the opposite?
SOLUTION #1
You may want to reverse the check to make sure that your chosen product variation attribute is shown under the product name on your account page under Downloads (as evidenced by your comment above):
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_1( $enabled ) {
if ( is_account_page() ) {
$enabled = true;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
function show_attributes_outside_title_2( $enabled ) {
if ( ! is_account_page() ) {
$enabled = false;
}
return $enabled;
}
SOLUTION #2
If you want to leave the code in your question unchanged you can use the woocommerce_account_downloads_column_download-product hook where download-product is the id of the product name column (in the /my-account/downloads/ page). Here the documentation.
Finally, with the wc_get_formatted_variation function you can get the name of the chosen variation. For more information on parameters read the documentation.
// shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the produc
$product_name = $download['product_name'];
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '';
} else {
echo esc_html( $product_name );
}
}
The code has been tested and works. Add it to your active theme's functions.php.
I changed Vincenzo's answer a bit to make it look the same way I see the attributes in my Cart and Checkout pages. Here's the code in case anybody else needs it:
// Shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the product
$product_name = $download['product_name'];
// define variable
$product_attributes = '';
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name();
$product_attributes = wc_get_formatted_variation( $product, true, true, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
} else {
echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
}
}
// Shows variation outside title in cart and checkout pages
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
The last two filters replace my code and the first part solves the issue in the Downloads page.
I've been trying to get rid of our exact stock numbers on product pages on our wordpress/woocommerce website. Ideally it shows In stock, Only 1 or 2 left in stock when theres only 1 or 2 available, or just sold out. I've tried many of the code snippets found here on stack overflow but none of them seem to change anything. Also the standard woocommerce settings at Products > Inventory to don't show any stock numbers doesn't do anything. On the front end it keeps showing Availability: Exact stock amount. Can anyone help me out?
Adding these kind of snippets to my child theme's functions.php or even in a code snippet plugin doesn't seem to do anything for me:
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}
A better way is to use woocommerce_get_availability_text instead of woocommerce_get_availability. This filters only the availability text and is called after woocommerce_get_availability.
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text', 10, 2 );
function set_custom_availability_text( $availability, $product ) {
if ( $product->is_in_stock() ) {
$availability = __( 'Available!', 'woocommerce' );
} elseif ( ! $product->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
}
return $availability;
}
If the above code doesn't do anything, test if the filter is being called by using the die() function. This will stop the execution of any code that comes after it and prints whatever string you put in between the parentheses:
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text_test', 10, 2 );
function set_custom_availability_text_test( $availability, $product ) {
die('Filter is called');
}
So if the filter is called correctly your page should render up to the availabitly text and display 'Filter is called' where the availabilty text should be. If nothing happens this means the filter isn't called. So then check if your functions.php is being loaded, or use a plugin like Code Snippets.
This snippet help you to change text for all product or particular one:
<?php
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability',1,2);
function wcs_custom_get_availability($availability,$_product ) {
global $product;
switch($_product->slug){
// Enter product slug for case
case "test-1":
// text for in stock mode
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
// text for out of stock mode
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Enter your custom text here', 'woocommerce');
}
break;
case "test2":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
case "test3":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity نفر",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
}
return $availability;
}
I have a wordpress website with customs posts and metas boxes.
One of these manage some pictures and I want to force the title of the post.
It should be the same as the name of the media uploaded.
Is it possible ? And how ?
Thank's
Manipulate the post title with a filter.
function change_post_title( $title, $id = null ) {
if ( is_single() ) {
$title = 'new title';
}
return $title;
}
add_filter( 'the_title', 'change_post_title', 10, 2 );
Thanks all for your help.
I have used a filter but this one :
function modify_post_title($data){
if($data['post_type'] == 'fcfm_photos' && isset($_POST['image'])) {
$data['post_title'] = substr($_POST['image'], -(strpos(strrev($_POST['image']),'/')));
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 );
And when I save the post, the post title change automatically. This is what i want.
I want to add the product link in the Processing Order email which User receives when order. When an order is placed an Order Email sent, I want to get the product link there when user clicks redirects to the detail product page when clicked. Is there any way, I get the product link or the Product Title would Hyperlink.
THANKS
The answer from gunbunnysoulja works great but needs two little updates:
get_product needs to be wc_get_product
$_product->id needs to be $_product->get_id()
The updated answer is as follows:
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$link = get_permalink( $_product->get_id() );
return ''. $item_name .'';
}
I am currently using this solution, which I found in the comments on another page. This is not my code.
http://www.vanbodevelops.com/tutorials/add-a-link-back-to-the-order-in-woocommerce-new-order-notifications-email#comment-636
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$link = get_permalink( $_product->id );
return ''. $item_name .'';
}
To link a product name with its product page in your order emails, open the functions.php file of your child theme and add the following snippet of code:
* Product Links in WooCommerce Order Emails
*/
add_filter('woocommerce_order_item_name', 'woocommerce_order_item_link', 10, 3);
function woocommerce_order_item_link( $item_name, $item, $bool ) {
$url = get_permalink( $item['product_id'] ) ;
return ''.$item_name .'';
}
I have been wondering how this works as well. There is little info available anywhere - at least not much with detailed step by step instructions.
The best solution I have came up with is to edit the customer-processing-order.php.
All I did was open it up in a text editor and added a few lines of text to:
"Your order has been received and is now being processed. Your order details are shown below for your reference. Please visit "http://www.youlinkurl".
The end user will have to copy and paste that link unfortunately but at least it works.