Solved

Wordpress Override Core Functions

Posted on 2014-04-09
5
1,548 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
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

856 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