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

2024-02-2312:37:22后端程序开发Comments388 views字数 5779阅读模式

最新版本的 WooCommerce(我猜是从 8.3 开始)中,您可能会注意到您的自定义付款方式在结账区块中不可用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

例如,如果您尝试停用商店中除自定义付款方式之外的所有付款方式,您可能会收到如下错误消息:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

如何将自定义支付网关与 WooCommerce Checkout 区块集成
这里我使用 Storefront 主题作为示例,但任何 WordPress 主题都会出现相同的错误消息。

但可以肯定的是,当您使用旧的[woocommerce_checkout]简码时,一切都运行良好。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

是的,之前发布的支付网关教程似乎不再完整,但我们今天将通过这个额外的教程来更改它,我将逐步指导您应该做什么,以添加自定义的兼容性WooCommerce 购物车和结帐块的 WooCommerce 付款方式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

这就是我们在本教程结束时要实现的目标:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

当然,我还会向您展示一些巧妙的额外内容,例如为您的付款方式添加自定义图标。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

服务器端集成

首先,让我们从服务器端集成开始,我很确定你们中的许多人都觉得使用 PHP 开发比 JavaScript + React 更舒服,所以让我们从简单的事情开始。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

注册一个块支持 PHP 类

“区块支持PHP类”是除主要支付网关类之外的PHP类。我们将使用下面的简单代码片段来注册它,这与我们在 woocommerce_payment_gateways hook 中注册主网关类时所做的类似。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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. }

请记住以下几点:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

块支持 PHP 类

在这一部分中,我将创建一个WC_Misha_Gateway_Blocks_Support PHP 类来扩展 WooCommerce 的 AbstractPaymentMethodType 类。同时不要忘记我们已经有了扩展 WC_Payment_Gateway 的WC_Misha_Gateway 类。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

就我而言,我将其放入includes/class-wc-misha-gateway-blocks-support.php.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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. }

首先让我们看一下类的属性和方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

属性:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

您还可以使用index.asset.php来获取脚本版本和依赖项。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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 块不兼容时,此部分通常很有用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

当用户尝试编辑古腾堡中的结账页面时,他们将收到通知:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

方法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

在我们的构建中,您肯定可以进一步配置 WooCommerce 混合构建,这样您就可以使用import { registerPaymentMethod } from ....文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

这就是我的文件夹结构的样子:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

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

以下是/src/index.js文件,以防您有疑问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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()是一个好主意,但我认为我们将在我的博客上的下一个教程中更深入地研究特定示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

最后!恭喜你!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

如果您想知道付款方式标题和说明的来源:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

添加付款方式图标

由于我向您承诺了更多示例,并且您可能不想等到下一个教程,所以让我们从这个开始。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

我现在的目标是在 WooCommerce Checkout 块中的自定义支付网关标题附近显示一个图标:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

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

首先,让我们修改我们的块支持 PHP 类,特别是它的get_payment_method_data(),我们将在那里提供另一个参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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 组件:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  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>标签,太棒了!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/61045.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/61045.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定