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

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

Related

Getting current active product filter

There is a widget called Active Product Filter in WooCommerce, I couldn't find a way to override it's designed so I tried making my own;
function abc_active_filter(){
$queryData = array();
parse_str($_SERVER['QUERY_STRING'], $queryData);
$active_filter = '';
foreach($queryData as $key => $value){
$active_filter = $active_filter.'<div>CLIK TO REMOVE - <span>'.$key.'</span><div>';
}
if(sizeof($queryData) > 0){
$active_filter = '<h3>Active Filter</h3>'.$active_filter;
}
return $active_filter;
}
add_shortcode('csx_active_filter', 'abc_active_filter');
This will output the query key of the current URL, for example;
min_price,
filter_size
whereas using the widget Active Product Filter the output is;
Min Price $589
Large
is there a way to achieve the output the same as the Active Product Filter? I found this the same question with me but the accepted answer is far different from what I'm trying to achieve and the one who asked the question is only looking for active filters by checking the query key.
Anyway, with the following code, I can achieve the same output of Active Product Filter;
if($key === 'min_price'){
$active_filter = $active_filter.'<div>CLIK TO REMOVE - <span>Min Price '.$value.'</span><div>';
}else if($key === 'filter_size'){
$active_filter = $active_filter.'<div>CLIK TO REMOVE - <span>'.$value.'</span><div>';
}else if .... and so on...
The problem is, filters are dynamic and I don't know all of them and it will take a lot of lines of codes to put them in conditional statements.
You can try using WC_Query::get_layered_nav_chosen_attributes();, simply print its value and see if the result is what you need.
print_r(WC_Query::get_layered_nav_chosen_attributes());
Otherwise, I wouldn't recommend to reinvent the wheel. You can override the widget by extending the class of WC_Widget_Layered_Nav_Filters.
In your functions.php, insert the below code. You can change its layout, elements depending on what design you wanted.
class Your_New_WC_Widget_Layered_Nav_Filters extends WC_Widget_Layered_Nav_Filters {
/**
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_layered_nav_filters';
$this->widget_description = __( 'Display a list of active product filters.', 'woocommerce' );
$this->widget_id = 'woocommerce_layered_nav_filters';
$this->widget_name = __( 'Active Product Filters', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'The Active filters', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' ),
),
);
parent::__construct();
}
/**
* Output widget.
*
* #see WP_Widget
* #param array $args Arguments.
* #param array $instance Widget instance.
*/
public function widget( $args, $instance ) {
if ( ! is_shop() && ! is_product_taxonomy() ) {
return;
}
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : 0; // WPCS: input var ok, CSRF ok.
$max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : 0; // WPCS: input var ok, CSRF ok.
$rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) ) : array(); // WPCS: sanitization ok, input var ok, CSRF ok.
$base_link = $this->get_current_page_url();
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || ! empty( $rating_filter ) ) {
$this->widget_start( $args, $instance );
echo '<ul>';
// Attributes.
if ( ! empty( $_chosen_attributes ) ) {
foreach ( $_chosen_attributes as $taxonomy => $data ) {
foreach ( $data['terms'] as $term_slug ) {
$term = get_term_by( 'slug', $term_slug, $taxonomy );
if ( ! $term ) {
continue;
}
$filter_name = 'filter_' . wc_attribute_taxonomy_slug( $taxonomy );
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array(); // WPCS: input var ok, CSRF ok.
$current_filter = array_map( 'sanitize_title', $current_filter );
$new_filter = array_diff( $current_filter, array( $term_slug ) );
$link = remove_query_arg( array( 'add-to-cart', $filter_name ), $base_link );
if ( count( $new_filter ) > 0 ) {
$link = add_query_arg( $filter_name, implode( ',', $new_filter ), $link );
}
echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>';
}
}
}
if ( $min_price ) {
$link = remove_query_arg( 'min_price', $base_link );
/* translators: %s: minimum price */
echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Min %s', 'woocommerce' ), wc_price( $min_price ) ) . '</a></li>'; // WPCS: XSS ok.
}
if ( $max_price ) {
$link = remove_query_arg( 'max_price', $base_link );
/* translators: %s: maximum price */
echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Max %s', 'woocommerce' ), wc_price( $max_price ) ) . '</a></li>'; // WPCS: XSS ok.
}
if ( ! empty( $rating_filter ) ) {
foreach ( $rating_filter as $rating ) {
$link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) );
$link = $link_ratings ? add_query_arg( 'rating_filter', $link_ratings ) : remove_query_arg( 'rating_filter', $base_link );
/* translators: %s: rating */
echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( esc_html__( 'Rated %s out of 5', 'woocommerce' ), esc_html( $rating ) ) . '</a></li>';
}
}
echo '</ul>';
$this->widget_end( $args );
}
}
}
Now, in your functions.php, insert the below code again. This will unregister the default widget and replace with the new one above where your custom design or layout exist.
function your_function_for_overriding_widgets() {
unregister_widget( 'WC_Widget_Layered_Nav_Filters' ); //unregistered the default widget
register_widget( 'Your_New_WC_Widget_Layered_Nav_Filters' ); //register your new widget
}
add_action( 'widgets_init', 'your_function_for_overriding_widgets' );

Show woocommerce product custom fields under the cart and order item name

I have a code that shows custom fields in the "General" tab on the product edit page.
After the manager has filled these fields, the data is displayed on the Archive/Category pages and in the Single Product page.
Also, these fields are in the cart and on the checkout page.
Here is my code:
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Custom Weight Field
woocommerce_wp_text_input(
array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
// Calories Field
woocommerce_wp_text_input(
array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'false',
'description' => __( '', 'woocommerce' )
)
);
// Ingredients Field
woocommerce_wp_textarea_input(
array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
// Hook callback function to save custom fields information
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Custom Weight Field
$custom_weight = $_POST['_custom_weight'];
if( ! empty( $custom_weight ) ) {
update_post_meta( $post_id, '_custom_weight', esc_attr( $custom_weight ) );
} else {
delete_post_meta( $post_id, '_custom_weight' );
}
// Save Calories Field
$сalories = $_POST['_сalories'];
if( ! empty( $сalories ) ) {
update_post_meta( $post_id, '_сalories', esc_attr( $сalories ) );
} else {
delete_post_meta( $post_id, '_сalories' );
}
// Save Ingredients Field
$ingredients = $_POST['_ingredients'];
if( ! empty( $ingredients ) ) {
update_post_meta( $post_id, '_ingredients', esc_html( $ingredients ) );
} else {
delete_post_meta( $post_id, '_ingredients' );
}
}
// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
$custom_weight = get_post_meta( $product->get_id(),'_custom_weight', true );
if( ! empty( $custom_weight ) )
echo '<p id="value-on-single-product">' . 'Weight: ' . $custom_weight . 'g' . '</p>';
$сalories = get_post_meta( $product->get_id(),'_сalories', true );
if( ! empty( $сalories ) )
echo '<p id="value-on-single-product">' . 'Calories: ' . $сalories . ' kcal.' . '</p>';
$ingredients = get_post_meta( $product->get_id(),'_ingredients', true );
if( ! empty( $ingredients ) )
echo '<p id="value-on-single-product">' . 'Ingredients: ' . $ingredients . '</p>';
}
// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'woocom_custom_fields_cart', 10, 2 );
function woocom_custom_fields_cart( $cart_data, $cart_item )
{
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
// Get the product ID
$product_id = $cart_item['product_id'];
if( $custom_field_value = get_post_meta( $product_id, '_custom_weight', true ) )
$custom_items[] = array(
'name' => __( 'Weight', 'woocommerce' ),
'value' => $custom_field_value,
'display' => $custom_field_value . 'g',
);
return $custom_items;
}
But unfortunately, it’s impossible for me to add these custom fields after the product name on the Thank You page, in the E-mail and on the order editing page.
I also have doubts whether the above code is correct, although everything works.
I will be glad for your help!
I have revisited your code and made some changes, adding the necessary code to display your custom fields data on cart, checkout, order received, my account order view and on email notifications (for Woocommerce 3+):
// Backend: Display additional product fields
add_action( 'woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data' );
function add_fields_to_options_general_product_data() {
// Custom Weight Field
woocommerce_wp_text_input( array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
));
// Calories Field
woocommerce_wp_text_input( array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
));
// Ingredients Field
woocommerce_wp_textarea_input( array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
));
}
// Backend: Save the data value from the custom fields
add_action( 'woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values' );
function save_admin_product_custom_fields_values( $product ) {
// Save Custom Weight Field
if( isset( $_POST['_custom_weight'] ) ) {
$product->update_meta_data( '_custom_weight', sanitize_text_field( $_POST['_custom_weight'] ) );
}
// Save Calories Field
if( isset( $_POST['_сalories'] ) ) {
$product->update_meta_data( '_сalories', sanitize_text_field( $_POST['_сalories'] ) );
}
// Save Ingredients Field
if( isset( $_POST['_ingredients'] ) ) {
$product->update_meta_data( '_ingredients', sanitize_textarea_field( $_POST['_ingredients'] ) );
}
}
// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
if( $custom_weight = $product->get_meta('_custom_weight') )
echo '<p id="value-on-single-product">' . __("Weight:", "woocommerce") . ' ' . $custom_weight . 'g' . '</p>';
if( $сalories = $product->get_meta('_сalories') )
echo '<p id="value-on-single-product">' . __("Calories:", "woocommerce") . ' ' . $сalories . ' kcal.' . '</p>';
if( $ingredients = $product->get_meta('_ingredients') )
echo '<p id="value-on-single-product">' . __("Ingredients:", "woocommerce") . ' ' . $ingredients . '</p>';
}
// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}
// Display custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_cart_item_name', 10, 3 );
function custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_qty;
}
// Display custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
$product = $item->get_product();
if( $value1 = $product->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $product->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $product->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
On cart page:
On checkout page:
On order received page:
On email notifications:

WP get locale in plugin code

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

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

Categories