📌  相关文章
📜  woocommerce 自定义付款流程方法 (1)

📅  最后修改于: 2023-12-03 15:05:59.120000             🧑  作者: Mango

Woocommerce 自定义付款流程方法

简介

Woocommerce 是一个流行的 WordPress 电商插件,既然是插件,就意味着它可以被扩展和自定义。其中,最常见的自定义需求是更改付款流程。本文将介绍两个自定义付款流程的方法。

方法一:直接更改支付网关

Woocommerce 提供了多种支付网关,如 PayPal、银行转账等。通过更改默认的支付网关,可以实现自定义付款流程。

add_filter( 'woocommerce_payment_gateways', 'add_custom_payment_gateway' );
function add_custom_payment_gateway( $gateways ) {
    $gateways[] = 'WC_Custom_Payment_Gateway';
    return $gateways;
}
 
/**
 * Custom Payment Gateway.
 *
 * Provides a Custom Payment Gateway.
 *
 * @class       WC_Custom_Payment_Gateway
 * @extends     WC_Payment_Gateway
 */
add_action( 'plugins_loaded', 'init_custom_gateway_class' );
function init_custom_gateway_class() {
 
    class WC_Custom_Payment_Gateway extends WC_Payment_Gateway {
 
        /**
         * Constructor for the gateway.
         */
        public function __construct() {
 
            $this->id                 = 'custom_gateway';
            $this->icon               = apply_filters( 'woocommerce_custom_gateway_icon', '' );
            $this->has_fields         = false;
            $this->method_title       = __( 'Custom Gateway', 'wc-custom-gateway' );
            $this->method_description = __( 'Allows payments via Custom Gateway. See documentation: http://woothemes.com', 'wc-custom-gateway' );
 
            // Load the settings.
            $this->init_form_fields();
            $this->init_settings();
 
            // Define user set variables
            $this->title        = $this->get_option( 'title' );
            $this->description  = $this->get_option( 'description' );
            $this->instructions = $this->get_option( 'instructions', $this->description );
 
            // Actions
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            add_action( 'woocommerce_thankyou_custom_gateway', array( $this, 'thankyou_page' ) );
 
            // Customer Emails
            add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
        }
 
        /**
         * Initialize Gateway Settings Form Fields.
         */
        public function init_form_fields() {
 
            $this->form_fields = array(
 
                'enabled' => array(
                    'title'   => __( 'Enable/Disable', 'wc-custom-gateway' ),
                    'type'    => 'checkbox',
                    'label'   => __( 'Enable Custom Gateway', 'wc-custom-gateway' ),
                    'default' => 'yes'
                ),
 
                'title' => array(
                    'title'       => __( 'Title', 'wc-custom-gateway' ),
                    'type'        => 'text',
                    'description' => __( 'This controls the title which the user sees during checkout.', 'wc-custom-gateway' ),
                    'default'     => __( 'Custom Gateway', 'wc-custom-gateway' ),
                    'desc_tip'    => true,
                ),
 
                'description' => array(
                    'title'       => __( 'Description', 'wc-custom-gateway' ),
                    'type'        => 'textarea',
                    'description' => __( 'This controls the description which the user sees during checkout.', 'wc-custom-gateway' ),
                    'default'     => __( 'Custom Gateway Payment Gateway', 'wc-custom-gateway' ),
                ),
 
                'instructions' => array(
                    'title'       => __( 'Instructions', 'wc-custom-gateway' ),
                    'type'        => 'textarea',
                    'description' => __( 'Instructions that will be added to the thank you page and emails.', 'wc-custom-gateway' ),
                    'default'     => '',
                ),
            );
        }
 
        /**
         * Output for the order received page.
         */
        public function thankyou_page() {
            if ( $this->instructions ) {
                echo wpautop( wptexturize( $this->instructions ) );
            }
        }
 
        /**
         * Add content to the WC emails.
         *
         * @access public
         * @param mixed $order_id
         * @param bool $sent_to_admin
         * @param bool $plain_text
         */
        public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
            if ( $this->instructions && ! $sent_to_admin && 'custom_gateway' === $order->get_payment_method() && $order->has_status( 'processing' ) ) {
                echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
            }
        }
 
    }
 
}
方法二:添加修改付款流程钩子

通过添加钩子,可以在付款流程中加入自定义的步骤。

add_action( 'woocommerce_before_checkout_form', 'add_custom_checkout_field' );
 
function add_custom_checkout_field($checkout) {
 
    echo '<div id="my_custom_checkout_field"><h3>' . __('My Custom Field') . '</h3>';
 
    woocommerce_form_field( 'my_field_name', array(
        'type' => 'text',
        'class' => array(
            'my-field-class form-row-wide'
        ) ,
        'label' => __('Fill in this field') ,
        'placeholder' => __('Enter the value') ,
        ), $checkout->get_value( 'my_field_name' ));
 
    echo '</div>';
}
 
// Process the checkout
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
 
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if (!$_POST['my_field_name']) wc_add_notice( __('Please enter a value for the custom field.') , 'error' );
}
 
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
 
function my_custom_checkout_field_update_order_meta($order_id) {
    if ($_POST['my_field_name']) update_post_meta($order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
 
// Display field value on the order edit page
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1);
 
function my_custom_checkout_field_display_admin_order_meta($order) {
    echo '<p><strong>'.__('My Custom Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
}

使用以上两种方法,可以实现完全自定义的付款流程。