Solved

Wordpress Override Core Functions

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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.
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…

739 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