Skip Navigation

[Resolved] Favorites view

This support ticket is created 7 years, 1 month 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 45 replies, has 3 voices.

Last updated by Christian Cox 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#579284

Christian,

I'm hoping you can help with a post I created a few months ago in regards to creating a Favorite posts view.
https://toolset.com/forums/topic/favorites-view-conditional-button-on-post-add-to-remove-from-favorite-list/

I'm experiencing conflicts with the plugin I had intended to use and I'd rather use a View to cut down on the number of plugins used.

I followed your steps and did this so far:
1. Added a custom repeating "number" field in the Motorcycles post type.
2. Created a form that edits the listing post type (Form name= Add to Favorites).
3. I Removed all the inputs from this form except the submit button.
4. I place the edit form in the Content Template for this post type, so it appears on every post.
5. Created view named Favorites List (needs to be formatted).

I'm having trouble with the rest of the next step, Creating a cred_save_data hook.

These were the next steps:
- Use the cred_save_data hook to add the User's ID to the repeating field when the form is submitted
- Filter your Favorites View by this custom field, and show only posts where the custom field value includes the current logged-in user's ID
- Once that works, you can make modifications in the cred_save_data hook and the form that will allow your users to un-favorite a post by deleting their ID from the custom field, if you think this is necessary. The Submit button can include conditional text depending on whether or not the post is already favorited, and the cred_save_data hook can be set up to toggle that custom field value depending on the current favorite status.

Can you assist me with creating the proper hook to allow users to Add and Remove favorites from within the posts and create a Favorites View to display Favorited Posts?

This post accomplished what I am trying to do, but not in this method. I believe the method we are using is more efficient as is doesn't need a separate post type:
https://toolset.com/forums/topic/add-post-to-a-favorite-list/
https://toolset.com/wp-content/uploads/2016/11/459894-favourites.gif?x28882

Thank you,
Chuck

#579587

Here's how the custom field would work:

add_action('cred_save_data_12345', 'toggle_user_favorite_cred',10,2);
function toggle_user_favorite_cred($post_id, $form_data)
{
  add_post_meta($post_id, 'wpcf-fieldslug', get_current_user_id(), false);
}

Replace 12345 with your CRED form ID, and replace fieldslug with the slug of your repeating number field. Place the form in a View that shows posts. Add different posts to favorites with a couple of different Users, and confirm that their ID is added to the post custom field as expected. Then we can work on modifications.

#579745

Updated custom field:
add_action('cred_save_data_1876', 'toggle_user_favorite_cred',10,2);
function toggle_user_favorite_cred($post_id, $form_data)
{
add_post_meta($post_id, 'wpcf-favorites', get_current_user_id(), false);
}

Screenshot of CRED form in content template (disregard favorite button code next to contact seller button, it's a placeholder):
hidden link

Screenshot of CRED form output:
hidden link

The strange part... When I placed the View in the my account, favorites section, it picked up prior favorites from the plugin I was using. Not sure how.
Here is a screenshot of the View:
hidden link

It's getting somewhere, but still needs:
1. CRED form not showing properly on front-end, so can't test to see if posts get favorited to the view.
2. Button to "Remove from Favorites" in the view.

Thank you,
Chuck

#579897

1. CRED form not showing properly on front-end, so can't test to see if posts get favorited to the view.
I didn't explain that part thoroughly enough, sorry. You removed some necessary information from the CRED form code. You stripped out the extra inputs correctly, but you also removed the basic form tags required for CRED to function. Yours should look more like this, so I recommend using the Autogenerate button again to regenerate the proper code, and use this as a guide:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

Let's make sure that works first, then move on to remove favorite.

#579917

Looks like it's creating repeated fields for the same user: hidden link

CRED form markup:

[credform class='cred-form cred-keep-original']
 
  [cred_field field='form_messages' value='' class='alert alert-warning']

  [cred_field field='favorites' post='motorcycles' value='' urlparam='' class='form-control' output='bootstrap']

  [cred_field field='form_submit' value='Add to Favorites' urlparam='' class='x-btn x-btn-global mam']
 
[/credform]

Nothing came through to the view.

Thanks,
Chuck

#579992

Looks like it's creating repeated fields for the same user
Okay are you saying that multiple fields are being added when you submit the form once, or that you have submitted the form multiple times and each time a new field was added? If it's the former, we need to investigate further. If it's the latter, I wouldn't worry about that for now because we will use conditional logic to prevent this in the next step.

Nothing came through to the view.
Can you share the settings you have applied to the View's Query Filter?

#580066

I had submitted the form multiple times and each time a new field was added, each with the value 2; or so it looks like.

I have no filters set at the moment for the View.

Thanks,
Chuck

#580268

I have no filters set at the moment for the View.
Okay you need to set this View up as a Motorcycles View, filtered by the favorites custom field, where the custom field is equal to a shortcode parameter you can call "userid". Then place your Motorcycles View with a shortcode and pass the current User's ID into your filter like so:

[wpv-view name='your-view-slug' userid='[wpv-current-user]']

This will return any Motorcycle post that includes the current User's ID in its repeating custom field. Try this out and let me know if you get the results you expect.

#580359

Does this filter look correct: hidden link

View Shortcode in page: hidden link

View showing no items: hidden link

Add to favorites button: hidden link

1. When I press the "add to favorites button", a message appears and it says please wait. Then the page reloads and another number line appears.
2. Nothing is being added to the favorites view.

Not sure why the post is not appearing in the View. I believe the view shortcode and filter is properly formatted.

Thanks,
Chuck

#580373

The filter looks okay to me, so I suspect something else is going on. Can I take a look in wp-admin to see how everything is set up? Private reply fields enabled.

#580399

Ah, okay I see the wpv-current-user field should include the "info" attribute, and I left that out of the sample I gave:

<h3 class="mtn">Favorites</h3><hr /><p>[wpv-view name="favorites-list" userid='[wpv-current-user info="id"]']</p>

I made that change and cleaned up the favorites fields and now I see the two bikes favorited by this account.

So now you can use the favorites field to determine whether you should add or delete a favorite when the form is submitted. Add a generic field when the current User has favorited this item already:

[credform class='cred-form cred-keep-original']
  [cred_field field='form_messages' value='' class='alert alert-warning']
  [wpv-for-each field="wpcf-favorites"]
    [wpv-conditional if="( [types field='favorites'][/types] eq [wpv-current-user info='id'] )"]
      [cred_generic_field field='remove-favorite' type='hidden' class='']
       {
         "required":0,
         "validate_format":0,
         "default":"1"
        }
      [/cred_generic_field]
    [/wpv-conditional]
  [/wpv-for-each]
  [cred_field field='form_submit' value='Submit' urlparam='' class='x-btn x-btn-global mam']  
[/credform]

Check the value of that field in the PHP function to know whether to add or remove the favorite metadata:

add_action('cred_save_data_1876', 'toggle_user_favorite_cred',10,2);
function toggle_user_favorite_cred($post_id, $form_data)
{
  if( isset($_REQUEST['remove-favorite']) ) {
    delete_post_meta($post_id, 'wpcf-favorites', get_current_user_id());
  } else {
    add_post_meta($post_id, 'wpcf-favorites', get_current_user_id(), false);
  }
}

Now when you submit the form, the favorite designation should be toggled appropriately. Let me know how it goes.

#580412

I've updated the CRED Form, as well as the PHP function.

I'm having trouble actually testing the view because there's no remove button. This is what my view currently looks like: hidden link
I'm not sure how it's showing the second motorcycle in the favorites list, as I've never clicked it's Add to Favorites button.

Here's what the page looks like after clicking the Add to Favorites button: hidden link
The button still reads Add to Favorites.

Thanks,
Chuck

#580413

I've also noticed this: hidden link

Looks like it's hiding a lot, as well as an alert.

Not sure if I should worry about that or not?

Thanks,
Chuck

#580872

I'm having trouble actually testing the view because there's no remove button.
Yes, the button text needs to be controlled with JavaScript so I have added this function in the CRED form's JS panel to handle that:

jQuery(window).bind("load", function() {
   jQuery( "input[name='form_submit_2']" ).val(jQuery( "input[name='remove-favorite']").length ? "Remove from Favorites" : "Add to Favorites");

You can change the text here if you'd like.

I'm not sure how it's showing the second motorcycle in the favorites list, as I've never clicked it's Add to Favorites button.
Somehow it was favorited - perhaps I added it during testing, I don't recall. Anyway I deleted the favorite indicator from this Motorcycle to reset it. Please test out the Add / Remove functionality again and let me know if it's not behaving as expected.

Looks like it's hiding a lot, as well as an alert.
Nothing to worry about here, this is all standard for the CRED form and is expected.

#580931

Looks like it's working!

Two things and I think will be solved:

1. Is it possible to have a spinner instead of displaying " Please wait. You are being redirected." and also avoid having the page reload?

2. What would I insert into the view to show a "remove from Favorites" button?
Screenshot: hidden link

Thanks!
Chuck