Solved

Wordpress Override Core Functions

Posted on 2014-04-09
5
1,487 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
Try is_front_page() instead of is_home().
0
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
@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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now