Pause changes for now, please. I will try another solution.
"If I can add/copy Packages, then each of them can be assigned to the corresponding product and thus determine the number of possible posts.
Can not really expand topic https://toolset.com/forums/topic/add-new-ad-package-when-cloned-does-not-formatpublish-packages-as-normal/ ?" - of course, in this topic any ideas, tips are welcome 🙂
As your request, I don't change anything in your website now.
I have tested above codes in your wenwebsite by these steps:
1) Login with user "info"
2) purchase the product "Wykup -Pakiet 10"
3) After confirm the order, the column "Original number of ads" dose display the value from custom field "product-post-limitation", if you need to use value from other field, you can change the field slug to other field slug.
And it is only an example, for your reference
For the question:
Can not really expand topic https://toolset.com/forums/topic/add-new-ad-package-when-cloned-does-not-formatpublish-packages-as-normal/ ?
The thread above is using CRED action hook to "cred_save_data" to update the "package" post title, you can get the detailed document about action hook "cred_save_data" here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Not sure what and how do you want to expand the topic.
And here is a document for using wordpres function wp_update_post to update the post title
https://codex.wordpress.org/Function_Reference/wp_update_post
Unfortunately, my attempts have not given anything.
I have tried to use such code to make posts in another category count down from the package:
add_action('cred_save_data_324', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$cred_form_id = get_post_meta($package, '_cred_form_id', true);
if(in_array($cred_form_id, array(1731)) ){
$package = get_post_meta($ad_package_id_for_processsing, 'wpcf-package-number-of-ads-allowed', TRUE);
update_post_meta($ad_package_id_for_processsing, 'wpcf-package-number-of-ads-allowed', $updated_package_credit);
};
}
Unfortunately, as you can see, my knowledge of PHP is ridiculous ...
We can return to the topic? Without your help I can not do it.
I thought that if the Packages will work with your method, then posts in other categories can work that way - just add their ID to the code that counts the posts in the Package. Do I think right?
Please do not disable the "WooCommerce - Autocomplete Orders" plugin. He will have to work in the ready-made service. I set it to "All paid orders of any product" mode, so it should not be a problem.
For your new question:
I have tried to use such code to make posts in another category count down from the package
It is a custom codes problem, please describe the question with more details:
1) How and where do you want the custom codes be triggered?
for example in some URL, I fill the form, and click a submit button.
2) Where is the post from?
3) Which category are you talking about?
4) How and where do you want it count down?
5) Which package do you want it count down from?
I am trying to help you on the custom codes problem, but you will need to provide the details. And again, new question, please create new thread for it.
I've been thinking about countdown new posts from different categories from packages, because I think it's strong related to the countdown of posts from different packages.
But maybe I'm wrong and it's better to postulate in a new thread.
Ok, can we go back to the issue of counting posts from different packages?
Current situation:
After the purchase of "Package 10" you can not add a new post because the package is immediately written as "exhausted".
In addition, it is saved as "CRED Auto Draft 929b3c4ef3815d1593c4bb692310f4b5", not enough - the previously purchased package has the same name.
After purchasing "Package 5" you can not add a new post, because the package is immediately written as "exhausted" but "active" (?!).
"Orginal number of ads" is undefined.
The name of the package "Package 5" is good.
I tried to show it to the screen "package-error-3.jpg".
Please, before modifying the functions.php file, download it from the server 🙂
Thanks again for help.
As your request, we can go back to the issue of counting posts from different packages.
We can handle the questions one by one.
To avoid more misunderstand, please confirm what you are going to achieve:
Q1) After the purchase of "Package 10" you can not add a new post because the package is immediately written as "exhausted".
Here is what the steps I tried:
1) Login with user "info", go to page:
hidden link
click button "Wykup -Pakiet 10-", and complete the purchase,
2) Login with admin user, Dashboard-> Packages, I can see the column "Original number of ads", display number "2", see screenshot "Original number of ads.JPG", what do you expect the column "Original number of ads" display?
And what do you expect column "Package number of ads allowed" display?
And if you need custom codes, I need your permission for modifying your theme file.
"Original number of ads" shows the total number of available posts in the package.
"Package number of ads allowed" this field should show how many left over posts can be added to the package.
So if the "Original number of ads" field shows 2 posts and has not yet added a post in this package, then in the "Package number of ads allowed" box should be 2.
If you added 1 post, it should be 1 (because it was available).
If you added 2 posts, it should be 0 (because it was available).
Or:
The "Original number of ads" field shows 5 posts and has not yet been posted in this package, it should be 5 in the "Package number of ads allowed" field.
If you added 1 post, it should be 4.
If you added 2 posts, it should be 3.
If you added 3 posts, it should be 2.
If you added 4 posts, it should be 1.
If you added 5 posts, it should be 0.
Of course you can modify my theme files.
OK, let's handle the question one by one,
First one:
Column "Original number of ads" shows the total number of available posts in the package.
Where is the "available posts" from? is this value from a custom field? From which custom field?
If you can provide the details, I will setup the codes with details for you.
And there is custom codes in your theme/functions.php:
// add the action
//add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );
add_action('woocommerce_order_status_completed','my_hook_func',10,1);
function my_hook_func($order_id)
{
$cred_form_id = get_post_meta($order_id, '_cred_form_id', true);
if(
in_array($cred_form_id, array(1772, 566)) // if it is CRED form 1772 or 566
){
$product_id = $data['cred_product_id']; // get the Product ID
$package_id = get_post_meta($order_id, '_cred_post_id', true); // get the Package ID
$order = wc_get_order( $order_id );
$add_user_post_limitation = 0;
foreach ( $order->get_items() as $item ) {
$product_id = $item['product_id'];
$product_post_limitation = get_post_meta($product_id, 'wpcf-product-post-limitation', true);
$add_user_post_limitation += $product_post_limitation;
}
update_post_meta($package_id, 'wpcf-original-number-of-ads', $add_user_post_limitation); //setup original-number-of-ads field value
}
}
It will be able to do this:
If a user purchase a product, after the woocommerce order is confirmed, it will read the custom field value "product-post-limitation" of product post, and update the new "package", custom field "original-number-of-ads" with the value of field "product-post-limitation", if you need value from other custom field, you can change the custom codes as what you want.
Ok, but "Package 5" still does not show anything in the "Original number of ads"
I wrote about it https://toolset.com/forums/topic/additional-packages/page/3/#post-540457
I can see the problem in your website, it is very strange, it works for the CRED form "Add new Ad package 10" and product "Pakiet 10", Your website is too slow to test and debug, and I am downloading the database dump file you provided in post:
https://toolset.com/forums/topic/additional-packages/page/2/#post-536025
And I am trying to duplicate same problem in my localhost, will feedback if there is anything found
I just tested the custom codes:
https://toolset.com/forums/topic/additional-packages/page/3/#post-541850
in my localhost with the database dump file you provided in post:
https://toolset.com/forums/topic/additional-packages/page/2/#post-536025
It works fine, it can setup the column "Original number of ads" value correctly, since your website are running other plugins and custom theme, I need your permission to deactivate plugins and switch theme in your website. if you can provide a full copy of your website, that will help us to locate the problem more efficiently.
https://toolset.com/faq/provide-supporters-copy-site/
Unfortunately I can not make a copy of my website.
I have restored the database from https://toolset.com/forums/topic/additional-packages/page/2/#post-536025
I also restored the code from https://toolset.com/forums/topic/additional-packages/page/3/#post-541850
Now the problem with "Package 5" has disappeared. "Package 5" properly shows the number of posts available and correctly deducts the added posts from the package. Works well.
In contrast, "Package 10" is written as "exhausted". Shows "Original number of ads" but does not show "Package number of ads allowed".
The name of each purchased "Package 10" is "CRED Auto Draft 929b3c4ef3815d1593c4bb692310f4b5" - of course this should not be.
Disabling plugins and my theme is useless because they all have to work on the final version of the page, but if you need it you can turn it off.
If somehow I can help you, please write boldly.
I am checking it in your website, will feedback if there is anything found
I simply tried these in your website:
1) Deactivate all other plugins except (CRED + CRED commerce plugin + Woocommerce plugin), and switch to wordpress default theme 2017
2) Add the custom codes I mentioned in post:
https://toolset.com/forums/topic/additional-packages/page/2/#post-537002
Into 2017 theme/functions.php
3) Test it in your website
hidden link
Click button "Wykup -Pakiet 5-", and complete the order, I can see in Packages list page:
hidden link
the column value is setup as number 2
Same as above Click button "Wykup -Pakiet 10-", and complete the order, I can see in Packages list page, the column value is setup as number 3
Can you confirm it?
If you can confirm it, then there might be some other compatibility problem in your website.
Now it is worse than it was - https://toolset.com/forums/topic/additional-packages/page/3/#post-542470
Purchased "Package 5" shows only "Original number of ads". In the "Package number of ads allowed" box there is nothing ...
The same in the purchased "Package 10".
In addition, now the name of all packages is the same - "CRED Auto Draft 929b3c4ef3815d1593c4bb692310f4b5".