2016-12-05 38 views
3

請幫忙!我試圖通過更改管理員的訂單詳細信息來定義支付網關。如何通過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選項。

回答

0

後頭痛的幾天,我發現簡單的方法如何展現定義的支付網關,只有當我發鏈接給客戶。

現在客戶可以使用默認'bac'方法進行訂購,並且管理員可以在付款前檢查它。然後管理員將訂單狀態更改爲等待付款並鏈接發送給客戶。當客戶打開鏈接時,我的自定義支付網關就會激活。

我決定使用woocommerce端點來檢查它是否爲'order-pay'頁面。我使用以下代碼:

function show_payment2_payment_gateway($available_gateways) { 

global $woocommerce, $order, $post; 

if (is_wc_endpoint_url('order-pay')) { 

unset($available_gateways['bacs']); 

} 
else { 

unset($available_gateways['payment2']); 

} 

return $available_gateways; 
}   

add_filter('woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1); 

現在它的工作原理與我以前一樣。我希望這會有用。感謝@LoicTheAztec的幫助!

0

在WooCommerce中列出默認支付網關的函數是core_gateways()。這個函數被掛鉤到一個名爲woocommerce_payment_gateways的過濾器。所以,第一步是刪除該過濾器並添加我們自己的過濾器。我將只在主題文件夾內的functions.php文件中工作(請記住,永遠不要修改核心文件)。要做到這一點,我們將使用remove_filter()和的add_filter()函數:

remove_filter('woocommerce_payment_gateways', 'core_gateways'); 
add_filter('woocommerce_payment_gateways', 'my_core_gateways'); 

現在,我們已經刪除了過濾器,你可以看到,在的add_filter()函數,我們有一個回調命名my_core_gateways 。這個回調函數的名字將取代默認的core_gateways()函數。此功能是列出WooCommerce默認支付網關的功能。我將更改該函數的內容並將調用替換爲WC_Gateway_BACS類。該班級是銀行轉帳默認班級。下面是新功能的代碼:

/** 
* core_gateways function modified. 
* 
* @access public 
* @param mixed $methods 
* @return void 
*/ 
function my_core_gateways($methods) { 
    $methods[] = 'WC_Gateway_BACS_custom'; 
    $methods[] = 'WC_Gateway_Cheque'; 
    $methods[] = 'WC_Gateway_COD'; 
    $methods[] = 'WC_Gateway_Mijireh'; 
    $methods[] = 'WC_Gateway_Paypal'; 
    return $methods; 
} 

正如你可以看到我所做的唯一的變化是,我通過WC_Gateway_BACS_custom取代WC_Gateway_BACS。

你還在我身邊嗎?總之,我需要刪除調用默認支付網關的過濾器,並使用自定義函數。在這個自定義函數中,我將調用替換爲BACS類,現在我需要創建這個新的BACS類。爲此,請使用該代碼:

class WC_Gateway_BACS_custom extends WC_Gateway_BACS { 

    /** 
    * Process the payment and return the result 
    * 
    * @access public 
    * @param int $order_id 
    * @return array 
    */ 
    function process_payment($order_id) { 
     global $woocommerce; 

     $order = new WC_Order($order_id); 

     // Mark as processing (that's what we want to change!) 
     $order->update_status('processing', __('Awaiting BACS payment', 'woocommerce')); 

     // Reduce stock levels 
     $order->reduce_order_stock(); 

     // Remove cart 
     $woocommerce->cart->empty_cart(); 

     // Return thankyou redirect 
     return array(
      'result' => 'success', 
      'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('thanks')))) 
     ); 
    } 

} 

在此代碼段中,我只將默認狀態從「等待」更改爲「正在處理」...。並且出現魔法出現!現在,每個使用BACS支付網關支付的訂單都將被標記爲處理,而不是保留。

1

更新2:有關您的意見(和你的問題更新)

你正在使用的鉤子是一個前端掛鉤(而不是管理),所以它不會起作用。

爲了達到目標,當您更新後端(管理員)編輯訂單頁面中的訂單時,需要替換要保存自定義複選框值的函數中的一些代碼。

所以,你的代碼現在是這樣的:

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; 

     // Passing the value to a variable 
     $nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ]; 

     // 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); 

     // Updating securely the data with your conditions 
     if($use_payment2 == 'yes') 
      update_post_meta($post_id, '_payment_method', 'payment2'); 
     else 
      update_post_meta($post_id, '_payment_method', 'bacs');  
    } 
} 

因爲你現在想到這應該工作...

代碼放在您的活動子主題的function.php文件(或主題)。或者也可以在任何插件php文件中使用。

由於從my answers一個驗證碼COMME,你不一定要保持開頭的名稱相同的功能與"mv_"這是有關這個問題的用戶名。你可以把它改成"dan_"例如...


參考:WooCommerce : Add custom Metabox to admin order page

+0

我已經使用我的後端功能更新了複選框的代碼。你可以檢查嗎? – danibeiss

+0

對不起,它不起作用。現在我的複選框不保存狀態值並僅顯示已檢查狀態 – danibeiss

+0

謝謝,我做了一些編輯,現在它在後端運行非常棒。但是,我仍然無法在前端工作。你會檢查我的更新代碼嗎? – danibeiss

相關問題