Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wordpress Override Core Functions

Posted on 2014-04-09
5
Medium Priority
?
1,739 Views
Last Modified: 2014-04-10
There is a Core Function within the Woo Commerce Plugin.  I would like to create a custom function, in my functions.php file that overrides the Woo Commerce Core function.
I'm not sure how.

The Woo Commerce Function is this:
	public function single_add_to_cart_text() {
		return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $this );
	}

Open in new window


The custom Function I would like to use is the following.
<?php
if ( is_home() ) {
   	public function single_add_to_cart_text() {
		return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to Backpack', 'woocommerce' ), $this );
	}
} else {
    	public function single_add_to_cart_text() {
		return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $this );
	}
}
?>

Open in new window

How do I make that work?  You can see that all I want to do is change the text if it is the home page.  The documentation said to not mess with the woo commerce core files but override in my functions file.
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Author Comment

by:rgranlund
ID: 39989306
I tried the following but it does not work:
if ( function_exists( 'single_add_to_cart_text' )) {
if ( is_home() ) {
		return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to Backpack', 'woocommerce' ), $this );

} else {
		return apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $this );
	}
}

Open in new window

0
 
LVL 15

Accepted Solution

by:
eemit earned 2000 total points
ID: 39991024
Try this in your child theme's functions.php:

add_filter( woocommerce_product_single_add_to_cart_text', 'yourchildthemeslug_single_add_to_cart_text' );
function yourchildthemeslug_single_add_to_cart_text( $text ) {
	if ( is_home() ) {
		$text = __( 'Add to Backpack', 'woocommerce' );
	} else {
		$text = __( 'Add to cart', 'woocommerce' );
	}
	return $text;
}

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39992775
@eemit That seems like it should work, but does not.  In the child functions.php does it know the it is the home page?  Meaning is it starting to early in the code for it to know?
0
 
LVL 15

Expert Comment

by:eemit
ID: 39992894
Try is_front_page() instead of is_home().
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39992918
@eemit, thank you for your help  I realized I gave you the wrong initial function name.  My bad.  It works 100% now.  Thanks!!!
0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question