rtpHarry / jet-engine-repeater-values

Access JetEngine repeater values via Elementor Dynamic Tags
GNU General Public License v3.0
39 stars 10 forks source link

Can't manage to compare values from a repeater field to sort them #5

Closed Heavyl closed 2 years ago

Heavyl commented 2 years ago

What I'm trying to do:

I have a jet engine repeater with 3 keys inside : name, price, link. For each post of a given post_type I want to compare all 'prices' values that exist for this repeater and store the lowest one to a custom meta field. Then I want to also sendthe link value from the same row to another meta field. Finally, those two last meta field should be updated when the post is saved, updated or published.

(It's basically a price comparison tool. )

What I manage to understand or not:

As I understood, using usort() is the best way to proceed since it will sort each array inside the first one, which is perfect for what I tried to achieve as it will allow me to select the link corresponding to the lowest 'price' value. I also understood that using the publish_post hook should do the trick for triggering this function.

But it's not working and I think that's maybe because I don't really understand how the repeater from jet engine is built. Or how the get_post_meta() works.

The code:

//The function to compare values from a given array
function compare_prices($price1, $price2){
    if($price1['price'] == $price2['price']){
        return rand(0,1) ? 1 : -1//if it is the same price then it is random
    }
    return $price1[0]['price'] > $price2[0]['price'];
    // if not it sorts the array
}

//The function to select the best price and update meta fields
function best_product( $id, $post ) {
    if ( get_post_type($post->ID) = 'post_type' ) {  // replace post_type with wanted post type
        $products = get_post_meta( $post->ID, 'link' );  
        usort($products, 'compare_prices');

        $lowest_price = $products[0]['price'];
        $best_link = $products[0]['link'];

        update_post_meta( $post->ID , 'best_link' , $best_link);
        update_post_meta( $post->ID , 'lowest_price' , $lowest_price);

    }
}
add_action( 'publish_post', 'best_product', 10, 2 );

I'm very new at PHP and this really is a huge time consuming issue without getting any result. Any help or tips would be greatly appreciated !

rtpHarry commented 2 years ago

Taking a glance at this on my lunch break, it seems like you have a number of issues with the code.

The 'post_type' in if ( get_post_type($post->ID) = 'post_type' ) { should be replaced with the actual post that you're looking into, eg post or page normally.

The line $products = get_post_meta( $post->ID, 'link' ); is only attempting to get the one meta value with a key of link. But then later on you are trying to use that like $products is an array of product data, so that won't work.

This line might create a bug as well add_action( 'publish_post', 'best_product', 10, 2 );, I'm not sure, but I think if you have a hook on a save, and then save the post that it can create an infinite loop. It might not be an issue though as you're only attempting to update the meta fields.

Finally, you might not be getting the data in the right way. I'm not sure if the repeater values are saved as meta items and can be accessed that way.

It's a lot to unpack and more than I have time to look into right now, sorry.

As it's not related to the usage of the plugin, I recommend that you post your question on StackOverflow, and also add my comments in to get the conversation started.

Hope you find your solution!

Heavyl commented 2 years ago

Thanks for your time and response !

I'll try to post this in the right place ;)