WordPress.org

Forums

WooCommerce - excelling eCommerce
Changing text for product add to cart error (7 posts)

  1. JBIRD1111
    Member
    Posted 2 years ago #

    I would like to change the default text of the message: "You already have this item in your cart" which happens when you have products that are sold individually. In class-wc-cart.php there is the following function:

    if ( $product_data->is_sold_individually() ) {
    	$in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] : 0;
    
    	// If its greater than 0, its already in the cart
    	if ( $in_cart_quantity > 0 ) {
    		$woocommerce->add_error( sprintf('<a href="%s">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __( 'View Cart →', 'woocommerce' ), __( 'You already have this item in your cart.', 'woocommerce' ) ) );
    		return false;
    	}
    }

    As you can see the text is defined above. How would I change this text using the functions.php file?

    http://wordpress.org/extend/plugins/woocommerce/

  2. Roy Ho
    Member
    Posted 2 years ago #

    Based on that code snippet there are two ways. If the code is within a WooCommerce template, then you can simply copy that template to your theme's woocommerce folder ( same structure as original ) and modify it there.

    The other way is to localize it by using the PO/MO files and translate that text.

  3. JBIRD1111
    Member
    Posted 2 years ago #

    The code is not within a template file - rather it is in class-wc-cart.php which is in the classes directory of the plugin.

    Im not sure what you mean by localizing it. How about using the function.php file to write a custom action / filter?

  4. Roy Ho
    Member
    Posted 2 years ago #

    If it is a core file, then you need to localize/translate it. The code snippet you provided does not have any hooks so you can't hook into it.

    Read here to learn to translate it -> http://codex.wordpress.org/Translating_WordPress

  5. JBIRD1111
    Member
    Posted 2 years ago #

    It seems like there has to be a better way than localization when it comes to this. Your right, there are no hooks in the snippet I provided. I personally do not know anything about localization - is the process simple? It doesn't necessary seem like the correct way to go about this! Thoughts?

  6. Roy Ho
    Member
    Posted 2 years ago #

    Unfortunately not that I know of...If there isn't a filter, you must use translation...

  7. JBIRD1111
    Member
    Posted 2 years ago #

    I will read up on translating this file. I will post any questions here, thanks!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic