Skip Navigation

[Resolved] Sum by toolset_get_related_posts

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 6.38 hours from now. Thank you for your understanding.

This topic contains 7 replies, has 2 voices.

Last updated by domenicoS 2 years, 1 month ago.

Assigned support staff: Waqar.

Author
Posts
#1206892

Hi,

I need your support to resolve an issue.

This is the situation:

I've a custom value "wpcf-adult-number" and it save value in the order of woocommerce.

I'va a product which is connected with toolset relationship (one-to-many) to many orders. The slug of the relationship is "prodotto-prenotazione"

In each order I've stored the value for "wpcf-adult-number"

I need to display the total as sum of all these value using a shortecode.

I try with this:

function iscritti_total_shortcode() {
    $post_id= get_the_ID();
		$order_id = toolset_get_related_posts( 
            $post_id, 
            'prodotto-prenotazione', 
            'parent'
        );
		
		foreach ($order_id as $value) {
		$sum += $value;
		}
return $sum;

}
add_shortcode('iscritti-total', 'iscritti_total_shortcode');

It returns me sthe sum of order id, of course.
How can change it to have the sum of value of "wpcf-adult-number" ?

#1206909
#1207607

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Domenico,

Thank you for waiting, while I performed some tests on my website.

I apologize for the typo in the code I shared earlier:


$sum += do_shortcode('[types field="adult-number" item="$value"][/types]');;

My tests confirm that the correct code needs to be:


$sum += do_shortcode('[types field="adult-number" item="'.$value.'"][/types]');

I hope this helps and please let me know how it goes.

regards,
Waqar

#1207832

Hi Waqar,

I try your suggestion but with not success. The result of the sum is an addition of postid and not of the field value.

├╣
function iscritti_total_shortcode() {
    $post_id= get_the_ID();
	
	$order_id = toolset_get_related_posts( 
            $post_id, 
            'prodotto-prenotazione', 
            'parent'
        );

		foreach ($order_id as $value) {
		$sum += do_shortcode('[types field="adult-number" item="'.$value.'"][/types]');
		}
return $sum;


}
add_shortcode('iscritti-total', 'iscritti_total_shortcode');

How can I use the toolset_get_related_posts to reach the field value ("adult-number") and not the post-id value? I think this is the issue.

#1208082

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Domenico,

During troubleshooting, I noticed something which explains why you're not able to make the shortcode work on your website.

The shortcode is calling for related posts through the relationship "prodotto-prenotazione" which is between:

1. Products: hidden link
2. Orders: hidden link

This means that inside the "foreach" loop, the "$value" will hold the IDs of "Orders" posts, related to the current "Products" post.

But the "adult-number" custom field that you need for the sum calculation, is not attached to the "Orders" post type.

Please check the custom field group "Richiesta disponibilità" ( hidden link ), where this "Numero Adullti" ( adult-number ) is defined. You'll note that it is attached to post types "Preventivi, Ordini Tour, Chats, Proposte", but not to "Orders" post type.

This means that the final shortcode that you have would work for calculation of any custom field which exists with post type "Orders", but not with any field like "Numero Adullti" which doesn't exist with orders.

I hope this clarifies.

regards,
Waqar

#1208175

Yes Waqar, you're right. the field is stored in the "Ordini tour" types.

I display them in the order detail page with this function:

function custom_meta_after_order_itemmeta( $item_id, $item, $product) { 
    $order_id = get_the_ID();
    $cred_meta = get_post_meta( $order_id, '_cred_meta');
     
    $cred_meta = maybe_unserialize( $cred_meta[0] );
    $extra_text = '';
    if(isset($cred_meta[0]['cred_post_id'])){
        $post_id = $cred_meta[0]['cred_post_id'];
		$product = wc_get_product( $item['product_id'] ); 
		$product_id = $product->get_id();
		$offer_id = toolset_get_related_post( $post_id, 'ordine-offerta',  'parent');
		if ($product_id == 31890 ) {$product_id =$offer_id;} 
		
		//Fields
		$price_calculation = get_post_meta($product_id, 'wpcf-price-calculation', true);
		$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
		} 

How can I reach it toolset_get_related_posts?

#1208185

Hi Waqar,

I display the first value of "adult-number" field from the first order related with this code:

function iscritti_total_shortcode() {
	
	$post_id= get_the_ID();
	
	$order_id = toolset_get_related_post ($post_id,'prodotto-prenotazione','child');	//with toolset_get_related_post s does not work
	
	$cred_meta = get_post_meta( $order_id, '_cred_meta'); 
    $cred_meta = maybe_unserialize( $cred_meta[0] );
    $extra_text = '';
    if(isset($cred_meta[0]['cred_post_id'])){
        $post_id = $cred_meta[0]['cred_post_id'];
		$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
		}
	
return $adult_num ;

How can display the sum of all value from "adult-number" of all orders related?

Thank you for your help Waqar

#1209208

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Domenico,

Assuming that the other shortcode "order-adult" is working to get the value of "wpcf-adult-number" from "Ordini tour" posts, you can update your shortcode's logic to:


// Numero iscritti Shortecode
function iscritti_total_shortcode() {
	
	$post_id= get_the_ID();
	$sum = '';
	
	$order_id = toolset_get_related_posts($post_id,'prodotto-prenotazione','parent');

	foreach ($order_id as $value) {

		$cred_meta = get_post_meta( $value, '_cred_meta');
		$cred_meta = maybe_unserialize( $cred_meta[0] );

		if(isset($cred_meta[0]['cred_post_id'])){
			$post_id_cred = $cred_meta[0]['cred_post_id'];
			$adult_num = get_post_meta($post_id_cred, 'wpcf-adult-number', true);
			$sum += $adult_num;
		}
	}
	
return $sum;

}
add_shortcode('iscritti-total', 'iscritti_total_shortcode');

Important note: As mentioned earlier, we can guide you around how Toolset related functions can be used, which in this case is "toolset_get_related_posts" and is returning the ID of related posts, but we can't perform troubleshooting around non-Toolset related custom PHP code.

For more personalized and 1-1 assistance around custom PHP code, it would be safer and efficient to hire a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1209209

Dear Waqar, My issue is resolved now.
It works great.
Thank you!