Last active
February 23, 2018 14:46
-
-
Save igorbenic/7f3cd5338d6707a8789a27ef76f141b5 to your computer and use it in GitHub Desktop.
Additional WooCommerce Settings Page for a Payment Gateway | https://www.ibenic.com/additional-woocommerce-settings-page-for-a-payment-gateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class WC_Custom_PayPal extends WC_Gateway_Paypal { | |
| /** | |
| * Setting a screen button in fields | |
| * @return void | |
| */ | |
| public function init_form_fields() { | |
| parent::init_form_fields(); | |
| $this->form_fields = array_merge( $this->form_fields, array( | |
| 'screen_button' => array( | |
| 'id' => 'screen_button', | |
| 'type' => 'screen_button', | |
| 'title' => __( 'Other Settings', 'custom_paypal' ), | |
| ) | |
| )); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'plugins_loaded', function(){ | |
| if( class_exists( 'WC_Gateway_Paypal' ) ) { | |
| class WC_Custom_PayPal extends WC_Gateway_Paypal { | |
| // Other Code will go here. | |
| } | |
| add_action( 'woocommerce_payment_gateways', 'wc_change_paypal' ); | |
| /** | |
| * Removing the existing PayPal Gateway and adding our own. | |
| * | |
| * @param array $gateways All Registered Gateways | |
| * @return array | |
| */ | |
| function wc_change_paypal( $gateways ) { | |
| for( $i = 0; $i < count( $gateways ); $i++ ) { | |
| if( 'WC_Gateway_Paypal' === $gateways[ $i ] ) { | |
| unset( $gateways[ $i ] ); | |
| } | |
| } | |
| $gateways[] = 'WC_Custom_PayPal'; | |
| return $gateways; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //... Goes in public function admin_options() { | |
| if( $orders ) { | |
| ?> | |
| <table class="form-table"> | |
| <thead> | |
| <tr> | |
| <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : | |
| if( 'order-actions' === $column_id ) { | |
| continue; | |
| } | |
| ?> | |
| <td><strong><?php echo esc_html( $column_name ); ?></strong></td> | |
| <?php endforeach; ?> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <?php foreach ( $orders as $customer_order ) : | |
| $order = wc_get_order( $customer_order ); | |
| $item_count = $order->get_item_count(); | |
| ?> | |
| <tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order"> | |
| <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?> | |
| <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>"> | |
| <?php if ( 'order-number' === $column_id ) : ?> | |
| <a href="<?php echo esc_url( $order->get_view_order_url() ); ?>"> | |
| <?php echo _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number(); ?> | |
| </a> | |
| <?php elseif ( 'order-date' === $column_id ) : ?> | |
| <time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time> | |
| <?php elseif ( 'order-status' === $column_id ) : ?> | |
| <?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?> | |
| <?php elseif ( 'order-total' === $column_id ) : ?> | |
| <?php | |
| /* translators: 1: formatted order total 2: total order items */ | |
| printf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); | |
| ?> | |
| <?php endif; ?> | |
| </td> | |
| <?php endforeach; ?> | |
| </tr> | |
| <?php endforeach; ?> | |
| </tbody> | |
| </table> | |
| <?php } else { | |
| echo '<p>' . __( 'No orders done yet with this gateway', 'custom_paypal' ) . '</p>'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class WC_Custom_PayPal extends WC_Gateway_Paypal { | |
| /** | |
| * Form Fields for Other Screen | |
| */ | |
| public function other_form_fields() { | |
| return array( | |
| 'convert' => array( | |
| 'type' => 'checkbox', | |
| 'id' => 'convert', | |
| 'title' => __( 'Convert?', 'custom_paypal' ), | |
| 'description' => __( 'Convert other currencies?', 'custom_paypal' ), | |
| ), | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class WC_Custom_PayPal extends WC_Gateway_Paypal { | |
| /** | |
| * Redefining how options are saved for this gateway. | |
| * If we are on a second screen, we will save the other fields. | |
| * If we are not on the second screen, save the original fields. | |
| */ | |
| public function process_admin_options() { | |
| if( isset( $_GET['screen'] ) && '' !== $_GET['screen'] ) { | |
| WC_Admin_Settings::save_fields( $this->other_form_fields() ); | |
| } else { | |
| parent::process_admin_options(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class WC_Custom_PayPal extends WC_Gateway_Paypal { | |
| /** | |
| * Redefining the display of options. | |
| * If we are on a second screen, we will show the other fields. | |
| * If we are not on the second screen, show the original fields. | |
| */ | |
| public function admin_options() { | |
| if( ! isset( $_GET['screen'] ) || '' === $_GET['screen'] ) { | |
| parent::admin_options(); | |
| } else { | |
| if( 'orders' === $_GET['screen'] ) { | |
| echo '<h2><a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=paypal' ) . '">' . $this->method_title . '</a> > ' . __( 'Orders done with PayPal', 'custom_paypal' ) . '</h2>'; | |
| $hide_save_button = true; // Remove the submit button. | |
| $orders = wc_get_orders(array( | |
| 'limit' => '-1', | |
| 'payment_method' => $this->id, | |
| )); | |
| if( $orders ) { | |
| // Code for Orders will go here | |
| } | |
| } else { | |
| echo '<h2><a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=paypal' ) . '">' . $this->method_title . '</a> > ' . __( 'Other Settings', 'custom_paypal' ) . '</h2>'; | |
| echo '<table class="form-table">'; | |
| WC_Admin_Settings::output_fields( $this->other_form_fields() ); | |
| echo '</table>'; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment