如何将自定义支付网关与 WooCommerce Checkout 区块集成

从 8.3 开始的 WooCommerce版本中,您可能会注意到您的自定义付款方式在结账区块中不可用。

例如,如果您尝试停用商店中除自定义付款方式之外的所有付款方式,您可能会收到如下错误消息:

这里我使用 Storefront 主题作为示例,但任何 WordPress 主题都会出现相同的错误消息。

但可以肯定的是,当您使用旧的[woocommerce_checkout]简码时,一切都运行良好。

是的,之前发布的支付网关教程似乎不再完整,但我们今天将通过这个额外的教程来更改它,我将逐步指导您应该做什么,以添加自定义的兼容性WooCommerce 购物车和结帐块的 WooCommerce 付款方式。

这就是我们在本教程结束时要实现的目标:

当然,我还会向您展示一些巧妙的额外内容,例如为您的付款方式添加自定义图标。

文章目录

服务器端集成

首先,让我们从服务器端集成开始,我很确定你们中的许多人都觉得使用 PHP 开发比 JavaScript + React 更舒服,所以让我们从简单的事情开始。

注册一个块支持 PHP 类

"区块支持PHP类"是除主要支付网关类之外的PHP类。我们将使用下面的简单代码片段来注册它,这与我们在 woocommerce_payment_gateways hook 中注册主网关类时所做的类似。

复制代码
  1. add_action( 'woocommerce_blocks_loaded', 'rudr_gateway_block_support' );
  2. function rudr_gateway_block_support() {
  3. // if( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
  4. // return;
  5. // }
  6. // 在这里引入 "gateway block support class" 文件
  7. require_once __DIR__ . '/includes/class-wc-misha-gateway-blocks-support.php';
  8. // 注册我们刚才引入的 PHP 类
  9. add_action(
  10. 'woocommerce_blocks_payment_method_type_registration',
  11. function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
  12. $payment_method_registry->register( new WC_Misha_Gateway_Blocks_Support );
  13. }
  14. );
  15. }

请记住以下几点:

  • 我注销了class_exists()条件,因为我们不再需要它,因为结账块现在是 WooCommerce 的一部分,而不是独立的插件。
  • 我们的块支持 PHP 类本身将位于一个单独的文件中,我们将在下一步中class-wc-misha-gateway-blocks-support.php查看它。

块支持 PHP 类

在这一部分中,我将创建一个WC_Misha_Gateway_Blocks_Support PHP 类来扩展 WooCommerce 的 AbstractPaymentMethodType 类。同时不要忘记我们已经有了扩展 WC_Payment_GatewayWC_Misha_Gateway 类。

就我而言,我将其放入includes/class-wc-misha-gateway-blocks-support.php.

复制代码
  1. <?php
  2. use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
  3. final class WC_Misha_Gateway_Blocks_Support extends AbstractPaymentMethodType {
  4. private $gateway;
  5. protected $name = 'misha'; // payment gateway id
  6. public function initialize() {
  7. // get payment gateway settings
  8. $this->settings = get_option( "woocommerce_{$this->name}_settings", array() );
  9. // you can also initialize your payment gateway here
  10. // $gateways = WC()->payment_gateways->payment_gateways();
  11. // $this->gateway = $gateways[ $this->name ];
  12. }
  13. public function is_active() {
  14. return ! empty( $this->settings[ 'enabled' ] ) && 'yes' === $this->settings[ 'enabled' ];
  15. }
  16. public function get_payment_method_script_handles() {
  17. wp_register_script(
  18. 'wc-misha-blocks-integration',
  19. plugin_dir_url( __DIR__ ) . 'build/index.js',
  20. array(
  21. 'wc-blocks-registry',
  22. 'wc-settings',
  23. 'wp-element',
  24. 'wp-html-entities',
  25. ),
  26. null, // or time() or filemtime( ... ) to skip caching
  27. true
  28. );
  29. return array( 'wc-misha-blocks-integration' );
  30. }
  31. public function get_payment_method_data() {
  32. return array(
  33. 'title' => $this->get_setting( 'title' ),
  34. // almost the same way:
  35. // 'title' => isset( $this->settings[ 'title' ] ) ? $this->settings[ 'title' ] : 'Default value';
  36. 'description' => $this->get_setting( 'description' ),
  37. // if $this->gateway was initialized on line 15
  38. // 'supports' => array_filter( $this->gateway->supports, [ $this->gateway, 'supports' ] ),
  39. // example of getting a public key
  40. // 'publicKey' => $this->get_publishable_key(),
  41. );
  42. }
  43. //private function get_publishable_key() {
  44. // $test_mode = ( ! empty( $this->settings[ 'testmode' ] ) && 'yes' === $this->settings[ 'testmode' ] );
  45. // $setting_key = $test_mode ? 'test_publishable_key' : 'publishable_key';
  46. // return ! empty( $this->settings[ $setting_key ] ) ? $this->settings[ $setting_key ] : '';
  47. //}
  48. }

首先让我们看一下类的属性和方法。

属性:

  • $name-- 这是此步骤中的支付网关 ID 。
  • $gateway-- 我们可以在这里存储支付网关对象的实例,但这不是必需的,所以我在代码中注释了这部分。

方法:

  • is_active(),
  • get_payment_method_script_handles()-- 这是我们包含 JavaScript 文件的地方,其中包含集成的客户端部分
  • get_payment_method_data()-- 提供您将在前端作为关联数组使用的所有必要数据。

您还可以使用index.asset.php来获取脚本版本和依赖项。

复制代码
  1. public function get_payment_method_script_handles() {
  2. $asset_path = plugin_dir_path( __DIR__ ) . 'build/index.asset.php';
  3. $version = null;
  4. $dependencies = array();
  5. if( file_exists( $asset_path ) ) {
  6. $asset = require $asset_path;
  7. $version = isset( $asset[ 'version' ] ) ? $asset[ 'version' ] : $version;
  8. $dependencies = isset( $asset[ 'dependencies' ] ) ? $asset[ 'dependencies' ] : $dependencies;
  9. }
  10. wp_register_script(
  11. 'wc-misha-blocks-integration',
  12. plugin_dir_url( __DIR__ ) . 'build/index.js',
  13. $dependencies,
  14. $version,
  15. true
  16. );
  17. return array( 'wc-misha-blocks-integration' );
  18. }

声明兼容性

当您想让用户知道您的付款方式与 WooCommerce Checkout 块不兼容时,此部分通常很有用。

当用户尝试编辑古腾堡中的结账页面时,他们将收到通知:

方法如下:

复制代码
  1. add_action( 'before_woocommerce_init', 'rudr_cart_checkout_blocks_compatibility' );
  2. function rudr_cart_checkout_blocks_compatibility() {
  3. if( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
  4. \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
  5. 'cart_checkout_blocks',
  6. __FILE__,
  7. false // true (compatible, default) or false (not compatible)
  8. );
  9. }
  10. }

客户端集成

设置项目

再次强调,我希望本教程中的内容保持超级简单,因此我将使用@wordpress/scripts即可。

在我们的构建中,您肯定可以进一步配置 WooCommerce 混合构建,这样您就可以使用import { registerPaymentMethod } from ....

这就是我的文件夹结构的样子:

为 WooCommerce Checkout 区块注册自定义付款方式

以下是/src/index.js文件,以防您有疑问。

复制代码
  1. import { decodeEntities } from '@wordpress/html-entities';
  2. const { registerPaymentMethod } = window.wc.wcBlocksRegistry
  3. const { getSetting } = window.wc.wcSettings
  4. const settings = getSetting( 'misha_data', {} )
  5. const label = decodeEntities( settings.title )
  6. const Content = () => {
  7. return decodeEntities( settings.description || '' )
  8. }
  9. const Label = ( props ) => {
  10. const { PaymentMethodLabel } = props.components
  11. return <PaymentMethodLabel text={ label } />
  12. }
  13. registerPaymentMethod( {
  14. name: "misha",
  15. label: <Label />,
  16. content: <Content />,
  17. edit: <Content />,
  18. canMakePayment: () => true,
  19. ariaLabel: label,
  20. supports: {
  21. features: settings.supports,
  22. }
  23. })

也许详细讨论 registerPaymentMethod() 以及讨论 registerExpressPaymentMethod()是一个好主意,但我认为我们将在我的博客上的下一个教程中更深入地研究特定示例。

最后!恭喜你!

如果您想知道付款方式标题和说明的来源:

添加付款方式图标

由于我向您承诺了更多示例,并且您可能不想等到下一个教程,所以让我们从这个开始。

我现在的目标是在 WooCommerce Checkout 块中的自定义支付网关标题附近显示一个图标:

首先,让我们修改我们的块支持 PHP 类,特别是它的get_payment_method_data(),我们将在那里提供另一个参数:

复制代码
  1. public function get_payment_method_data() {
  2. return array(
  3. 'title' => $this->get_setting( 'title' ),
  4. 'description' => $this->get_setting( 'description' ),
  5. 'icon' => plugin_dir_url( __DIR__ ) . 'assets/icon.png',
  6. ...

然后我建议为其创建另一个 React 组件:

复制代码
  1. const Icon = () => {
  2. return settings.icon
  3. ? <img src={settings.icon} style={``{ float: 'right', marginRight: '20px' }} />
  4. : ''
  5. }
  6. const Label = () => {
  7. return (
  8. <span style={``{ width: '100%' }}>
  9. {label}
  10. <Icon />
  11. </span>
  12. );
  13. };

如果未提供图标图像 URL,则不会显示<img>标签,太棒了!

相关推荐
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082855 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe5 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5