I have the ability to highlight posts.
This is done by matching the corresponding products with the CPT "Payment" terms.
add_action( 'woocommerce_order_status_completed', 'add_featured_option', 10, 1 );
function add_featured_option( $order_id ) {
$cred_form_id = get_post_meta($order_id, '_cred_form_id', true);
$cred_form_ids = array(39);// here you can add more CRED form IDs
if( in_array($cred_form_id, $cred_form_ids) ){
$order = wc_get_order( $order_id );
$post_id = get_post_meta($order_id, '_cred_post_id', true);
$terms = array();
foreach ( $order->get_items() as $item ) {
$product_id = $item['product_id'];
if($product_id == 1174){ // this is product ID of "home"
$terms[] = 795; // this is term ID of "inhome"
}
if($product_id == 981){ // this is product ID of "featured"
$terms[] = 796; // this is term ID of "featured"
}
if($product_id == 1175){ // this is product ID of "intop"
$terms[] = 797; // this is term ID of "intop"
}
}
if(!empty($terms)){
$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );
}
}
}
I also have CRED forms with which you can extend the post display period.
I want to make the post extension form remove highlights from his meta if they were added earlier.
I'm trying to do this in code:
add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
if ($form_data['id']==2226){
if (isset($_POST['payment']))
{
delete_post_meta($post_id, '__payment', $_POST['payment'], true);
}
}
}
Unfortunately this my code does not work. My PHP knowledge is very poor.
Can someone help me to do this correctly?
Okay I see in your add_featured_option function you are setting some terms in the "payment" taxonomy for the post created by CRED:
$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );
Then in your cred_save_data hook you are attempting to remove some postmeta values from the post edited by CRED:
delete_post_meta($post_id, '__payment', $_POST['payment'], true);
I'm not sure I understand why you are using delete_post_meta. Is some information in the postmeta table? If so, I need to see how it is added so I can determine what syntax should be used.
If you have not used update_post_meta or add_post_meta to save information in postmeta, you should not use delete_post_meta to remove these terms from a post. You should use the same function,
wp_set_object_terms. The documentation for this function is here:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms
Thanks for the advice.
Unfortunately, or did it wrong:
add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
if ($form_data['id']==2226){
if (isset($_POST['payment']))
{
wp_set_object_terms( $terms, null, 'payment' );
}
}
}
or this method does not work.
wp_set_object_terms( $terms, null, 'payment' );
The documentation says that the 1st parameter should be the object ID - the post where you want to remove the terms. Change $terms to $post_id so you are passing the correct information into the wp_set_object_terms function. If this does not work, start adding error log statements to determine where the problem is.
add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
error_log('action hook called');
if ($form_data['id']==2226){
error_log('inside if');
if (isset($_POST['payment']))
{
error_log('payment post is set');
wp_set_object_terms( $post_id, null, 'payment' );
}
}
}
Then you can read these messages in the error log. If you do not know how to access your error log, go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:
define('WP_DEBUG', true);
Then add these lines, just before it says 'stop editing here':
ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
Then submit the CRED form again. This will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php.
Here is the file "error_log.txt:
[08-Aug-2017 19:53:54 UTC] action hook called
[08-Aug-2017 19:54:17 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/betamat/public_html/auto-box/wp-content/plugins/woocommerce/includes/class-wc-order-item-product.php on line 169
[08-Aug-2017 19:54:18 UTC] PHP Notice: Undefined index: order_on-hold in /home/betamat/public_html/auto-box/wp-content/plugins/cred-commerce/classes/Form_Handler.php on line 82
[08-Aug-2017 19:54:25 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:25 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:25 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice: Trying to get property of non-object in /home/betamat/public_html/auto-box/wp-content/plugins/toolset-classifieds/inc/message_system.class.php on line 368
[08-Aug-2017 19:54:41 UTC] action hook called
[08-Aug-2017 19:54:41 UTC] inside if
[08-Aug-2017 19:54:56 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/betamat/public_html/auto-box/wp-content/plugins/woocommerce/includes/class-wc-order-item-product.php on line 169
[08-Aug-2017 19:54:57 UTC] PHP Notice: Undefined index: order_on-hold in /home/betamat/public_html/auto-box/wp-content/plugins/cred-commerce/classes/Form_Handler.php on line 82
[08-Aug-2017 19:55:07 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:08 UTC] PHP Notice: get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:08 UTC] PHP Notice: Trying to get property of non-object in /home/betamat/public_html/auto-box/wp-content/plugins/toolset-classifieds/inc/message_system.class.php on line 368
Notice that you see all the log messages except "payment post is set"? This means that the conditional just before the last log message was not true:
if (isset($_POST['payment']))
In plain language, this means that the form did not contain any value for an input named "payment". That could mean that the input field is not included in your form, or the input value is empty. Can you copy + paste the markup from your CRED form editor here? I need to see how you implemented the "payment" field.
Well... I did not think about it...
In CREDIT form there is nothing associated with "payment". There is only a button that leads to WooCommerce payment.
[credform class="cred-form cred-keep-original"]
[cred_field field="form_submit" value="Extend"]
[/credform]
Even I have no idea how to insert it into the CRED form.
I see, in this case the payment isset conditional is probably not necessary. Is there any reason why submitting this form should not remove the terms? Anytime this form is submitted, all 'payment' taxonomy terms should be removed from the CRED post. Is that correct? This conditional can be removed, unless you disagree. The updated code, without any error logging:
add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
if ($form_data['id']==2226){
wp_set_object_terms( $post_id, null, 'payment' );
}
}
It works great. Thank you very much.
By the way, I noticed another problem with the code that I showed in https://toolset.com/forums/topic/deleting-cpt-terms-from-meta-post/#post-557262 This code that matches the corresponding products to the CPT "Payment" terms.
If I have already added a "Payment" option and want to add another, then this one added earlier is deactivated. For example: I have added the "featured" option and added the "intop" option, the "featured" option disappears, no longer assigned to the post.
It should be possible to add all three options.
I'm trying to change that, but I'm not capable ...
Can you help me change that, or do I have to create a new topic?
Hi, I think this answer will be more than a sentence or two, so please create a new topic. We can help you remove all the terms, or add to the existing terms, or pick and choose terms to keep, however you would like. Thanks!
Ok.
Thank you once again.