Приветствую, Игорь!
В общем да, но единственное мне нужно автоматически обновлять форму при изменении кастомного поля(не категории).
Я еще раз все пересмотрел, да Игорь, ты был прав, к полям можно подступиться через имеющие хуки. В частности hivepress/v1/models/listing/fields
и hivepress/v1/forms/listing_update
. Моя ошибка была, что приоритет в плагине у этих хуков 100, а у меня было 10, поэтому у меня еще не успели прогрузиться атрибуты
Я добился всего того же что и через свой кастомный хук, единственная проблема которая осталась, что на этапе сохранения формы, он не сохраняет ее потому-что атрибут price не передается, хотя он на него и не ругается, просто как-будто происходит перезагрузка страницы и все, но если сделать без условий unset($fields['price']);
в hivepress/v1/models/listing/fields
то он все сохранит нормально.
Я предполагаю что не хватает условия на каком-то этапе, чтобы удалился атрибут, у тебя случаем нет никаких идей?
Greetings, Igor!
In general, yes, but in the singular, I need to automatically sell the form to use a custom field (not a category).
I reviewed everything again, yes Igor, you were right, you can approach the fields through hook events. In particular, hivepress/v1/models/listing/fields
and hivepress/v1/forms/listing_update
. My mistake was that these hooks have a priority of 100, and I had 10, so I didn’t have time to load the attributes
I achieved everything the same as through my custom hook, the only problem that remains is that at the stage of saving the form, it does not save it because the price attribute is not passed, although it does not swear at it, it’s just as if the page is reloaded and that’s all, but if you do without conditions unset($fields['price']);
in hivepress/v1/models/listing/fields
then it will save everything normally.
I’m assuming it’s missing a condition at some stage to remove the attribute, do you have any ideas?
Мой код | My code
add_filter( 'hivepress/v1/forms/listing_update', [ $this, 'alter_update_form' ], 1000, 2 );
add_filter( 'hivepress/v1/models/listing/fields', [ $this, 'my_pet_model_fields' ], 1000,2);
public function alter_update_form( $form_args, $form ) {
$fields = $form->get_model()->_get_fields();
if($fields['listing_type']->get_value() == 1){
unset($form_args['fields']['price']);
}
if (!hp\is_rest() && isset($_GET['listing_type']) && (int) $_GET['listing_type'] == 1){
unset($form_args['fields']['price']);
}
if(hp\is_rest() && isset($form_args['fields']['price']) && isset($_POST['listing_type']) && $_POST['listing_type'] == 1){
unset($form_args['fields']['price']);
}
if(!hp\is_rest() && isset($form_args['fields']['price']) && !is_null(hivepress()->request->get_context('listing')) && hivepress()->router->get_current_route_name() == 'listing_edit_page'){
$meta = get_post_meta(hivepress()->request->get_context('listing')->get_id(), 'hp_listing_type', true);
if($meta == 1){
unset($form_args['fields']['price']);
}
} elseif (!hp\is_rest() && isset($form_args['fields']['price']) && !is_null(hivepress()->request->get_context('listing')) && hivepress()->router->get_current_route_name() == 'listing_submit_details_page'){
$meta = get_post_meta(hivepress()->request->get_context('listing')->get_id(), 'hp_listing_type', true);
if($meta == 1){
unset($form_args['fields']['price']);
}
}
$form_args = hp\merge_arrays(
$form_args,
[
'fields' => [
'listing_type' => [
'label' => 'Тип объявления',
'type' => 'select',
'_order' => 1,
'default' => isset($_GET['listing_type']) ? (int) $_GET['listing_type'] : 0,
'attributes' => [
'data-render' => hivepress()->router->get_url( 'form_resource', [ 'form_name' => $form::get_meta( 'name' ) ] ),
],
],
],
]
);
return $form_args;
}
public function my_pet_model_fields( $fields,$model ) {
$model_fields = $model->_get_fields();
if($model_fields['listing_type']->get_value() == 1){
unset($fields['price']);
}
if (!hp\is_rest() && isset($_GET['listing_type']) && (int) $_GET['listing_type'] == 1){
unset($fields['price']);
}
if(hp\is_rest() && isset($fields['price']) && isset($_POST['listing_type']) && $_POST['listing_type'] == 1){
unset($fields['price']);
}
if(!hp\is_rest() && isset($fields['price']) && !is_null(hivepress()->request->get_context('listing')) && hivepress()->router->get_current_route_name() == 'listing_edit_page'){
$meta = get_post_meta(hivepress()->request->get_context('listing')->get_id(), 'hp_listing_type', true);
if($meta == 1){
unset($fields['price']);
}
} elseif (!hp\is_rest() && isset($fields['price']) && !is_null(hivepress()->request->get_context('listing')) && hivepress()->router->get_current_route_name() == 'listing_submit_details_page'){
$meta = get_post_meta(hivepress()->request->get_context('listing')->get_id(), 'hp_listing_type', true);
if($meta == 1){
unset($fields['price']);
}
}
if(isset($fields['price'])){
var_dump($model_fields);
}
return $fields;
}