WP get locale in plugin code - php

Hy i am trying to get current language inside of plugin code. I have try with get_locale() but it always give me just en_us. I have try to find solution on WordPress code references but did not found anything that work.
In question is plugin WooCommerce, file wc-cart-functions.php
There are lines:
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
and
$message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
I want to get this result:
if($language == 'hr') { $added_text = sprintf( _n( '%s je dodan u košaricu.', '%s su dodani u košaricu.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
} else { $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); }
if($language == 'hr') { $message = sprintf( '%s %s', esc_url( home_url().'/kosarica' ), esc_html__( 'Pogledaj košaricu', 'woocommerce' ), esc_html( $added_text ) );
} else { $message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); }
Normally, i would solve this by geting language from URL, but that website doesnt have language in URL.

If your using the polylang plugin, see this site: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/ .. you probably need to use pll_current_language() which should return current language.

<?php
$language = pll_current_language('slug');
if($language == 'hr') {
$added_text = sprintf( _n( '%s je dodan u košaricu.', '%s su dodani u košaricu.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );} else {
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); }
if($language == 'hr') {
$message = sprintf( '%s %s', esc_url( home_url().'/kosarica' ), esc_html__( 'Pogledaj košaricu', 'woocommerce' ), esc_html( $added_text ) );} else {
$message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); }

Related

WooCommerce 'if ( is_cart() )' in functions.php not working?

I've added the code below within my functions.php so I can change the WooCommerce standard 'Add to cart notice'.
The notice does change but the if ( is_cart() ) does not seem to work. It outputs FALSE on the cart page.
I must have overseen something..?
add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );
function yw_add_to_cart_message($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
if ( is_cart() ) {
$cart_link = '<div class="uk-width-medium-1-5 uk-text-right"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</div>';
} else {
$cart_link = '<div class="uk-width-medium-1-5 uk-text-right"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</div>';
}
$added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div>' . $cart_link . '</div>';
$message = sprintf( '%s', $added_text );
return $message;
}
You have to use get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' instead of is_cart():
add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );
function yw_add_to_cart_message($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
$cart_link = '<i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '';
} else {
$cart_link = '<i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '';
}
$added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div><div class="uk-width-medium-1-5 uk-text-right">' . $cart_link . '</div></div>';
$message = sprintf( '%s', $added_text );
return $message;
}
Related threads: Here and Here

Woocommerce return to category button in message

I'm trying to add a button so users can go back to the current category they are buying a product. In order to do this, i would like this button to appear once they buy a product, specifically in the message, so my code goes like this:
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$single_cat = array_shift( $product_cats );
$message = '<a class="button wc-forward" href="http://website/newstore/category/campamentos/' . $single_cat->slug . ' ">Continue Shopping</a>';
$message .= sprintf( '%s %s', esc_url( wc_get_page_permalink('cart') ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
It works good, but i can't get the continue shopping button to have the last part of the link assigned here >
$message = '<a class="button wc-forward" href="http://website/newstore/category/campamentos/' . $single_cat->slug . ' ">Continue Shopping</a>';
Guess is because i'm not using echo with $single_cat->slug??

Wordpress output message [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want the text before the button, does anyone one how?
heres the code
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n ( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$message = sprintf( '%s%s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
QUESTION #2
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '**DIV HERE** %s has been added to your cart.', '**DIV HERE** %s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
This should work. Try this
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
Question #2 Answer
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '<div class="acmsg">%s</div> %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '<div class="acmsg">%s</div> %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );

Using str_replace to change text within PHP template

I am trying to change some text on my WooCommerce-based WP Website.
Essentially, through a filter using the str_replace I'm trying to change the text '%s reviews for %s' to be something else.
This text is included in the single-product-reviews.php file.
See below:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
In order to do this I'm trying to use the following, but it doesn't seem to work. I'm not sure what string I should be targetting.
function lnz_replace_content()
{
echo str_replace("%s reviews for %s","%s comments about %s", $product);
}
add_filter('init','lnz_replace_content');'
I've had a go with gettext too but that doesn't seem to work either in this case.
OP Update
I've had a go at using gettext, but it doesn't seem to work in this case.
As previously mentioned, I'm targeting the following code (in single-product-review.php)
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
If I use 'gettext' to replace 'Reviews' it works fine. If I try and get it to replace '% review for %s' it doesn't work.
Any ideas why.
you can do this with 2 ways:
1) change in language file:
msgid "%s reviews for %s"
msgstr "%s comments about %s"
msgid "%s review for %s"
msgstr "%s comment about %s"
2) chang code:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
to
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s comment about %s', '%s comment about %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
You can use the template overrides to override single-product-reviews.php by copying it into your theme's woocommerce folder.
or you can filter gettext from your theme's functions.php
add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
/**
* Change comment form default field names.
*
* #link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
if ( is_singular() ) {
switch ( $translated_text ) {
case '%s reviews for %s' :
$translated_text = __( '%s comments for %s', 'theme_text_domain' );
break;
case 'Related Products' :
$translated_text = __( 'Related Opportunities', 'theme_text_domain' );
break;
}
}
return $translated_text;
}

Adding multiple buttons to a cart using php

I need two buttons to show up after a product is added to a cart on an e commerce site.Here is how the code reads now:
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
else :
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
endif;
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
I want both the "Continue Shopping" and View Cart buttons to show up.
So if I am understanding correctly, this would display the Cart and Continue shopping buttons together:
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
else :
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
endif;
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );

Categories