Thank you very much Christian,
Promotions work well. The problem with bonuses is that they are associated with a user, not a shop. So the relationship I understand should be like that:
add_action( 'rest_api_init', 'create_api_posts_meta_field_bonos');
function create_api_posts_meta_field_bonoa() {
register_rest_field( 'bonos', 'parent-page', array(
'get_callback' => 'get_parent_page_for_api_bonos',
'schema' => null,
)
);
}
function get_parent_page_for_api_bonos( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the related usuario post ID
return toolset_get_related_post( $post_id, 'user_bono', 'parent');
}
But... how i could associate an user? Because in user dates doesnt appear this field.
As discussed in another ticket, you can create a proxy post type for Users: a custom post type like "Profiles" or "Members", or something similar. Then for testing, publish one Profile post while logged in as the User you want to test, and link the User's Profile post and a published Bonos post in a Toolset post relationship. You can use a 1-to-1 relationship for this purpose if each User can have only one Bonos post, or a one-to-many relationship if the User can have multiple Bonos posts.
See this guide for more information about using a proxy post type for Users: https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/
Let me know if you are struggling to set this up and I can offer guidance.
Thanks Cristhian, I will study what you send me. Once the publications are linked to the user, will I be able to keep the validity and title data in my json?
In this case, I will also be able to make a list to have a listing of all the coupons and their linked users?
And will I also be able to search for the coupon code? to mark it as "used"? Is the consumption of the coupons offline and in the establishment that has been used? (the establishments are a custom type)
is there a way to link that a user can only create 2 coupons?
Once the publications are linked to the user, will I be able to keep the validity and title data in my json?
I can show you how to access the title information from the related post and provide examples showing how to add that information into the rest API. The rest API should handle valid encoding of the title text automatically, I think. We can try it out and see.
In this case, I will also be able to make a list to have a listing of all the coupons and their linked users?
On the front-end of the site, you can create a View of coupons to list all the coupons. To display their linked users, you would need to create a nested View of the related post type with a post relationship filter, then in the loop of the View display the post author information. Nested Views are not supported yet in Blocks, so you must create the nested View in the legacy Views editor.
And will I also be able to search for the coupon code? to mark it as "used"? Is the consumption of the coupons offline and in the establishment that has been used? (the establishments are a custom type)
Sorry I don't understand what this means. Are you talking about searching for a post in wp-admin, or on the front-end of the site? How do you plan to mark it as "used" - will this be a post status, or a custom field value, or a taxonomy term?
is there a way to link that a user can only create 2 coupons?
If the User creates coupons on the front-end of the site using Forms, you can see information about limiting the number of results here in the first FAQ here: https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/#faq
Instead of using a limit of 1 in the View, you can use a limit of 2.
Hello Christian,
I have been looking at the issue of coupons and have simplified it.
- I need to create a campaign with X number of coupons per user (this number can vary from one campaign to another) I should be able to pass via API how many of those X coupons have already been generated.
- The user via APP (external to wordpress) must be able to create in my BBDD the bonus if the campaign is active
- This bonus has to be saved as a field next to the validity date, amount and campaign it belongs to in the user fields I will send via API (it is only saved in the backend) there is no user front-end on the web
So,
I have created the campaign with a ratio of 1 to 1000 coupons.
I have the custom type coupons, how do I relate it to the user? in my user I have created a coupon field. Should this be filled in with the date of validity?
How do I send you this information via API?
I have the custom type coupons, how do I relate it to the user? in my user I have created a coupon field.
You created a custom field to store the Coupon ID in the User Profile, not a proxy post type? Now it sounds like you do not want to use the proxy post type approach we discussed. It sounds like you want to take the repeating custom field approach to link Coupons and Users (the approach outlined in #4 here: https://toolset.com/forums/topic/relationship-in-api-rest-2/page/2/#post-1849031). I can help you with that, as long as you are aware of the limitations I described earlier. If you want to save each Coupon ID as a custom field value in the User's WordPress profile, you must create a custom number User field that allows multiple instances (see multiple-instances.png) in Toolset > Custom Fields > User Fields. To save a Coupon ID in the User's repeating custom field, you can use the WordPress API add_user_meta: https://developer.wordpress.org/reference/functions/add_user_meta/
$user_id = 1;
$coupon_id = 12345;
$user_field_slug = 'coupon-field-slug';
$updated = add_user_meta( $user_id, 'wpcf-' . $user_field_slug, $coupon_id );
Each time a new Coupon is created, you must call add_user_meta to add that Coupon post ID to the User's repeating field.
- This bonus has to be saved as a field next to the validity date, amount and campaign it belongs to in the user fields I will send via API (it is only saved in the backend) there is no user front-end on the web
in my user I have created a coupon field. Should this be filled in with the date of validity?
It sounds like this information would be better stored in the Coupon post's custom fields, as it is directly related to the Coupon and not the User. Since there can be multiple Coupons, it would be better to store this information in the Coupon post type because there is no way to create Types' Repeatable Field Groups (RFGs) in a User's profile, and you need a way to save this information in a repeating format for each Coupon. RFGs are only supported in post types, not User Profiles or Taxonomy terms. If you want to include this Coupon field information in the User's API response, you would query the Coupon ID field you created using get_user_meta, loop over those values, and fetch the custom field values from each Coupon post using get_post_meta. Then include the Coupon posts as an array in the User's API response.
https://developer.wordpress.org/reference/functions/get_user_meta/
https://developer.wordpress.org/reference/functions/get_post_meta/
I think I'm a bit lost with this new approach.
Created the following fields in the custom type "coupons" (attache image)
So, I dont know why doesnt appear name campaign if there are a relationship between coupons and campaign, with this
On this basis, it would be sufficient to give the user the ID of the voucher he has created. But how can I do this?
Following what you have indicated, I have created a user creation form for the role 'ciudadano' and I have also created a form for that user to create the vouchers.
But I have several problems
- These coupons are not directly linked to the campaign
- the form is not linked to any author
Then I'm in trouble.
Can you help me?
- These coupons are not directly linked to the campaign
Have you set up a relationship between Campaigns and Coupons? If you have a one-to-many relationship between Campaigns (parent) and Coupons (child), then when you create a Form that creates new Coupons, it will automatically include a parent Campaign select field. On the front-end, the User can select a parent Campaign for the new Coupon post. If you already created the new Coupon Form, you can add a post relationship field manually in the Form builder. I can split off a separate ticket for you if you'd like some help with that.
- the form is not linked to any author
Every post published in a Toolset Form has an author. If the User is logged in when they submit the Form, then the User will be the author of the post created by the Form. This is an automatic process, nothing else is required to set the post author. If you cannot see the author information in the post editor screen, be sure the Author field is active in Toolset > Post Types > (your custom post type) > Sections to display when editing.
If you want to also store the new Coupon post ID in the User's repeating fields, you can use the cred_save_data hook to trigger the add_user_meta function I described in my previous reply:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Example:
add_action('cred_save_data', 'tssupp_set_user_meta',10,2);
function tssupp_set_user_meta($post_id, $form_data) {
global $current_user;
$forms = array( 123 );
if ( in_array( $form_data['id'], $forms ) )
{
$user_id = $current_user->ID;
$user_field_slug = 'coupon-field-slug';
$updated = add_user_meta( $user_id, 'wpcf-' . $user_field_slug, $post_id );
}
}
You would change 123 to match your new Coupon Form ID, and change coupon-field-slug to match the slug of the User's repeating coupon field. This will add the new Coupon ID to the User's repeating field values whenever a new Coupon is created using Forms.
In the relation between campaign and bonds I have what I attach in the image
Is this enough?
When I fill in the form this coupon is not linked to the campaign.
What am I doing wrong?
In the relation between campaign and bonds I have what I attach in the image
You cannot relate Campaigns directly to Coupons using a relationship between Campaigns and Bonos. You must use a one-to-many relationship between Campaigns and Coupons. Then after that relationship is created in Toolset > Relationships, you must have a post relationship field in the Coupon Form to relate Campaigns and Coupons when the new Coupon is created.
1. Go to Toolset > Relationships to confirm there is a one-to-many relationship between Campaigns and Coupons. If no relationship between Campaigns and Coupons exists, you must create one.
2. Edit your New Coupon Form. If you are using the drag-and-drop Form builder, find the Campaigns - Coupons relationship in the Relationships section and drag it into the Form (drag-and-drop.png). If you are using the Expert mode builder, click "Add fields" above the editor and click the Campaigns - Coupons relationship button to add this field to the Form (expert-mode.png).
Great!
if I want the campaign to be selected and cannot be changed?
You can apply a default value to any field in the drag-and-drop Form Builder using a hard-coded value:
...a shortcode:
...or by setting the value with a URL parameter:
<em><u>hidden link</u></em>
If you want to make the field uneditable (readonly), you must use the Expert mode Form builder and apply the readonly attribute in the field shortcode, set to "true":
[cred-field ... readonly="true"][/cred-field]
Documentation for all the supported field shortcode attributes, including default value, URL parameter, and read-only, is available here:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_field
I have this
<div class="form-group">
<label>[cred_i18n name='@campana-bonos-bono.parent-label']Campañas Bonos Bonos[/cred_i18n]</label>
[cred_field field='@campana-bonos-bono.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='true' author='$current' value='Campaña Bonos Inicial']
</div>
Where i put this?
[cred-field ... readonly="true"][/cred-field]
You may add readonly='true' anywhere in the shortcode attributes. The order of attributes is not important:
[cred_field field='@campana-bonos-bono.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='true' author='$current' value='Campaña Bonos Inicial' readonly='true']
However, I see you have set the value using a post title. That won't work, because the value attribute must be defined as the post ID of the related post.