My custom payment method is not showing besides the default payment methods of WooCommerce in the checkout page

58 views Asked by At

I created a custom payment method for WooCommerce and it was showing in the checkout page if it was enable from the WooCommerce payment settings. But after installing the WP and installing the latest WooCoommerce plugin it does not show anymore.

enter image description here

Earlier it was working fine enter image description here

This is the code I am using code.

// Registering the store credit custom payment gateway
add_filter( 'woocommerce_payment_gateways', 'add_store_credit_custom_payment_method' );
function add_store_credit_custom_payment_method( $methods )
{
    $methods[] = 'WC_Store_Credit';
    return $methods;
}

// Registering store credit class
add_filter( 'woocommerce_payment_gateways', 'store_credit_class' );
function store_credit_class( $gateways )
{
    $gateways[] = 'Store_Credit_Payment_Method';
    return $gateways;
}

// Creating the custom store credit payment class
add_action( 'plugins_loaded', 'store_credit_gateway' );
function store_credit_gateway()
{
    if ( class_exists( 'WC_Payment_Gateway' ) ) {
        class Store_Credit_Payment_Method extends WC_Payment_Gateway {

            public $order_status;
            public function __construct() {
                $this->id = 'store_credit'; // payment method id
                $this->icon = ''; // payment method icon
                $this->has_fields = true; // in case you need a custom credit card form
                $this->method_title = esc_html__( 'Store Credit', 'hex-coupon-for-woocommerce' ); // Title of the payment method
                $this->method_description = esc_html__( 'Take payments via store credit', 'coupon' ); // Description of the payment method

                // Payment method supports
                $this->supports = [
                    'products'
                ];

                $this->init_form_fields();

                $this->init_settings();
                $this->title = $this->get_option( 'title' );
                $this->description = $this->get_option( 'description' );
                $this->enabled = $this->get_option( 'enabled' );

                // Hook for saving the settings
                add_action( 'woocommerce_thankyou_hex_store_credit', [ $this, 'store_credit_update_order_status' ], 10, 1 );
            }

            // All plugin options
            public function init_form_fields()
            {
                $this->form_fields = [
                    'enabled' => [
                        'title'       => esc_html__( 'Enable/Disable', 'coupon' ),
                        'label'       => esc_html__( 'Enable Hex Store Credit', 'coupon' ),
                        'type'        => 'checkbox',
                        'description' => '',
                        'default'     => 'no'
                    ],
                    'title' => [
                        'title'       => esc_html__( 'Title', 'coupon' ),
                        'type'        => 'text',
                        'description' => esc_html__( 'The user will see this during the checkout process.', 'coupon' ),
                        'default'     => esc_html__( 'Store Credit', 'coupon' ),
                        'desc_tip'    => true,
                    ],
                    'description' => [
                        'title'       => esc_html__( 'Description', 'coupon' ),
                        'type'        => 'textarea',
                        'description' => esc_html__( 'This description will be shown during the checkout process.', 'coupon' ),
                        'default'     => esc_html__( 'Pay with your hex store credit.', 'coupon' ),
                    ],
                ];
            }

            // This form will be used during the checkout
            public function payment_fields()
            {
                if( $this->description ) {
                    // Displaying the description below the payment option
                    echo wpautop( wp_kses_post( $this->description ) );
                    $total_available_store_credit = StoreCreditPaymentHelpers::getInstance()->show_total_remaining_amount();
                }
                ?>
                <div class="form-row form-row-wide" style="border: 1px solid black; padding: 10px">
                    <label><?php echo esc_html__( 'Total credit available', 'coupon' ); ?></label>
                    <b><?php echo wc_price( $total_available_store_credit ); ?></b>
                </div>
                <div class="clear"></div>
                <?php
            }

            // Processing the payment method
            public function process_payment( $order_id )
            {
                // Getting the order details
                $order = wc_get_order( $order_id );

                // Reducing the product stock level
                wc_reduce_stock_levels( $order_id );

                // Making the cart empty
                WC()->cart->empty_cart();

                // Returning to the thank-you redirection
                return array(
                    'result' => 'success',
                    'redirect' => $this->get_return_url( $order )
                );
            }

            public function store_credit_update_order_status( $order_id )
            {
                // Updating the order status to 'processing' if paid with 'hex_store_credit'
                $order = wc_get_order( $order_id );
                if ( $order && $order->get_payment_method() === 'hex_store_credit' ) {
                    $order->update_status( 'processing' );
                }
            }
        }
    }
}

// Processing the custom payment
add_action( 'woocommerce_checkout_process', 'process_store_credit_payment' );
function process_store_credit_payment()
{
    if( $_POST['payment_method'] != 'store_credit' )
        return;
}

// Updating the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'custom_payment_update_order_meta' );
function custom_payment_update_order_meta( $order_id )
{
    if( $_POST['payment_method'] != 'store_credit' )
        return;
}

// Display the custom payment details
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order )
{
    if( $order->get_payment_method() != 'store_credit' )
        return;
}

// Filter hook to show custom confirmation text about store credit in checkout thankyou page
add_filter( 'woocommerce_thankyou_order_received_text', 'custom_store_credit_thankyou_message', 10, 2 );
function custom_store_credit_thankyou_message( $thankyou_message, $order )
{
    $site_url = get_site_url() . '/my-account/store-credit';

    $payment_method = $order->get_payment_method();

    if ( $payment_method === 'store_credit' ) {
        // Appending store credit thank you message
        $thankyou_message .= '<div class="store-credit">' . esc_html__( 'You have used store credit as payment method.', 'coupon' ) . '</div>';
        $thankyou_message .= '<a href="' . esc_url( $site_url ) . '">' . esc_html__( 'Check the store credit logs to see the details' ) . '</a>';
    }

    return $thankyou_message;
}
0

There are 0 answers