簡體   English   中英

如何覆蓋Woocommerce核心插件功能?

[英]How can I override core Woocommerce plugin function?

我正在嘗試覆蓋Woocommerce Stripe插件的輸出HTML。 我將如何去做? 我已經閱讀了一些有關動作鈎子和過濾器的文章和文檔,但我相信這些僅用於Woocommerce插件,而不是Woo / Stripe擴展插件。 如果解決方案是鈎子或過濾器,那會是什么樣? 我可以修改核心文件或使用JS,但這根本不是真正可行的解決方案...

我要修改的文件位於:

插件/ woocommerce網關條紋/包含/類-WC-網關stripe.php

這是我要在該文件中修改的標記塊:

/**
 * 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
}

您是正確的,在這種情況下,由於缺少動作/過濾器掛鈎,您無法修改輸出。 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";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM