Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pay without need to login WooCommerce

For a WooCommerce webshop we send out a lot of payment links through email. Before getting to the payment page customers are obligated to login first. We would like the customer to be able to complete payment without logging in as often they don't know their password because of different company departments.

I found this code but this only lets the administrator pay without logging in:

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);
like image 859
Mas Avatar asked Jan 01 '26 13:01

Mas


2 Answers

here is working function with all users just test it :

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 );
like image 98
kashalo Avatar answered Jan 04 '26 22:01

kashalo


I have created a different solution for this problem, allowing anyone who has the WooCommerce-generated Payment URL (which includes the Order Key) to complete the payment for that order. (So we retain some of the security/protection, rather than just allowing anyone to pay for anything and see any order.)

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

Using this function, a user visiting a URL which has an Order Number, and the associated Order Key, will be able to complete the payment, but if the Order Key is not valid, or not present, then it will fail.

Examples:

  • your.domain/checkout/order-pay/987/?pay_for_order=true&key=wc_order_5c4155dd4462c
    PASS if "wc_order_5c4155dd4462c" is Order Key for Order #987
  • your.domain/checkout/order-pay/987/?pay_for_order=true
    FAIL as No Key Parameter Present
  • your.domain/checkout/order-pay/987/
    FAIL as No Key Parameter Present
like image 32
Luke Stevenson Avatar answered Jan 04 '26 22:01

Luke Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!