I'm trying to display the appropriate form depending on the package I purchased earlier.
I have two types of packages - for 15 days and for 30 days.
Each package is another CRED form. I also created 2 forms for creating posts - one for 15 days, another for 30 days.
You need to be able to view a link to one of these forms depending on the package purchased earlier, something like:
[wpv-conditional if="($(packeage-30) ne '1' )"]
[cred_child_link_form form='730' text='Add post 15' target='_self']
[/wpv-conditional]
[wpv-conditional if="($(packeage-30) eq '1' )"]
[cred_child_link_form form='4397' text='Add post 30' target='_self']
[/wpv-conditional]
In the CRED form for 30 days I added a hidden box:
[credform class="cred-form"]
[cred-field field="form_messages" value=""]
[cred-field field="form_submit" value="30 days"]
[cred_generic_field field='package-30' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1"
}
[/cred_generic_field]
[/credform]
I do not know what I'm doing wrong. Maybe my idea is bad...
Please give me a hint on how to do it right.
Dear Bochnacki,
It depends on how do you setup the "the package purchased earlier"?
If you are using a custom user field to store the value of "the package purchased earlier", you will need to use the types shortcode in the [wpv-conditional] shortcode, see our document:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
Using shortcodes in conditions
"It depends on how do you setup the "the package purchased earlier"?" - Do not understand the question.
The 30 day Package is a CRED form that does not have any settings, other than assigning to the product.
I wrote such a shrtcode (I'm not sure if well):
function package_purchased_earlier() {
global $cred_form_id;
if($cred_form_ids = array(4376) ){
return 1;
}else{
return 0;
}
}
add_shortcode( 'package30', 'package_purchased_earlier' );
It is not possible for two packages to be purchased simultaneously, so I think it is enough to indicate the ID of the package purchased so that it can be checked for 30 days or not.
Then use the shortcode:
[wpv-conditional if="( '[package30]' eq '1' )"][cred_child_link_form form='4397' text='Add post 30' target='_self'][/wpv-conditional]
But maybe I'm wrong because my way still does not work...
I means this:
How do you setup the "package purchased earlier"?
How do you setup the two types of packages - for 15 days and for 30 days?
As I know, in your previous post you have customized the Classifieds demo site
If it is depends on the different CRED form, for example:
CRED form A - create/edit the package "15 days"
CRED form B - create/edit the package "30 days",
You can try these:
1) When user submit the CRED form A, add a custom field into the package post, setup the field value as "15 days", same as above:
When user submit the CRED form B, add a custom field into the package post, setup the field value as "30 days"
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
2) In the single package post, use shortcode [wpv-conditional] to check the field value, then display the what you want
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
I made a new custom field "Package days" for the entries type "Package".
The field has two options:
- 15 days (value = 1)
- 30 days (value = 2)
The code I added to functions.php:
add_action('cred_save_data_4376', 'my_save_data_activation_package_30',10,2);
function my_save_data_activation_package_30($post_id, $form_data)
{
if ($form_data['id']==4376)
{
if (isset($_POST['package-days']))
{
// add it to saved post meta
add_post_meta($post_id, '__my_custom_field', $_POST['package-days'], true);
}
}
};
And code on the page to show/hide the appropriate link to the CRED form:
[wpv-conditional if="( $(wpcf-package-days) eq '1' )"][cred_child_link_form form='730' text='Add post 15' target='_self'][/wpv-conditional]
[wpv-conditional if="( $(wpcf-package-days) eq '2' )"][cred_child_link_form form='4397' text='Add post 30"' target='_self'][/wpv-conditional]
What am I doing wrong that it still does not work?
How do you make the custom field "Package days" in the CRED form?
In your case, I don't think you need it, here is the example, the CRED form (ID 4376) is for "30 days" post:
1) Modify your PHP code as below:
add_action('cred_save_data_4376', 'my_save_data_activation_package_30',10,2);
function my_save_data_activation_package_30($post_id, $form_data)
{
update_post_meta($post_id, 'wpcf-package-days','30-days');
};
2) In the single package post, use shortcode [wpv-conditional] to check the field value, then display the what you want, like this:
[wpv-conditional if="( $(wpcf-package-days) eq '30-days' )"][cred_child_link_form form='4397' text='Add post 30"' target='_self'][/wpv-conditional]
In the CRED form I added a hidden field
[credform class="cred-form"]
[cred-field field="form_messages" value=""]
[cred-field field="form_submit" value="Package 30 days"]
[cred_generic_field field='package-days' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"30-days"
}
[/cred_generic_field]
[/credform]
After converting PHP to the one you gave above and adding a shortcode [wpv-conditional if="( $(wpcf-package-days) eq '30-days' )"] to the link page, I will only see a link to the form for 15 days.
"2) In the single package post, use shortcode [wpv-conditional] to check the field value" - Shortcode [wpv-conditional] I've added to a page where there are links to different category forms (cars, vans, trucks, ...). It's not in a single package, just on a regular WP page.
I mean, after the purchase of the package for 15 days, I have a link to the form that you can add a post with a display time of 15 days.
After the purchase of the package for 30 days, I will have to show a link to the form for 30 days.
I hope I described it clearly.
Again, I don't think the generic_field "package-days" is needed in your case, but since you insist to use it, here are the detail steps:
1) In the content of CRED form, use the same codes you mentioned above:
[credform class="cred-form"]
[cred-field field="form_messages" value=""]
[cred-field field="form_submit" value="Package 30 days"]
[cred_generic_field field='package-days' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"30-days"
}
[/cred_generic_field]
[/credform]
2) In your theme/functions.php, use below PHP codes:
add_action('cred_save_data_4376', 'my_save_data_activation_package_30',10,2);
function my_save_data_activation_package_30($post_id, $form_data)
{
if (isset($_POST['package-days']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-package-days', $_POST['package-days'], true);
}
};
3) in the target page("just on a regular WP page" you mentioned above), here is the problem, it is not the package post user created, as a workaround, you can use view to get the package post, for example: create a view list package posts
order by the Post date, descending
filter by a Post author filter
Select posts with the author the same as the current logged in user.
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/
Then we will get the latest "package" post just created by current user, in the view's loop use the same codes you mentioned above:
[wpv-conditional if="( $(wpcf-package-days) eq '30-days' )"][cred_child_link_form form='4397' text='Add post 30"' target='_self'][/wpv-conditional]
"3) in the target page("just on a regular WP page" you mentioned above), here is the problem, it is not the package post user created," - it is not a problem.
Links to forms are included in the appropriate code:
[wpv-if evaluate="(classifieds_check_if_subscription_is_still_valid() = 'yes') AND (classifieds_verify_if_user_is_ad_package_client() = 'yes') AND ('[wpv-current-user info="logged_in"]' = 'true') AND ('[wpv-current-user info="role"]' = 'dealer')"]
[wpv-conditional if="( $(wpcf-package-days) ne '30-days' )"][cred_child_link_form form='730' text='Add post 15' target='_self'][/wpv-conditional]
[wpv-conditional if="( $(wpcf-package-days) eq '30-days' )"][cred_child_link_form form='4397' text='Add post 30' target='_self'][/wpv-conditional]
[/wpv-if]
I changed the code in functions.php to the one you entered.
Unfortunately still does not work well. After you purchase the "Package 30 day" you will see the corresponding link - to the 30 day form. But after the purchase of "Package 15 Days" is still the same link to the 30 day form, and should be a link to the form for 15 days.
Yes, it is only an example for "30-days" package,
You will need apply the same settings to the "15-days" package,
for example,
1) In your CRED form for creating "15-days" package post, setup the field like this:
[cred_generic_field field='package-days' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"15-days"
}
[/cred_generic_field]
2) Add below codes in your theme/functions.php:
add_action('cred_save_data_1234', 'my_save_data_activation_package_15',10,2);
function my_save_data_activation_package_15($post_id, $form_data)
{
if (isset($_POST['package-days']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-package-days', $_POST['package-days'], true);
}
};
Please replace 1234 with the CRED form ID of step 1
3) Use the wpv-conditional shortcode like thisL
[wpv-conditional if="( $(wpcf-package-days) eq '15-days' )"][cred_child_link_form form='730' text='Add post 15' target='_self'][/wpv-conditional]
Hope it is clear
In CRED form for creating "15-days" package post i added:
[cred_generic_field field='package-days' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"15-days"
}
[/cred_generic_field]
I added the code
add_action('cred_save_data_566', 'my_save_data_activation_package_15',10,2);
function my_save_data_activation_package_15($post_id, $form_data)
{
if (isset($_POST['package-days']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-package-days', $_POST['package-days'], true);
}
};
add_action('cred_save_data_4376', 'my_save_data_activation_package_30',10,2);
function my_save_data_activation_package_30($post_id, $form_data)
{
if (isset($_POST['package-days']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-package-days', $_POST['package-days'], true);
}
};
On the links page I added
[wpv-conditional if="( $(wpcf-package-days) eq '15-days' )"][cred_child_link_form form='730' text='Add post 15' target='_self'][/wpv-conditional]
No link is shown now.
Since it is a custom PHP codes problem, please provide a test site with same problem, also point out the problem page URL and CRED form URL, and where I can edit your PHP codes. I need a live website to test and debug. thanks
Thanks for the details, I can login your website, but your website isn't in English, it is hard to debug in it, here is what I found.
1) Login as a "Dealer" user (beta41@onet.pl)
2) Goto URL hidden link
I can not find this text you mentioned in the private message: Kup Pakiet 5
So I can not do the rest test you mentioned in the private message
3) I can see you have put the shortcode [wpv-conditional ...] into a page:
hidden link
It is a wordpress page, it isn't the "Package" post, there isn't such a custom field "wpcf-package-days" in this page, so the shortcode [wpv-conditional ...] won't work.
The shortcodes will be able to check if current "Package" post has custom field "wpcf-package-days", and the custom field "wpcf-package-days" value is "15-days" or "30-days", then output what you want.
So In your case, I suggest you create a view, query "Package" post, filter by the post author is current user, and put the [wpv-conditional ...] shortcode in the view's loop.
display this view in the your page (ID 624), and test again
Idea very cool - thank you. Now show the correct links.
Why are there also links to already used Packages ?...
This is my loop:
<wpv-loop>
[wpv-conditional if="( $(wpcf-package-days) eq '15-days' )"][cred_child_link_form form='730' text='Dodaj ogłoszenie z Pakietu (15 dni) w kat. "Pojazdy osobowe"' target='_self'][/wpv-conditional]<br>
[wpv-conditional if="( $(wpcf-package-days) eq '30-days' )"][cred_child_link_form form='4397' text='Dodaj ogłoszenie z Pakietu (30 dni) w kat. "Pojazdy osobowe"' target='_self'][/wpv-conditional]
</wpv-loop>
Yes, it is expected result, this view will output all package posts created by current user.
I suggest you add a filter in this view, filter by "Package number of ads allowed" > 0, and test again