Avatar of Aaron Roessler
Aaron Roessler

asked on 

how to use add_filter for this specific function

I am learning how to use add_filter but cannot seem to get it to work for this specific one.

I created a short video to also help explain https://www.loom.com/share/dad288b48b5c4ede99a7104b784b257f

The plugin has an admin toggle setting that enables and disables the woocommerce product hover effect. I need to make it conditional and turn it off for the Shop Page but leave it on for all other pages.  I found the source code and apply_filter of the plugin that I need to edit (see 2nd code below): The last line apply_filters tells me I can use add_filter

So here is what I have now which does nothing and just returns the same data the source code was doing... but I dont know how to modify the return $newdata to disable the hover effect
 
function conditional_hover_effect($newdata) {
	echo '<script>console.log("Function is firing")</script>';
return $newdata ;
}
add_filter( 'jet-woo-builder/template-functions/product-thumbnail', 'conditional_hover_effect', 10, 2 );

Open in new window


		public function get_product_thumbnail( $image_size = 'thumbnail_size', $use_thumb_effect = false, $attr = array() ) {
			global $product;

			if ( ! is_a( $product, 'WC_Product' ) ) {
				return;
			}

			$thumbnail_id        = get_post_thumbnail_id( $product->get_id() );
			$enable_thumb_effect = filter_var( jet_woo_builder_settings()->get( 'enable_product_thumb_effect' ), FILTER_VALIDATE_BOOLEAN );
			$placeholder_src     = apply_filters( 'jet-woo-builder/template-functions/product-thumbnail-placeholder', Elementor\Utils::get_placeholder_image_src() );
			$attr                = array( 'data-no-lazy' => '1' );

			if ( empty( $thumbnail_id ) ) {
				return sprintf( '<img src="%s" alt="EMPTY">', $placeholder_src );
			}

			$html = wp_get_attachment_image( $thumbnail_id, $image_size, false, $attr );

			if ( $use_thumb_effect && $enable_thumb_effect ) {
				$html = $this->add_thumb_effect( $html, $product, $image_size, $attr );
			}
			return apply_filters( 'jet-woo-builder/template-functions/product-thumbnail', $html );
		}

Open in new window

PHPWordPress

Avatar of undefined
Last Comment
Chris Stanyon
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey Aaron,

In your filter, you're not actually doing anything with $newdata - you're just taking it in as an argument, and then returning it ... unchanged. The idea of a filter is that it changes the data before returning it:

function conditional_hover_effect($newdata) {
    $newdata = $newdata . "<script>console.log('Function is firing')</script>";
    return $newdata;
}
add_filter( 'jet-woo-builder/template-functions/product-thumbnail', 'conditional_hover_effect', 10, 2 );

Open in new window

Now the $newdata is passed in, we add some additional text to it, and then we return it.
Avatar of Aaron Roessler
Aaron Roessler

ASKER

Is it possible to modify whats inside the $newdata instead of just adding html before or after it?  Also, how can I echo or print to screen or console showing what variables I can edit within $newdata?  sorry i am very new to wordpress filters.

How do i modify $newdata or maybe i'm doing this all wrong and not even using the correct add_filter ?
Here is the php file I am trying to modify with that Filter (lines 61 to 113 are the two functions that output the image on the page with the hover effect.) https://github.com/golaphazi/store-html/blob/master/jet-woo-builder/includes/class-jet-woo-builder-template-functions.php

Looking at the plugin source code in that file above (public function get_product_thumbnail), I see that these two lines that look important but I don't understand how to modify anything yet by using add_filter

$placeholder_src     = apply_filters( 'jet-woo-builder/template-functions/product-thumbnail-placeholder', Elementor\Utils::get_placeholder_image_src() );
return apply_filters( 'jet-woo-builder/template-functions/product-thumbnail', $html );

Open in new window


Here is my new function that adds a div wrapper around $newdata. but I want to learn how to modify the data inside it.
function conditional_hover_effect($newdata) {
    $newdata = $newdata . "<script>console.log('Function is firing')</script>";
    return '<div class="this_is_the_wrapper_of_newdata">' .$newdata . '</div>';
}
add_filter( 'jet-woo-builder/template-functions/product-thumbnail', 'conditional_hover_effect');

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hmmm.

The problem you have with using filters like this is that you're receiving a fully formatted HTML string in to the filter. To strip out bits from the middle would require some pretty complex regex, and probably isn't the best way to do it.

Looking at the code you've linked to, I would suggest the best way to do it is through your template files. If you look at the function signature from line 61, you'll see this:

public function get_product_thumbnail( $image_size = 'thumbnail_size', $use_thumb_effect = false, $attr = '' ) {

Open in new window

Somewhere in your theme templates, this function will be called, and it will be passing true in as the second argument. You'd want to change that to pass in false. Doing this means the additional HTML for enabling the thumb effect won't be added, because it's conditional based on this bit of code (lines 74-76):

if ( $use_thumb_effect && $enable_thumb_effect ) {
    $html = $this->add_thumb_effect( $html, $product, $image_size, $attr );
}

Open in new window

Have a search through your theme's (or the parent theme's) template files for get_product_thumbnail. When you find it, that's probably the best place to turn it on and off.
Avatar of Aaron Roessler

ASKER

This is VERY helpful and helps me understand more how the apply_filter works.  This also led me down the correct path to find another file and method to almost get this working.  One new small problem though.

I found a file in the plugin folder labeled "item-thumb.php" on this line below if I change true to false as you suggested this will enable or disable the hover effect. So I just needed to add a conditional statement if and else. which almost works.

$thumbnail  = jet_woo_builder_template_functions()->get_product_thumbnail( $size, true );

Open in new window


So I will use that same line of code but use FALSE.  I tried  if (is_page('349')  but that did not work which I don't understand. Either way I got it working with the code below.  

The problem is this only works upon initial page load but when I select a Category Filter i.e. "Lips" the Page uses AJAX to show the Lip Balm products but then this code no longer works anymore.

Here is the shop page where this code works: https://allgood.flywheelsites.com/shop/
This page also works if you go to it directly. https://allgood.flywheelsites.com/shop/?jet-smart-filters=jet-woo-products-grid/default&_tax_query_product_cat%5B%5D=129

But soon as I click on a category filter checkbox (Example: "Lips") the conditions dont work and it uses the else TRUE line.  ? I dont understand why since the page is still meeting the condition?
 if ( is_archive() || is_category() || is_tax() || is_product_category() || has_term( 'lips' )  ) {
	$thumbnail  = jet_woo_builder_template_functions()->get_product_thumbnail( $size, false );
	echo "<script>console.log('Shop page');</script>";
}else{ 
	echo "<script>console.log('Not shop page');</script>";
$thumbnail  = jet_woo_builder_template_functions()->get_product_thumbnail( $size, true );
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Aaron Roessler

ASKER

That was IT!! Thank You!!  looking at your code taught me a few things beyond just getting it to work. I did not realize true / false is the same as using a variable checking if it equals a set of conditions. So True and $onShop =  both will have same result.  

I really appreciate your help!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

No worries Aaron. Glad I could help :)
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo