我想通过存储数字字段的输入来计算库存到零。
到目前为止,我有以下几点:
add_action( 'wpcf7_before_send_mail',
    function( $contact_form, $abort, $submission ) {
       $post_id = get_the_id();
       $voorraad = '';
       if ($post_id === 5080){
       $voorraad = get_post_meta(5080,'voorraad',true);
       $newvoorraad = implode( ', ', (array) $submission->get_posted_data( 'number-435' ));
       $stock = $voorraad - $newvoorraad;
       update_post_meta($post_id, 'voorraad', $stock);
         }
    },10,3
    );然而,这是行不通的。
元字段voorraad存在并且具有值450。但是,该字段在输入后不会更新。我不能首先存储提交的数据,即使我使用一个具有不同名称的空元字段,并存储一个像'example‘这样的集合字符串,只是为了测试目的。
如何在提交时将用户输入存储在元字段中?
有什么想法吗?
发布于 2021-03-25 23:14:57
get_the_id()函数不会从表单中检索postID。你必须从$submission->get_meta('container_post_id)上得到它
add_action( 'wpcf7_before_send_mail',
    function( $contact_form, $abort, $submission ) {
        $post_id = $submission->get_meta('container_post_id');
        $voorraad = '';
        if ($post_id === 5080){
            $voorraad = get_post_meta(5080,'voorraad',true);
            $newvoorraad = implode( ', ', (array) $submission->get_posted_data( 'number-435' ));
            $stock = $voorraad - $newvoorraad;
            update_post_meta($post_id, 'voorraad', $stock);
        }
    },10,3
);方法#2
另一种方法是使用联系人表单ID作为触发器,只需将您的帖子ID放在表单中。
add_action('wpcf7_before_send_mail',
    function ($contact_form) {
        if ($contact_form->id() === 123) { // Your Contact Form ID
            $submission = WPCF7_Submission::get_instance();
            $voorraad = '';
            $voorraad = get_post_meta(5080, 'voorraad', true);
            $newvoorraad = implode(', ', (array)$submission->get_posted_data('number-435'));
            $stock = $voorraad - $newvoorraad;
            update_post_meta(5080, 'voorraad', $stock);
        }
    }
);https://stackoverflow.com/questions/66800355
复制相似问题