, пожалуйста, помогите! Я пытаюсь определить Payment Gateway, изменив данные заказа у администратора.Как установить Платежный шлюз путем изменения заказа от администратора в WooCommerce
В качестве опции по умолчанию я хочу использовать платежный шлюз «bacs». Клиент делает заказ, а затем я хочу изменить порядок и превратить способ оплаты в таможенный шлюз «payment2».
Для этого я сделал метабокс с флажком, который должен включать/выключать метод «payment2» и отменять «bacs» по умолчанию. Флажок работает правильно.
Но, я не могу заставить его работать. Прежде всего, я не могу получить мета-сообщение со значением флажка. Проверьте код, приведенный ниже, пожалуйста:
function show_payment2_payment_gateway($available_gateways) {
$use_payment2 = get_post_meta($post->ID, 'use_payment2', true);
if($use_payment2 == "yes") {
unset($available_gateways['bacs']);
}
else {
unset($available_gateways['payment2']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1);
UPD
Это мой код для серверного флажка. Как я уже сказал, что это хорошо работает и сохранить мета-значение, как «да»
//
//Adding Meta container admin shop_order pages
//
add_action('add_meta_boxes', 'mv_add_meta_boxes');
if (! function_exists('mv_add_meta_boxes'))
{
function mv_add_meta_boxes()
{
global $woocommerce, $order, $post;
add_meta_box('mv_other_fields', __('PAYMENT2','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core');
}
}
//
//adding Meta field in the meta container admin shop_order pages
//
if (! function_exists('mv_save_wc_order_other_fields'))
{
function mv_add_other_fields_for_packaging()
{
global $woocommerce, $order, $post;
$meta_field_data = get_post_meta($post->ID, 'use_payment2', true);
$meta_field_data_checked = $meta_field_data["use_payment2"][0];
if($meta_field_data == "yes") $meta_field_data_checked = 'checked="checked"';
echo '
<label for="use_epay">TURN PAYMENT2 ON?</label>
<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">
<input type="checkbox" name="use_payment2" value="yes" '.$meta_field_data_checked.'>';
}
}
//
//Save the data of the Meta field
//
add_action('save_post', 'mv_save_wc_order_other_fields', 10, 1);
if (! function_exists('mv_save_wc_order_other_fields'))
{
function mv_save_wc_order_other_fields($post_id) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if (! isset($_POST[ 'mv_other_meta_field_nonce' ])) {
return $post_id;
}
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
//Verify that the nonce is valid.
if (! wp_verify_nonce($nonce)) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check the user's permissions.
if ('page' == $_POST[ 'post_type' ]) {
if (! current_user_can('edit_page', $post_id)) {
return $post_id;
}
} else {
if (! current_user_can('edit_post', $post_id)) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
update_post_meta($post_id, 'use_payment2', $_POST[ 'use_payment2' ]);
}
}
UPD
Это рабочий код для Back-End (включить переключатель METABOX). Это сохранить значение флажка и способ оплаты изменения в деталях заказа:
//
//Adding Meta container admin shop_order pages
//
add_action('add_meta_boxes', 'mv_add_meta_boxes');
if (! function_exists('mv_add_meta_boxes'))
{
function mv_add_meta_boxes()
{
global $woocommerce, $order, $post;
add_meta_box('mv_other_fields', __('PAYMENT2','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core');
}
}
//
//adding Meta field in the meta container admin shop_order pages
//
if (! function_exists('mv_save_wc_order_other_fields'))
{
function mv_add_other_fields_for_packaging()
{
global $woocommerce, $order, $post;
$meta_field_data = get_post_meta($post->ID, 'use_payment2', true);
echo '<label for="use_payment2">USE PAYMENT2?</label>
<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">';
if($meta_field_data == "yes") {
$meta_field_data_checked = 'checked="checked"';
echo'<input type="checkbox" name="use_payment2" value="yes" '.$meta_field_data_checked.'>';
}
else {
echo'<input type="checkbox" name="use_payment2" value="yes">';
}
}
}
//Save the data of the Meta field
add_action('save_post', 'mv_save_wc_order_other_fields', 10, 1);
if (! function_exists('mv_save_wc_order_other_fields'))
{
function mv_save_wc_order_other_fields($post_id) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if (! isset($_POST[ 'mv_other_meta_field_nonce' ])) {
return $post_id;
}
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
//Verify that the nonce is valid.
if (! wp_verify_nonce($nonce)) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check the user's permissions.
if ('page' == $_POST[ 'post_type' ]) {
if (! current_user_can('edit_page', $post_id)) {
return $post_id;
}
} else {
if (! current_user_can('edit_post', $post_id)) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
$use_payment2 = sanitize_text_field($_POST[ 'use_payment2' ]);
update_post_meta($post_id, 'use_payment2', $use_payment2);
if($_POST[ 'use_payment2' ] == 'yes') {
update_post_meta($post_id, '_payment_method', 'payment2');
}
elseif (get_post_meta($post_id, '_payment_method', true) != 'bacs') {
update_post_meta($post_id, '_payment_method', 'bacs');
}
}
}
Но, как я могу использовать флажок состояние на моем переднем конце? Я до сих пор не могу получить значение флажка, используя этот код:
function show_payment2_payment_gateway($available_gateways) {
global $woocommerce, $order, $post;
$payment_method = get_post_meta($post_id, 'use_payment2', true);
if(isset($payment_method) == 'yes') {
unset($available_gateways['bacs']);
}
else {
unset($available_gateways['payment2']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1);
Теперь, это всегда показывает вариант payment2 даже если флажок установлен или снят.
Я обновил код с помощью моей функции backend для флажка. Не могли бы вы проверить пожалуйста? – danibeiss
К сожалению, он не работает. Теперь мой флажок не сохраняет значение состояния и показывает только проверенное состояние – danibeiss
Спасибо, я внес некоторые изменения и теперь он работает потрясающе в Backend. Но я все еще не могу заставить его работать над интерфейсом. Не могли бы вы проверить мой обновленный код? – danibeiss