-
-
Save senadir/bbc562e8c5b7170e71052696f55dce64 to your computer and use it in GitHub Desktop.
To add a new payment requirements to your plugin, you use register_payment_requirements. This is an example from WooCommerce Bookings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Somewhere in includes/blocks/class-wc-bookings-gateway.php | |
/** | |
* Returns an array of supported features. | |
* | |
* @return string[] | |
*/ | |
public function get_supported_features() { | |
return array( 'products', 'booking_availability' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Automattic\WooCommerce\Blocks\Package; | |
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi; | |
add_action('woocommerce_blocks_loaded', function() { | |
// ExtendRestApi is stored in the container as a shared instance between the API and consumers. | |
// You shouldn't initiate your own ExtendRestApi instance using `new ExtendRestApi` but should | |
// always use the shared instance from the Package dependency injection container. | |
$extend = Package::container()->get( ExtendRestApi::class ); | |
$extend->register_payment_requirements( | |
array( | |
'data_callback' => 'inject_payment_feature_requirements_for_cart_api', | |
) | |
); | |
}); | |
/** | |
* Check the content of the cart and add required payment methods. | |
* | |
* | |
* @return array list of features required by cart items. | |
*/ | |
function inject_payment_feature_requirements_for_cart_api() { | |
// No bookings in the cart, no need to add anything. | |
if ( ! Pseudo_Booking_Class::cart_contains_booking() ) { | |
return array(); | |
} | |
// No booking in cart that requires availability. | |
if ( ! Pseudo_Booking_Class::bookings_require_availability() ) { | |
return array(); | |
} | |
return array( 'booking_availability' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment