Functions.php code not working with Bricks

Hey Guys!

First time Bricks user here. I currently use another page builder and looking to migrate.

I currently have the following code in my functions.php live site (non-bricks):

add_action( 'init', 'custom_post_type', 0 );

}

function update_percentage_of_tickets_sold_custom_meta() {

	global $post;
	
	$product_data = wc_get_product( $post );
	
	if ( is_object( $product_data ) ) {

		$product_data_type = method_exists( $product_data, 'get_type' ) ? $product_data->get_type() : $product_data->product_type;	

		if ( is_object( $product_data ) && $product_data_type == 'lottery' ) {
			
			$product_id = $product_data->get_id();
			$max_tickets = $product_data->get_max_tickets();
			$lottery_participants_count = !empty($product_data->get_lottery_participants_count()) ? $product_data->get_lottery_participants_count() : '0';
			
			$sold_percentage = get_post_meta( $product_id, 'percentage_of_tickets_sold', true ) ? get_post_meta( $product_id, 'percentage_of_tickets_sold', true ) : 0;
			$sold_percentage_temp = ( $lottery_participants_count / $max_tickets )*100;
			
			if ( intval ($sold_percentage_temp) > intval ( round($sold_percentage) ) ){

				update_post_meta( $product_id, 'percentage_of_tickets_sold', intval(round($sold_percentage_temp)) );
			}
		
		}
	}
}
add_action( 'template_redirect', 'update_percentage_of_tickets_sold_custom_meta' );

However, when I use this in a clean Brick install, the site breaks.

The specific line:

		$product_data_type = method_exists( $product_data, 'get_type' ) ? $product_data->get_type() : $product_data->product_type;	

appears to be causing the issues.

Any advice would be useful - cheers!

1 Like

HI @embark,

I’m definitely new to Bricks and don’t know a great deal about php, however. I checked the complete code you provided in PHP Checker and it shows an error. Checking just the Function the code appears to be fine.

Is the code you have provided the COMPLETE code in your functions.php file??
And have you got the Bricks Child Theme activated??

The code works in the child theme functions.php file. But I can’t get this newly created meta value ‘percentage_of_tickets_sold’ to show in Bricks - am I missing how to do this correctly?

Thanks in advance!

If you want to output your post’s custom field, you should use dynamic data. Check article on how to use dynamic data in Bricks under “Native WordPress custom fields” section.

1 Like

Thanks for the guide - pointed me in the right direction!

I had tried the dynamic data previously without any luck.

I am using {term_meta:percentage_of_tickets_sold} but this doesn’t render any dynamic date.

Checking the database, I found: “percentage_of_tickets_sold - 1” for the product. So the value exists in the database but it’s not rendering on front-end.

As mentioned, coming from another builder (where I can do this instantly) I am struggling a little here.

Thanks again for the help.

I have also tried:

{echo:update_percentage_of_tickets_sold_custom_meta}

But still not the solution.

Ok, not sure if I understand. Did you manage to make it work or not? If not, could you please describe your setup, especially the elements structure and where are you trying to use this custom field? Your original post was more about function not working despite being put in functions.php. So I just want to know what I can help you with precisely :slight_smile:

Apologies, I didn’t mean to confuse!

The code isn’t throwing the error in functions.php as per my original post - so that’s working and creating the custom meta value in the database.

The issue now is rendering the dynamic data for this custom meta value ‘percentage_of_tickets_sold’.

In the other page builder I use, I can enter {{dc:post:meta key=“percentage_of_tickets_sold”}}% to display that custom meta value for each product.

So, I must be missing something on Bricks! (First site having used X/Pro theme for 6+ years).

Thanks again!

Ok, no problem, just wanted to make sure I understand your situation correctly. So please share screenshots of your elements structure and the settings of an element that you want to output this meta in. If I see correctly, this meta is attached to the product of type “lottery” so is this a WooCommerce product or a different CPT created by some plugin? Just want to make sure before I make any unnecessary assumptions.

Cool!

Here’s my screen ATM…

“lottery” is a custom woocommerce product type created by a plug-in - correct. By the custom meta value is not created by the plug-in, that’s why we created the code in the functions.php above.

Ok, so step by step.

First, you don’t want to use {echo:function_name} with the function to update this meta. As “echo” tag is used to output given function value. Your function just updates stuff, doesn’t return anything explicitly.

Second, please try the following:

  1. Add new basic text element
  2. In the element’s “content” tab, click the dynamic content icon (the bolt) and search for your field there
  3. If you can find it, just click it and let us know if it displays on the front-end and in what form was it added inside the content box. If not, let us know as well, and I will give you additional options to try out.

Thanks for the thorough details…

I created a basic text element as suggested, but that’s the thing, using the dynamic content icon (the bolt) the field isn’t there at all…

Cheers!

Thanks for the suggestion - unfortunately didn’t work. Funny how you get quirks like that!

Ok, thanks for the info.

How is this meta created exactly? Is it by adding a native WordPress post custom field, with ACF (or similar) or with code?

So now let’s try something that should work for 99,9% :slight_smile:

If you know the ID of the lottery product post you’re testing, go to the next step. If not in the same text element, manually paste this: {echo:get_the_ID()}, save and on the frontend see what the ID of the post is. Remember it.

Then change the text content to this {echo:get_post_meta( 999, 'percentage_of_tickets_sold', true )} and remember to change the 999 with the lottery you’re testing. Let me know if this outputs the metakey value.

The meta is created solely by the original code:

Following those steps you mentioned, I got the ID of 863 - so I then added the following onto the basic text element:

{echo:get_post_meta( 863, 'percentage_of_tickets_sold', true )}

This resulted in a blank output on the front-end.

Thanks again for your assistance with this.

Ok, that’s weird as this is a pure way of retrieving this meta, so it has nothing to do with Bricks.

Wait, you mentioned that

Do you mean you’ve found the field named “percentage_of_tickets_sold - 1”?

Then use this field name in the function I gave you (with the “-1” at the end).

Strange. Like I said, I currently have the exact same set-up in another builder, on a live site without any issues. The builder can pick out the meta values fine. Bricks isn’t able to find this in the dynamic data list it seems.

The meta field name is “percentage_of_tickets_sold” and the meta value is “1”. So I am trying to get the “1” to output on the front-end see.

Screenshot 2022-07-25 18.38.28

Here’s the set-up in the other builder. Check “Percent” field, where I can insert the dynamic data into the field perfectly.

I don’t think I can input dynamic data into a skill/progess bar either, but that’s another issue…

Yeah I know, but from what I’ve quoted it sounds like you have this field name with the “-1” at the end which could be the case if WordPress thinks that you already have this field and creates a new one with “-1” at the end not to create duplicates.

You can check all available fields for this post by using code block element and pasting there the code below. Remember to check “Execute code” below the code field and to reload it after pasting with Ctrl+R and then saving the builder.

<?php
$this_post_id = get_the_ID();
$myvals = get_post_meta($this_post_id );

foreach($myvals as $key=>$val)
{
    echo $key . ' : ' . $val[0] . '<br/>';
}
?>
1 Like

Thanks for that code! I checked - meta value wasn’t there. I populated another product and redid the previous steps. I now have that meta value outputting with the code: {echo:get_post_meta( 259, ‘percentage_of_tickets_sold’, true )}%

But will this only appear to use the post ID (259) value? I.e. won’t this show the same value for every product?