Skip to content

Instantly share code, notes, and snippets.

@senadir
Created October 14, 2021 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senadir/bbc562e8c5b7170e71052696f55dce64 to your computer and use it in GitHub Desktop.
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
<?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' );
}
<?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