Last active
July 16, 2022 23:18
-
-
Save NickMkrtchyan/5cff8cda2ed9c3e9096dcb42a6c2b9bf to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name: Custom Payment Gateway | |
* Plugin URI: | |
* Description: Woocommerce non_responsible custom payment gateway, for online ordering without online payment systems. | |
* Author: Nick Mkrtchyan | |
* Author URI: https://nickmkrtchyan.github.io/ | |
* Version: 1.1.0 | |
* Text Domain: woo_gateway | |
* Domain Path: /i18n/languages/ | |
* | |
* Copyright: (c) 2021 | |
* | |
* | |
* @author Nick Mkrtchyan | |
*/ | |
defined( 'ABSPATH' ) or exit; | |
// Make sure WooCommerce is active | |
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
return; | |
} | |
/** | |
* Add the gateway to WC Available Gateways | |
* | |
* @since 1.0.0 | |
* @param array $gateways all available WC gateways | |
* @return array $gateways all WC gateways + Custom Special gateway | |
*/ | |
function wc_add_special_to_gateways( $gateways ) { | |
$gateways[] = 'WC_Gateway_Special'; | |
return $gateways; | |
} | |
add_filter( 'woocommerce_payment_gateways', 'wc_add_special_to_gateways' ); | |
/** | |
* Adds plugin page links | |
* | |
* @since 1.0.0 | |
* @param array $links all plugin links | |
* @return array $links all plugin links + our custom links (i.e., "Settings") | |
*/ | |
function wc_special_gateway_plugin_links( $links ) { | |
$plugin_links = array( | |
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=special_payment' ) . '">' . __( 'Configure', 'wcpg-special' ) . '</a>' | |
); | |
return array_merge( $plugin_links, $links ); | |
} | |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_special_gateway_plugin_links' ); | |
/** | |
* Custom Payment Gateway | |
* | |
* Provides an Custom Payment Gateway; mainly for testing purposes. | |
* We load it later to ensure WC is loaded first since we're extending it. | |
* | |
* @class WC_Gateway_Special | |
* @extends WC_Payment_Gateway | |
* @version 1.0.0 | |
* @package WooCommerce/Classes/Payment | |
* @author Me | |
*/ | |
add_action( 'plugins_loaded', 'wc_special_gateway_init', 11 ); | |
function wc_special_gateway_init() { | |
class WC_Gateway_Special extends WC_Payment_Gateway { | |
public $domain; | |
/** | |
* Constructor for the gateway. | |
*/ | |
public function __construct() { | |
$this->id = 'special_payment'; | |
$this->domain = 'wcpg-special'; | |
$this->icon = apply_filters('woocommerce_payment_gateway_icon', ''); | |
$this->has_fields = false; | |
$this->method_title = __( 'Custom Payment', $this->domain ); | |
// Define "payment type" radio buttons options field | |
$this->options = array( | |
'type1' => __( 'Type 1', $this->domain ), | |
'type2' => __( 'Type 2', $this->domain ), | |
'type3' => __( 'Type 3', $this->domain ), | |
); | |
// 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->order_status = $this->get_option( 'order_status' ); | |
$this->status_text = $this->get_option( 'status_text' ); | |
// Actions | |
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); | |
add_action( 'woocommerce_checkout_create_order', array( $this, 'save_order_payment_type_meta_data' ), 10, 2 ); | |
add_filter( 'woocommerce_get_order_item_totals', array( $this, 'display_transaction_type_order_item_totals'), 10, 3 ); | |
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'display_payment_type_order_edit_pages'), 10, 1 ); | |
add_action( 'woocommerce_thankyou_' . $this->id, 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 = apply_filters( 'wc_special_payment_form_fields', array( | |
'enabled' => array( | |
'title' => __( 'Enable/Disable', $this->domain ), | |
'type' => 'checkbox', | |
'label' => __( 'Enable Special Payment', $this->domain ), | |
'default' => 'yes' | |
), | |
'title' => array( | |
'title' => __( 'Title', $this->domain ), | |
'type' => 'text', | |
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', $this->domain ), | |
'default' => __( 'Special Payment', $this->domain ), | |
'desc_tip' => true, | |
), | |
'description' => array( | |
'title' => __( 'Description', $this->domain ), | |
'type' => 'textarea', | |
'description' => __( 'Payment method description that the customer will see on your checkout.', $this->domain ), | |
'default' => __( 'Please remit payment to Store Name upon pickup or delivery.', $this->domain ), | |
'desc_tip' => true, | |
), | |
'instructions' => array( | |
'title' => __( 'Instructions', $this->domain ), | |
'type' => 'textarea', | |
'description' => __( 'Instructions that will be added to the thank you page and emails.', $this->domain ), | |
'default' => '', // Empty by default | |
'desc_tip' => true, | |
), | |
'order_status' => array( | |
'title' => __( 'Order Status', $this->domain ), | |
'type' => 'select', | |
'description' => __( 'Choose whether order status you wish after checkout.', $this->domain ), | |
'default' => 'wc-completed', | |
'desc_tip' => true, | |
'class' => 'wc-enhanced-select', | |
'options' => wc_get_order_statuses() | |
), | |
'status_text' => array( | |
'title' => __( 'Order Status Text', $this->domain ), | |
'type' => 'text', | |
'description' => __( 'Set the text for the selected order status.', $this->domain ), | |
'default' => __( 'Order is completed', $this->domain ), | |
'desc_tip' => true, | |
), | |
) ); | |
} | |
// /** | |
// * Output the "payment type" radio buttons fields in checkout. | |
// */ | |
// public function payment_fields(){ | |
// if ( $description = $this->get_description() ) { | |
// echo wpautop( wptexturize( $description ) ); | |
// } | |
// echo '<style>#transaction_type_field label.radio { display:inline-block; margin:0 .8em 0 .4em}</style>'; | |
// $option_keys = array_keys($this->options); | |
// woocommerce_form_field( 'transaction_type', array( | |
// 'type' => 'radio', | |
// 'class' => array('transaction_type form-row-wide'), | |
// 'label' => __('Payment Information', $this->domain), | |
// 'options' => $this->options, | |
// ), reset( $option_keys ) ); | |
// } | |
/** | |
* Save the chosen payment type as order meta data. | |
* | |
* @param object $order | |
* @param array $data | |
*/ | |
public function save_order_payment_type_meta_data( $order, $data ) { | |
if ( $data['payment_method'] === $this->id && isset($_POST['transaction_type']) ) | |
$order->update_meta_data('_transaction_type', esc_attr($_POST['transaction_type']) ); | |
} | |
/** | |
* Output for the order received page. | |
* | |
* @param int $order_id | |
*/ | |
public function thankyou_page( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( $this->instructions ) { | |
echo wpautop( wptexturize( $this->instructions ) ); | |
} | |
} | |
/** | |
* Display the chosen payment type on the order edit pages (backend) | |
* | |
* @param object $order | |
*/ | |
public function display_payment_type_order_edit_pages( $order ){ | |
if( $this->id === $order->get_payment_method() && $order->get_meta('_transaction_type') ) { | |
$options = $this->options; | |
echo '<p><strong>'.__('Transaction type').':</strong> ' . $options[$order->get_meta('_transaction_type')] . '</p>'; | |
} | |
} | |
/** | |
* Display the chosen payment type on order totals table | |
* | |
* @param array $total_rows | |
* @param WC_Order $order | |
* @param bool $tax_display | |
* @return array | |
*/ | |
public function display_transaction_type_order_item_totals( $total_rows, $order, $tax_display ){ | |
if( is_a( $order, 'WC_Order' ) && $order->get_meta('_transaction_type') ) { | |
$new_rows = []; // Initializing | |
$options = $this->options; | |
// Loop through order total lines | |
foreach( $total_rows as $total_key => $total_values ) { | |
$new_rows[$total_key] = $total_values; | |
if( $total_key === 'payment_method' ) { | |
$new_rows['payment_type'] = [ | |
'label' => __("Transaction type", $this->domain) . ':', | |
'value' => $options[$order->get_meta('_transaction_type')], | |
]; | |
} | |
} | |
$total_rows = $new_rows; | |
} | |
return $total_rows; | |
} | |
/** | |
* Add content to the WC emails. | |
* | |
* @access public | |
* @param WC_Order $order | |
* @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 && $this->id === $order->get_payment_method() | |
&& $order->has_status( $this->order_status ) ) { | |
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL; | |
} | |
} | |
/** | |
* Process the payment and return the result | |
* | |
* @param int $order_id | |
* @return array | |
*/ | |
public function process_payment( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
// Mark as on-hold (we're awaiting the payment) | |
$order->update_status( $this->order_status, $this->status_text ); | |
// Reduce stock levels | |
wc_reduce_stock_levels( $order->get_id() ); | |
// Remove cart | |
WC()->cart->empty_cart(); | |
// Return thankyou redirect | |
return array( | |
'result' => 'success', | |
'redirect' => $this->get_return_url( $order ) | |
); | |
} | |
} | |
} | |
// Минимальная Сумма Заказа | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
/* Check if WooCommerce is active */ | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
/* Settings */ | |
add_filter( 'woocommerce_general_settings','hs_woo_minimum_order_settings', 10, 2 ); | |
function hs_woo_minimum_order_settings( $settings ) { | |
$settings[] = array( | |
'title' => __( 'Минимальная сумма заказа', 'wc_minimum_order_amount' ), | |
'type' => 'title', | |
'desc' => 'Установите минимальную сумму заказа и настройте уведомление. Если минимальная сумма заказа не будет соблюдена, клиент не сможет перейти к оформлению заказа.', | |
'id' => 'wc_minimum_order_settings', | |
); | |
// Minimum order amount | |
$settings[] = array( | |
'title' => __( 'Minimum order amount', 'woocommerce' ), | |
'desc' => __( 'Оставьте это поле пустым, в противном случае установите минимальную сумму заказа. ', 'wc_minimum_order_amount' ), | |
'id' => 'wc_minimum_order_amount_value', | |
'default' => '', | |
'type' => 'number', | |
'desc_tip' => true, | |
'css' => 'width:70px;', | |
); | |
// Cart message | |
$settings[] = array( | |
'title' => __( 'Сообщение в корзине', 'woocommerce' ), | |
'desc' => __( 'Показать это сообщение, если сумма текущего заказа меньше определенного минимума - например, «50».', 'wc_minimum_order_amount' ), | |
'id' => 'wc_minimum_order_cart_notification', | |
'default' => 'Your current order total is %s — your order must be at least %s. Please adjust before heading to checkout.', | |
'type' => 'text', | |
'desc_tip' => true, | |
'css' => 'width:500px;', | |
); | |
// Checkout message | |
/* No longer needed because we don't see the checkout message | |
$settings[] = array( | |
'title' => __( 'Checkout message', 'woocommerce' ), | |
'desc' => __( 'Show this message if the current order total is less than the defined minimum', 'wc_minimum_order_amount' ), | |
'id' => 'wc_minimum_order_checkout_notification', | |
'default' => 'Your current order total is %s — your order must be at least %s.', | |
'type' => 'text', | |
'desc_tip' => true, | |
'css' => 'width:500px;', | |
); | |
*/ | |
$settings[] = array( 'type' => 'sectionend', 'id' => 'wc_minimum_order_settings' ); | |
return $settings; | |
} | |
/* Notices and checks */ | |
add_action( 'woocommerce_before_cart', 'hs_wc_minimum_order_amount' ); | |
add_action( 'woocommerce_review_order_before_payment', 'hs_wc_minimum_order_amount', 11 ); | |
function hs_wc_minimum_order_amount() { | |
// Get the minimum value from settings | |
$minimum = get_option( 'wc_minimum_order_amount_value' ); | |
// check if the minimum value has even been set | |
if ($minimum) { | |
if ( WC()->cart->total < $minimum ) { | |
if( is_cart() ) { | |
wc_print_notice( | |
sprintf( get_option( 'wc_minimum_order_cart_notification' ), | |
wc_price( WC()->cart->total ), | |
wc_price( $minimum ) | |
), 'error' | |
); | |
// wp_redirect(WC()->cart->get_cart_url()); | |
} | |
else { | |
/* | |
wc_add_notice( | |
sprintf( get_option( 'wc_minimum_order_checkout_notification' ), | |
wc_price( WC()->cart->total ), | |
wc_price( $minimum ) | |
), 'error' | |
); */ | |
/* don't go to checkout, stay on the cart until resolved */ | |
wp_redirect(WC()->cart->get_cart_url()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment