对于WooCommerce网上商店,我们会通过电子邮件发送大量付款链接。在进入付款页面之前,客户有义务先登录。我们希望客户能够在不登录的情况下完成付款,因为公司部门不同,他们通常不知道密码。
我找到了这段代码,但这只让管理员付费而无需登录:
function your_custom_function_name($allcaps, $caps, $args)
{
if (isset($caps[0])) {
switch ($caps[0]) {
case 'pay_for_order':
$user_id = $args[1];
$order_id = isset($args[2]) ? $args[2] : null;
// When no order ID, we assume it's a new order
// and thus, customer can pay for it
if (!$order_id) {
$allcaps['pay_for_order'] = true;
break;
}
$user = get_userdata($user_id);
if (in_array('administrator', (array)$user->roles)) {
$allcaps['pay_for_order'] = true;
}
$order = wc_get_order($order_id);
if ($order && ($user_id == $order->get_user_id() || !$order - > get_user_id())) {
$allcaps['pay_for_order'] = true;
}
break;
}
}
return $allcaps;
}
add_filter('user_has_cap', 'your_custom_function_name', 10, 3);
答案 0 :(得分:3)
这是工作功能,所有用户只需测试它:
function your_custom_function_name( $allcaps, $caps, $args ) {
if ( isset( $caps[0] ) ) {
switch ( $caps[0] ) {
case 'pay_for_order' :
$order_id = isset( $args[2] ) ? $args[2] : null;
$order = wc_get_order( $order_id );
$user = $order->get_user();
$user_id = $user->ID;
// When no order ID, we assume it's a new order
// and thus, customer can pay for it
if ( ! $order_id ) {
$allcaps['pay_for_order'] = true;
break;
}
$order = wc_get_order( $order_id );
if ( $order && ( $user_id == $order->get_user_id() || ! $order->get_user_id() ) ) {
$allcaps['pay_for_order'] = true;
}
break;
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'your_custom_function_name', 10, 3 );
答案 1 :(得分:1)
我为这个问题创建了一个不同的解决方案,允许拥有WooCommerce生成的Payment URL(包括Order Key)的任何人完成该订单的付款。 (因此,我们保留了一些安全性/保护性,而不仅仅是允许任何人支付任何费用并查看任何订单。)
function allow_payment_without_login( $allcaps, $caps, $args ) {
// Check we are looking at the WooCommerce Pay For Order Page
if ( !isset( $caps[0] ) || $caps[0] != 'pay_for_order' )
return $allcaps;
// Check that a Key is provided
if ( !isset( $_GET['key'] ) )
return $allcaps;
// Find the Related Order
$order = wc_get_order( $args[2] );
if( !$order )
return $allcaps; # Invalid Order
// Get the Order Key from the WooCommerce Order
$order_key = $order->get_order_key();
// Get the Order Key from the URL Query String
$order_key_check = $_GET['key'];
// Set the Permission to TRUE if the Order Keys Match
$allcaps['pay_for_order'] = ( $order_key == $order_key_check );
return $allcaps;
}
add_filter( 'user_has_cap', 'allow_payment_without_login', 10, 3 );
使用此功能,访问具有订单号和关联的订单密钥的URL的用户将能够完成付款,但是如果该订单密钥无效或不存在,则它将失败。
示例: