Solved

Wordpress Override Core Functions

Posted on 2014-04-09
5
1,563 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

740 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