php - 如何覆盖核心 Woocommerce 插件功能?

标签 php wordpress woocommerce payment-gateway stripe-payments

我正在尝试覆盖 Woocommerce Stripe 插件的输出 HTML。我该怎么做呢?我读过一些关于操作 Hook 和过滤器的文章和文档,但我相信这些仅适用于 Woocommerce 插件,而不适用于 Woo/Stripe 扩展插件。如果 Hook 或过滤器是解决方案,那会是什么样子?我可以修改核心文件或使用 JS,但这些根本不是真正可行的解决方案......

我尝试修改的文件位于:

plugins/woocommerce-gateway-stripe/includes/class-wc-gateway-stripe.php

这是我要在该文件中修改的标记 block :

/**
 * Payment form on checkout page
 */
public function payment_fields() {
    $checked = 1;
    ?>
    <fieldset>
        <?php
            if ( $this->description ) {
                echo wpautop( esc_html( $this->description ) );
            }
            if ( $this->saved_cards && is_user_logged_in() && ( $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) && is_string( $customer_id ) && ( $cards = $this->get_saved_cards( $customer_id ) ) ) {
                ?>
                <p class="form-row form-row-wide">
                    <a class="button" style="float:right;" href="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); ?>#saved-cards"><?php _e( 'Manage cards', 'woocommerce-gateway-stripe' ); ?></a>
                    <?php if ( $cards ) : ?>
                        <?php foreach ( (array) $cards as $card ) : ?>
                            <label for="stripe_card_<?php echo $card->id; ?>">
                                <input type="radio" id="stripe_card_<?php echo $card->id; ?>" name="stripe_card_id" value="<?php echo $card->id; ?>" <?php checked( $checked, 1 ) ?> />
                                <?php printf( __( '%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe' ), (isset( $card->type ) ? $card->type : $card->brand ), $card->last4, $card->exp_month, $card->exp_year ); ?>
                            </label>
                            <?php $checked = 0; endforeach; ?>
                    <?php endif; ?>
                    <label for="new">
                        <input type="radio" id="new" name="stripe_card_id" <?php checked( $checked, 1 ) ?> value="new" />
                        <?php _e( 'Use a new credit card', 'woocommerce-gateway-stripe' ); ?>
                    </label>
                </p>
                <?php
            }
        ?>
        <div class="stripe_new_card" <?php if ( $checked === 0 ) : ?>style="display:none;"<?php endif; ?>
            data-description=""
            data-amount="<?php echo $this->get_stripe_amount( WC()->cart->total ); ?>"
            data-name="<?php echo sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name' ) ); ?>"
            data-label="<?php _e( 'Confirm and Pay', 'woocommerce-gateway-stripe' ); ?>"
            data-currency="<?php echo strtolower( get_woocommerce_currency() ); ?>"
            data-image="<?php echo $this->stripe_checkout_image; ?>"
            >
            <?php if ( ! $this->stripe_checkout ) : ?>
                <?php $this->credit_card_form( array( 'fields_have_names' => false ) ); ?>
            <?php endif; ?>
        </div>
    </fieldset>
    <?php
}

最佳答案

您是正确的,在这种情况下,由于缺少操作/过滤器 Hook ,您无法修改输出。对于 WooCommerce 的所有附加组件(或与此相关的其他插件)来说,情况不一定如此,只是这里恰好是这种情况。

我建议编写一个插件来扩展 Stripe 网关插件提供的类。仅覆盖 payment_fields() 函数以提供您自己的输出 - 请记住,您仍然需要确保基本插件所需的所有内容仍然存在。启用您的插件并在 WooCommerce 配置中选择它作为付款类型。

插件,my-stripe-gateway.php

    require_once ABSPATH . '/wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-gateway-stripe.php';

    if ( ! class_exists( 'WC_Gateway_Stripe' ) )
        return;

    require_once 'my-stripe-gateway-class.php';

    /**
    * Add the Gateway to WooCommerce
    **/
    function add_my_stripe_gateway( $methods ) {
        $methods[] = 'My_Stripe_Gateway';
        return $methods;
    }
    add_filter( 'woocommerce_payment_gateways', 'add_my_stripe_gateway' );
}
add_action( 'plugins_loaded', 'my_stripe_gateway_init', 0 );

自定义支付网关,my-stripe-gateway-class.php

<?php
class My_Stripe_Gateway extends WC_Gateway_Stripe // or whatever the class name is
{
    // call the parent constructor, doesn't happen by default
    public function __construct(){
        parent::__construct();
        $this->id = 'my_stripe_gateway';
        $this->method_title = 'My Stripe Gateway';
        $this->method_description = 'Customized Stripe payment gateway.';
    }
    // override the payment_fields() function
    public function payment_fields(){
        // reproduce/modify this function in your class
        echo "whatever";
    }
}

关于php - 如何覆盖核心 Woocommerce 插件功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26834334/

相关文章:

php - 无法从 PHP 中的另一台服务器连接远程 MySQL 服务器

php - 防止Mysql覆盖数据

PHP 邮件不工作并返回 false

JavaScript 非常简单 : 'Read more' . 。 .' Show Less' 应用于 WordPress 的功能?

php - 在 WooCommerce Checkout 上为每种产品添加数量输入字段

php - WooCommerce API 创建产品变体未按预期工作

php - Laravel 几分钟后自动注销

wordpress - get_avatar_url(get_avatar()) 总是返回默认的 96 大小?

css - 如何在 Wordpress 插件的 css 中形成两列表

php - 在 Woocommerce 中仅显示分组产品