如何通过WooCommerce的管理端更改订单设置支付网关

问题描述:

请帮忙!我试图通过更改管理员的订单详细信息来定义支付网关。如何通过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选项。

后头痛的几天,我发现简单的方法如何展现定义的支付网关,只有当我发链接给客户。

现在客户可以使用默认'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的帮助!

在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支付网关支付的订单都将被标记为处理,而不是保留。

更新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