forked from mcgregormedia/Purchase-Orders-for-WooCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurchase-orders-for-woocommerce.php
187 lines (111 loc) · 4.81 KB
/
purchase-orders-for-woocommerce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
Plugin Name: Purchase Orders for WooCommerce
Plugin URI: https://mcgregormedia.co.uk
Description: Adds a Purchase Order payment method to WooCommerce.
Author: McGregor Media Web Design
Author URI: https://mcgregormedia.co.uk
Version: 1.7.10
Text Domain: pofwc
WC requires at least: 3.0
WC tested up to: 4.4
License: GNU General Public License v2.0
License URI: http://www.gnu.org/licenses/gpl-2.0.html
This gateway forks the WooCommerce core "Cheque" payment gateway to create another offline payment method.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Came directly here? Vamoose. Go on, scram.
}
/**
* Loads translation files
*
* @since 1.2.0 Added function
*/
function pofwc_load_textdomain() {
load_plugin_textdomain( 'pofwc', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'pofwc_load_textdomain' );
/**
* Adds option on activation to check if newly activated. If true, runs WooCommerce check after register_activation_hook redirection
*
* @since 1.2.0 Added function
*/
function pofwc_activate(){
add_option( 'pofwc_activated', 'pofwc' );
}
register_activation_hook( __FILE__, 'pofwc_activate' );
/**
* Checks whether WooCommerce is active and deactivates plugin with admin notice if not
*
* @since 1.2.0 Added function
*/
function pofwc_load_plugin(){
if ( is_admin() && get_option( 'pofwc_activated' ) == 'pofwc' ) {
delete_option( 'pofwc_activated' ); // remove option we set on activation
if ( !class_exists( 'WooCommerce' ) ) { // check WooCommerce is active
add_action( 'admin_notices', 'pofwc_admin_notice' ); // if not display admin notice
deactivate_plugins( plugin_basename( __FILE__ ) ); // deactivate plugin
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
add_action( 'admin_init', 'pofwc_load_plugin' );
/**
* Display an error message if WooCommerce is not activated
*
* @return string The formatted HTML
*
* @since 1.2.0 Added function
*/
function pofwc_admin_notice (){
?>
<div class="notice notice-error"><p><?php _e( 'Purchase Orders for WooCommerce requires WooCommerce. Please install and activate WooCommerce.', 'pofwc' ) ?></p></div>
<?php
}
/**
* Adds the gateway to WC Available Gateways
*
* @param array $gateways all available WC gateways
* @return array $gateways all WC gateways + offline gateway
*
* @since 1.0.0
*/
function pofwc_add_to_gateways( $gateways ) {
$gateways[] = 'WC_Gateway_Purchase_Order';
return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'pofwc_add_to_gateways' );
/**
* Adds plugin page links
*
* @param array $links all plugin links
* @return array $links all plugin links + our custom links
*
* @since 1.0.0
*/
function pofwc_purchase_order_gateway_plugin_links( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=purchase_order_gateway' ) . '">' . __( 'Settings', 'pofwc' ) . '</a>'
);
return array_merge( $plugin_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pofwc_purchase_order_gateway_plugin_links' );
function pofwc_show_invoice_address( $order_id ){
echo '<h2>Purchase order information</h2>';
echo '<p>';
echo '<strong>Purchase order number:</strong> ' . get_post_meta( $order_id, '_purchase_order_number', true ) . '<br>';
echo '<strong>Invoice address:</strong> <br>';
echo ( get_post_meta( $order_id, '_purchase_order_company_name', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_company_name', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_address1', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_address1', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_address2', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_address2', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_address3', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_address3', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_town', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_town', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_county', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_county', true ) ) . '<br>' : '';
echo ( get_post_meta( $order_id, '_purchase_order_postcode', true ) ) ? esc_html( get_post_meta( $order_id, '_purchase_order_postcode', true ) ) . '<br>' : '';
echo '</p>';
}
add_action( 'woocommerce_view_order', 'pofwc_show_invoice_address', 20 );
// Require files
require dirname( __FILE__ ) . '/class-purchase-order-gateway.php';