Issue changing WooCommerce coupon label on mobile - wordpress

I am trying to change the coupon name/label in the WooCommerce cart and checkout tables with:
add_filter( 'woocommerce_cart_totals_coupon_label', 'custom_woocommerce_cart_totals_coupon_label', 10, 2 );
function custom_woocommerce_cart_totals_coupon_label( $label, $coupon ) {
if ( $coupon->get_code() !== 'coupon1' ) {
return 'Rabattcode'. strtoupper( $coupon->code );
}
return $label;
}
Now I need to add a -class around the actual coupon code name, so strtoupper( $coupon->code ) should be wrapped inside <small></small>. How can I do this?
I tried
return 'Rabattcode'. '<small>' . strtoupper( $coupon->code ) . '</small>';
but this does not work on mobile screens, there the actual HTML is displayed like: Rabattcode<small>COUPON2</small>

Related

Change frontend text for “backorder allowed”

WooCommerce has a product option to “allow backorder but inform customer” which shows a notice on the frontend product page. How can I change the text of this notice?
I have seen this helpful post https://storepro.io/learn/how-to-change-out-of-stock-text-in-woocommerce/ sharing how to change "out of stock" text, but that is different from the "backorder allowed" text.
Code snippet for function.php from the above linked page:
add_filter('woocommerce_get_availability_text', 'themeprefix_change_soldout', 10, 2 );
/* Change Sold Out Text to Something Else */
function themeprefix_change_soldout ( $text, $product) {
if ( !$product->is_in_stock() ) {
$text = '<div class="">My custom sold out message.</div>';
}
return $text;
}
You can copy the code from original source code for the backorder validation and rewrite the status text and return it from the function.
Here is the code:
function vh_wc_custom_get_availability_text( $availability, $_product ) {
if ( $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = $_product->backorders_require_notification() ? __( 'Your new text goes here', 'your-child-theme-text-domain' ) : '';
} elseif ( ! $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = __( 'Your new text goes here', 'your-child-theme-text-domain' );
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'vh_wc_custom_get_availability_text', 10, 2 );
You can check this page for more detail on woocommerce_get_availability_text filter hook.

Prevent WooCommerce coupon stacking on cart and checkout page

I need to prevent two specific coupons from being used together. I successfully implemented this code, which prevents stacking these coupons on the cart page:
add_action( 'woocommerce_before_cart', 'check_coupon_stack' );
function check_coupon_stack() {
$coupon_code_1 = 'mycode1';
$coupon_code_2 = 'mycode2';
if ( WC()->cart->has_discount( $coupon_code1 ) && WC()->cart->has_discount( $coupon_code2) ) {
WC()->cart->remove_coupon( $coupon_code2 );
$notice_text = 'Discount code '.$coupon_code1.' cannot be combined with code '.$coupon_code2.'. Code '.$coupon_code2.' removed.';
wc_print_notice( $notice_text, 'error' );
wc_clear_notices();
}
}
However, this does not prevent stacking on the checkout page, which follows the cart page.
I have tried simply adding:
add_action( 'woocommerce_before_checkout_form', 'check_coupon_stack' );
But that doesn't make this work on the checkout page. What more is needed?
WooCommerce contains multiple hooks that apply to coupons, woocommerce_applied_coupon is one of them, which is very suitable for your question.
Furthermore, your current code only works in one direction, which is when $coupon_code_1 is used, $coupon_code_2 is removed. However, this is not applied in the reverse direction while you indicate in your question that you want to prevent two specific coupons from being used together.
This is taken into account in my answer, so you get:
function action_woocommerce_applied_coupon( $coupon_code ) {
// Settings
$coupon_code_1 = 'coupon1';
$coupon_code_2 = 'coupon2';
// Initialize
$combined = array( $coupon_code_1, $coupon_code_2 );
// Checks if coupon code exists in an array
if ( in_array( $coupon_code, $combined ) ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// Computes the difference of arrays
$difference = array_diff( $combined, $applied_coupons );
// When empty
if ( empty( $difference ) ) {
// Shorthand if/else - Get correct coupon to remove
$remove_coupon = $coupon_code == $coupon_code_1 ? $remove_coupon = $coupon_code_2 : $remove_coupon = $coupon_code_1;
// Remove coupon
WC()->cart->remove_coupon( $remove_coupon );
// Clear Notices
wc_clear_notices();
// Error message
$error = sprintf( __( 'Discount code "%1$s" cannot be combined with code "%2$s". Code "%2$s" removed.', 'woocommerce' ), $coupon_code, $remove_coupon );
// Show error
wc_print_notice( $error, 'error' );
}
}
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );

Anyway to change the Total title from the WooCommerce order received page's order overview

Is there a hook to change the Total title from the WooCommerce order received page's order overview? Please see the below image to understand better
Yes, you can use the gettext WordPress filter to "translate" (rename in your case) that string of text.
Your string is inside the thankyou.php WooCommerce template:
esc_html_e( 'Total:', 'woocommerce' );
Based on WooCommerce: How to Translate / Rename Any String tutorial, the right code should be the following:
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 9999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Total:':
$translated = 'Whatever:';
break;
}
}
return $translated;
}

Show the name of the chosen variation under the product title on the My Account > Downloads page

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.

WooCommerce Stock number display

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;
}

Resources