請幫忙!我試圖通過更改管理員的訂單詳細信息來定義支付網關。如何通過WooCommerce的管理端更改訂單設置支付網關
作爲默認選項,我想使用'bac'支付網關。客戶訂購,然後我想更改訂單並將付款方式轉爲自定義的「payment2」網關。
爲此,我製作了帶複選框的元框,它應該打開/關閉'payment2'方法並取消默認'bac'。複選框正常工作。
但是,我無法得到它的工作。首先,我無法使用複選框值獲取後期元。校驗碼下面有請:
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
這是我的後端代碼複選框。正如我說這是運作良好,並保存meta值的「是」
//
//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
用於後端(自定義複選框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選項。
我已經使用我的後端功能更新了複選框的代碼。你可以檢查嗎? – danibeiss
對不起,它不起作用。現在我的複選框不保存狀態值並僅顯示已檢查狀態 – danibeiss
謝謝,我做了一些編輯,現在它在後端運行非常棒。但是,我仍然無法在前端工作。你會檢查我的更新代碼嗎? – danibeiss