Skip Navigation

[Resolved] implement favorites system with toolset

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 61 replies, has 4 voices.

Last updated by simonM-5 4 years ago.

Assisted by: Jamal.

Author
Posts
#1818801

HI Jamal

Great on all fronts - looking very promising now. I will test now building an Edit Ad Form to edit a post from the front end, and see what happens and get back to you.

I first need to copy and paste all our code snippets into one text file and add them to functions.php until we get our site sorted out. Unfortunately our host provider is very unavailable at the moment and causing a lot of stress to the point where we have decided to move host providers. But we can still continue along here in the meantime.

I'll update you again when I have the Edit Post Form done and integrated into the site.

Kind regards
Simon

#1818983

Hi Jamal

OK so I added an Edit Job Ad form and tried editing some existing Job Ads.

It seems to work - post edits are being passed to their languages duplicates correctly.

Edits to language duplicates are not being passed to their originals. But I believe that is expected WPML functionality as we covered in another ticket together and we already prevent that on our site. (also got our site working again by adding all snippets to functions.php - thanks for that valuable tip, I forgot that that was possible because I have been working with Toolset for so long!)

The Favourite flagging is preserved and synced between languages, no matter what language is favourited.

It looks like it is working as we want it to. 😀😀😀

So to finalise this prototype, I think it would be a good idea to:
- clear out Nanny Ads and all references to it, and just have Job Ads as the prototype. The same will apply for Nanny Ads (I can do that)
- clear up Toolset objects that I created which are no longer needed.

Here is a list of all the items I now see as redundant, please correct me if I'm wrong, especially in regard to the Favourites CPT

- CPT Favourites and its related custom fields - correct?
- All CPT/Custom fields/Views/Content Templates/Pages/Post Forms related to Nanny Ads

Kind regards
Simon

#1822025

Hello Simon,

Indeed, the CPT "Favourites" is not used anymore. Forms that we have created for it are not used too.
On my test site, I did not test anything on the Nanny Ads.

Best regards,
Jamal

#1822161

HI Jamal

I deleted all the Nanny Ad related stuff.

So we don't need these two post forms either, because the plugin just needs a text string to display (for which we used the Font Awesome hearts, correct?
- Favourite Job Ad
- Unfavourite Job Ad

Best regards
Simon

#1822377

Exactly 🙂

We don't need these Favourites CPT post forms as we are not using Toolset for favoriting/unfavoriting features.

We'll only need to customize the settings for the plugin in order to use FontAwesome icons, and in order to show add/remove to the favorites list.

We also need both custom codes:
- Custom code to sync favorites list across languages.
- Custom code to pass the favorites posts to the "My Favorites Job Ads" view.

I removed the forms from my test site hidden link
And now, it is minimal. It only holds two Toolset elements(The views) and the custom codes.

#1822585

HI Jamal

OK, one last thing - I created a custom user field too, so I deleted that.

So I will try and replicate what we have built here in our site.

This is great Jamal, again thank you for all your patient efforts - I think other customers could benefit greatly from this too!

Kind regards
Simon

#1822601

Thank you Simon for your kind words. I agree other users will find this ticket useful.

I'll set this ticket as waiting for your reply, it should be kept open for 3 weeks until you can work it on your website and let me know what you will get.

All the best for your project.

#1822699

HI Jamal

I added the custom code from the Favourites site as snippet via Toolset Custom Code in to our site and now on Find a Job I'm seeing these error messages:

Warning: A non-numeric value encountered in .../apps/dev/wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 378

Warning: Creating default object from empty value in .../apps/dev/wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 152

This may be related to the other ticket I just opened. It has a similar error message.

Thanks and regards
Simon

#1822737

HI Jamal

I need a little more of your expertise modifying the code for Nanny Ads too..

1) I changed the view ID from the 69 to 10121 (our "My Favourite Job Ads View")

FROM
function prefix_get_favorite_posts( $query_args, $view_settings, $view_id ) {
if ( ($view_id == 69) and function_exists( 'wpfp_get_user_meta' ) ){

TO
function prefix_get_favorite_posts( $query_args, $view_settings, $view_id ) {
// My Favourite Job Ads View 10121
if ( ($view_id == 10121) and function_exists( 'wpfp_get_user_meta' ) ){
....

2) Can I just repeat the following snippet for My Favourite Nanny Ads View?

function prefix_get_favorite_posts( $query_args, $view_settings, $view_id ) {
// My Favourite Nanny Ads View 10151
if ( ($view_id == 10151) and function_exists( 'wpfp_get_user_meta' ) ){
$query_args['post__in'] = wpfp_get_user_meta();
}

return $query_args;
}

3) Do I just need to repeat the following snippet and change job-ad to nanny-ad?

function get_duplicates( $post_id){
$args = array('element_id' => $post_id, 'element_type' => 'job-ad' );
$info = apply_filters( 'wpml_element_language_details', null, $args );
$post = get_post( $post_id );
$posts = apply_filters( 'wpml_get_element_translations', NULL, $info->trid, 'post_' . $post->post_type );
return $posts;
}

Thanks and regards
Simon

#1823757

Hello Simon,

Regarding the previous reply about PHP Warning, I still can't find a step-by-step way to reproduce the warnings, sometimes they appear and sometimes no. No systematic way to reproduce the issue and thus to investigate it.

Regarding your last message, you are right. You need to change the view_id with your views' ids, and for the Nanny Ads, you will need to change the post type slug(nanny-ad).

I'll analyze further and try to find a way to reproduce the warning in order to debug them.

#1824697

Hi Jamal

OK, thanks. I updated the code. Had to rename a few functions along the way to differentiate them for job ads and nanny ads.

We will test some more to try and get a reproducible case for you today.

Kind regards
Simon

#1825821

Thank you Simon!

I'll be looking forward to your reply.

#1826161

Hey Jamal

The error appears for me to be reproduced every time I favourite/unfavourite an Ad. So far it is reproducible every time for me.

For the first time favouriting an Ad, I get both error messages, that is:
(1) Warning: A non-numeric value encountered in .../apps/dev/wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 378

(2) Warning: Creating default object from empty value in .../apps/dev/wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 152

thereafter, for example by unfavouriting the same Ad, just the second one:
(2) Warning: Creating default object from empty value in .../apps/dev/wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 152

If I leave the Find a Job page and come back to it, I also only get the 2nd one, so it would appear the very first time the Ad is favourited both errors, thereafter just the second one.

Kind regards
Simon

#1829581

Hello Simon,

I think that the issue is there because the custom code for syncing the favorite flag is not executed. And that is because the trigger of the code is wrapped in an init hook. Please change the part of the snippet that triggers the sync from:

add_action('init', function(){
	if (function_exists('wpfp_link')) {
		add_action('wpfp_after_add', 'jts_sync_favorite', 10, 1);
		add_action('wpfp_after_remove', 'jts_sync_unfavorite', 10, 1);
	}
});

To:

if (function_exists('wpfp_link')) {
  add_action('wpfp_after_add', 'jts_sync_favorite', 10, 1);
  add_action('wpfp_after_remove', 'jts_sync_unfavorite', 10, 1);
}

It seems that the favorite plugin uses (+) (-1, 0, 1), and because the syncing code was not triggered, there were some unexpected values, such as 2.

In my local copy, I am not getting these notices, but the syncing was not working. After changing the code the syncing is working now.

Please try this, and test with new posts that won't have any previous values. If this is still not working, I'll take another copy from your website to get the recent state.

#1829605

HI Jamal

Thanks for the update. I did as you advised, erased all Ads and created a new Job Ad and a new Nanny Ad.

(1) Before even favouriting either of the new Ads, the following is showing up under My Nanny Ads and My Job Ads (click megaphone in menu > My Nanny/Job Ads)

The test results reveal that 1 is being stored for the shortcodes "is_job_ad_wpml_original" and "is_nanny_ad_wpml_original" in both EN and DE, when viewing in DE I would expect this shortcode to return 0 and to hide the Edit Ad buttons.

The error messages are now the same for Job Ads :
Notice: Trying to get property 'source_language_code' of non-object in .../wp-content/toolset-customizations/func-is-job-ad-wpml-original.php on line 14

and Nanny Ads:
Notice: Trying to get property 'source_language_code' of non-object in .../wp-content/toolset-customizations/func-is-nanny-ad-wpml-original.php on line 14

(2) After favouriting either a Job or Nanny Ad, I see the spinner symbol, indicating that something is happening, but the page doesn't refresh. If I manually refresh the page, the Ad appears correctly as favourited, however if I switch to German, the favouriting has not been synced to the German duplicate.

Kind regards
Simon