How to add custom text before price in Woocommerce - php

How can I add a custom text using a snippet to my Woocoomerce price in the category view and product page?
function sv_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'unit_price', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . ' / Stück</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_html', 10, 2 );

add_filter("wc_price","addtext",10,4);
function addtext($return, $price, $args, $unformatted_price){
if(is_product_category() || is_shop() || is_product()){
$return = 'like Preis:'.$return;
}
return $return;
}

Related

how to add text in front of price but it should visible only on product page - woocommerce [duplicate]

I am adding a price suffix on the WooCommerce single product page (and only there, not in the loop!).
I use the following:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if( is_product() ) {
$price = $price . ' <small>incl. tax</small>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
However, this also adds the price suffix to the Up-Sells on the product page (I am not talking about the related products, but the Up-Sells).
How can I exclude the price suffix for the Up-Sells?
I tried:
if( is_product() && !$woocommerce_loop['name'] == 'up-sells' )
But the suffix is still displayed for the Up-Sells.
In your code $woocommerce_loop is not defined
Instead of the compare, do the reverse and only apply it to an empty value
So you get:
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] == '' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
OR use
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] !== 'related' && $woocommerce_loop['name'] !== 'up-sells' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

Add price suffix only on WooCommerce single product page without linked products

I am adding a price suffix on the WooCommerce single product page (and only there, not in the loop!).
I use the following:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if( is_product() ) {
$price = $price . ' <small>incl. tax</small>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
However, this also adds the price suffix to the Up-Sells on the product page (I am not talking about the related products, but the Up-Sells).
How can I exclude the price suffix for the Up-Sells?
I tried:
if( is_product() && !$woocommerce_loop['name'] == 'up-sells' )
But the suffix is still displayed for the Up-Sells.
In your code $woocommerce_loop is not defined
Instead of the compare, do the reverse and only apply it to an empty value
So you get:
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] == '' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
OR use
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] !== 'related' && $woocommerce_loop['name'] !== 'up-sells' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

Display product categories and "brand" attribute term names under item name everywhere in WooCommerce

I want to display the category & attribute (brand) of each product under the product name in the Cart/checkout page
Example:
"Name of product"
"Category | Brand"
As shown in this image from shop
I would like to display it the same way on the cart page
(and also Thank you page + Order details, but these are lower priority)
I have this code which adds the Category to the cart page, but how can i add the attribute/brand next to it? Refer to this image
add_filter( 'woocommerce_cart_item_name', 'category_under_at_cart', 99, 3);
function category_under_at_cart( $name, $cart_item, $cart_item_key ) {
$product_item = $cart_item['data'];
// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
}
$cat_ids = $product_item->get_category_ids();
$attributes = $product_item->get_attributes();
// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( count( $cat_ids )) . ' ', ' | ','</span>');
return $name;
}
The following will display the formatted product category(ies) and product attribute "brand" term names on minicart, cart, checkout, customer order and email notifications:
// Custom funtion that return the formatted category(ies) and attribute 'brand' term names
function get_categories_and_brand_html( $product_id ){
$product = wc_get_product($product_id);
$cat_names = (array) wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );
$brand_name = $product->get_attribute('brand');
$output = '';
if ( ! empty($cat_names) || ! empty($brand_name) ) {
$output .= '</br><span class="posted_in">';
if ( ! empty($cat_names) ) {
$output .= implode(', ', $cat_names);
}
if ( ! empty($cat_names) && ! empty($brand_name) ) {
$output .= ' | ';
}
if ( ! empty($brand_name) ) {
$output .= $brand_name;
}
$output .= '</span>';
}
return $output;
}
// Display term names in minicart and cart page
add_filter( 'woocommerce_cart_item_name', 'category_brand_after_cart_item_name', 100, 3 );
function category_brand_after_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$terms_html = get_categories_and_brand_html( $cart_item['product_id'] );
if ( ! is_checkout() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
// Display term names in checkout page
add_filter( 'woocommerce_checkout_cart_item_quantity', 'category_brand_after_checkout_item_name', 100, 3 );
function category_brand_after_checkout_item_name( $quantity, $cart_item, $cart_item_key ) {
$terms_html = get_categories_and_brand_html( $cart_item['product_id'] );
if ( is_checkout() && ! empty($terms_html) ) {
$quantity .= $terms_html;
}
return $quantity;
}
// Display term names on customer orders
add_filter( 'woocommerce_order_item_quantity_html', 'category_brand_after_order_item_name', 100, 2 );
function category_brand_after_order_item_name( $item_name, $item ) {
$terms_html = get_categories_and_brand_html( $item->get_product_id() );
if ( is_wc_endpoint_url() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
// Display term names on email notifications
add_filter( 'woocommerce_order_item_name', 'category_brand_after_email_item_name', 100, 3 );
function category_brand_after_email_item_name( $item_name, $item, $is_visible ) {
$terms_html = get_categories_and_brand_html( $item->get_product_id() );
if ( ! is_wc_endpoint_url() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Display a custom product price in woocommerce archive page and for upsells/related products

Is there a way to enable a backend field in the WordPress dashboard to show a custom price per product in the archive page and for upsells/related products but leave the price in product-summary?
Example: Product A has a price of 10€ but I would like to show "from 6€/kg" instead.
The easiest way would be to use a custom field which is overriding the woocommerce_template_loop_price but this code doesn't work but I don't understand why.
add_action('woocommerce_template_loop_price', 'shopprice_change', 10, 2);
function shopprice_change ($price, $product) {
global $post, $blog_id;
$post_id = $post->ID;
$price = get_post_meta($post_id, 'shoppricechange', true);
return $price;
wp_reset_query();
}
UPDATE
I've found a solution for changing the price in archive page without changing on single product page:
function cw_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'shoppricechange', true );
if (is_product()) return $price_html;
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
Problem: Actually the upsells and related products should change as well on the single product page. But with if (is_product()) return $price_html; its excluding them as well. Who helps solving this will get the bounty. Thank you!
But with if (is_product()) return $price_html; its excluding them as
well.
Using this in place of the is_product() works well for me:
is_single( $product->get_id() )
And you shouldn't call $product->id directly. Instead, use $product->get_id().
Here's the full code I used:
function cw_change_product_html( $price_html, $product ) {
if ( is_single( $product->get_id() ) )
return $price_html;
$unit_price = get_post_meta( $product->get_id(), 'shoppricechange', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
woocommerce_template_loop_price is not a action it's a function
Use the code below
function cw_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'shoppricechange', true );
$returnedSignleProdctP =false;
$trace = debug_backtrace();
$callstack = (array) $trace;
foreach ($callstack as $key => $value) {
if(isset($value['function']) && $value['function'] == 'woocommerce_output_related_products' ){
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
}
if(isset($value['function']) && $value['function'] == 'woocommerce_template_single_price' ){
$price_html = $price_html;
}
if(isset($value['function']) && $value['function'] == 'woocommerce_template_loop_price' ){
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
}
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
Maybe this plugin can help, Woocommerce Extra Price Fields (Source: https://wordpress.org/plugins/woocommerce-extra-price-fields/)

Display parent grouped product name in Woocommerce orders details

For Woocommerce grouped products I am displaying parent product name in cart and checkout pages using the code bellow:
// Adding the grouped product ID custom hidden field data in Cart object
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['add-to-cart'] ) && $product_id != $_REQUEST['add-to-cart'] ) {
$cart_item_data['custom_data']['grouped_product_id'] = $_REQUEST['add-to-cart'];
$data['grouped_product_id'] = $_REQUEST['add-to-cart'];
// below statement make sure every add to cart action as unique line item
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 10, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
// Only in cart and checkout pages
if ( is_cart() || is_checkout() )
{
// The product object from cart item
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
// The parent product name and data
if( ! empty( $cart_item['custom_data']['grouped_product_id'] ) ){
$group_prod_id = $cart_item['custom_data']['grouped_product_id'];
$group_prod = wc_get_product($group_prod_id);
if ( ! $group_prod->is_type( 'grouped' ) ) return $cart_item_name;
$parent_product_name = $group_prod->get_name();
$group_prod_permalink = $group_prod->is_visible() ? $group_prod->get_permalink() : '';
if ( ! $product_permalink )
return $parent_product_name . ' > ' . $product->get_name();
else
return sprintf( '%s > %s', esc_url( $group_prod_permalink ), $parent_product_name, esc_url( $product_permalink ), $product->get_name() );
}
else
return $cart_item_name;
}
else
return $cart_item_name;
}
Code comes from here: Add the parent product name to each cart item names in WooCommerce
Now I would like to display also this parent product name in orders details and in back-end too.
I will be grateful for any help on this.
I have revisited the original code answer a bit and I have enabled the display of those parent products linked names on orders and email notifications:
// Adding the grouped product ID custom hidden field data in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 20, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty($_REQUEST['add-to-cart']) && $product_id != $_REQUEST['add-to-cart']
&& is_numeric($_REQUEST['add-to-cart']) ){
$group_prod = wc_get_product($_REQUEST['add-to-cart']);
if ( ! $group_prod->is_type( 'grouped' ) )
return $cart_item_data; // Exit
$cart_item_data['grouped_product'] = array(
'id' => $_REQUEST['add-to-cart'],
'name' => $group_prod->get_name(),
'link' => $group_prod->get_permalink(),
'visible' => $group_prod->is_visible(),
);
// Below statement make sure every add to cart action as unique line item
$cart_item_data['grouped_product']['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 20, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
// The product object from cart item
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
// The parent product name and data
if( isset( $cart_item['grouped_product'] ) ){
$group_product = $cart_item['grouped_product'];
$group_prod_link = $product->is_visible() && is_cart() ? $group_product['link'] : '';
if ( ! $group_prod_link )
return $group_product['name'] . ' > ' . $product->get_name();
else
return sprintf(
'%s > %s',
esc_url( $group_prod_link ),
$group_product['name'],
esc_url( $product_permalink ),
$product->get_name()
);
}
else
return $cart_item_name;
}
// Save grouped product data in order item meta
add_action( 'woocommerce_checkout_create_order_line_item', 'added_grouped_order_item_meta', 20, 4 );
function added_grouped_order_item_meta( $item, $cart_item_key, $values, $order ) {
if( isset($values['grouped_product']) ){
$item_id = $item->get_id();
$grouped_data = $values['grouped_product'];
unset($grouped_data['unique_key']);
$item->update_meta_data( '_grouped_product', $grouped_data );
}
}
// Display grouped product linked names in order items (+ email notifications)
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 20, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
$product = $item->get_product();
$product_id = $item->get_product_id();
$product_permalink = $is_visible ? $product->get_permalink( $item ) : '';
$grouped_data = wc_get_order_item_meta( $item->get_id(), '_grouped_product', true );
if( empty($grouped_data) ){
$item_name = $product_permalink ? sprintf(
'%s',
esc_url( $product_permalink),
$item->get_name()
) : $item->get_name();
} else {
$item_name = $product_permalink ? sprintf(
'%s > %s',
$grouped_data['link'],
$grouped_data['name'],
esc_url( $product_permalink) ,
$item->get_name()
) : $grouped_data['name'] . ' > ' . $item->get_name();
}
return $item_name;
}
// Display on backend order edit pages
add_action( 'woocommerce_before_order_itemmeta', 'backend_order_item_name_grouped', 20, 3 );
function backend_order_item_name_grouped( $item_id, $item, $product ){
if( ! ( is_admin() && $item->is_type('line_item') ) ) return;
$grouped_data = wc_get_order_item_meta( $item_id, '_grouped_product', true );
if( empty($grouped_data) ) return;
$product_link = admin_url( 'post.php?post=' . $grouped_data['id'] . '&action=edit' );
$grouped_name_html = '' . esc_html( $grouped_data['name'] ) . '';
echo '<br><br><div class="wc-order-item-name">
<small><strong>'.__('Grouped parent').':</strong></small><br>
' . $grouped_name_html . '
</div>';
}
Code goes in function.php file of your active child theme (or theme);
Tested and works.

Categories