Skip to content

Instantly share code, notes, and snippets.

@JPry
Created June 20, 2019 23:47
Show Gist options
  • Save JPry/1661079fa689c92c3e2cc25946edc2d8 to your computer and use it in GitHub Desktop.
Save JPry/1661079fa689c92c3e2cc25946edc2d8 to your computer and use it in GitHub Desktop.
Code snippet to allow customers to pay for an order without logging in.
<?php
/**
* Allow orders created by an admin to be paid without the user logging in.
*
* @param array $allcaps All capabilities of the user.
* @param array $caps Capabilities requested.
* @param array $args Extra Arguments.
*
* @return array The filtered capabilities of the user.
*/
function jp_custom_order_caps( $allcaps, $caps, $args ) {
if ( ! isset( $caps[0], $args[2] ) ) {
return $allcaps;
}
switch ( $caps[0] ) {
case 'pay_for_order':
$order = wc_get_order( $args[2] );
if ( 'admin' === $order->get_created_via() ) {
$allcaps['pay_for_order'] = true;
}
break;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'jp_custom_order_caps', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment