-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebhooks.php
More file actions
98 lines (86 loc) · 3.52 KB
/
Copy pathWebhooks.php
File metadata and controls
98 lines (86 loc) · 3.52 KB
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
<?php
use Evoliz\Client\Config;
use Evoliz\Client\Exception\ResourceException;
require_once 'Class/EvolizSaleOrder.php';
abstract class Webhooks
{
public static $config;
/**
* @param Config $config
* @return void
*/
public static function init(Config $config)
{
self::$config = $config;
add_filter('woocommerce_checkout_fields', __CLASS__ . '::addNewFieldsToCheckout');
add_action('woocommerce_checkout_create_order', __CLASS__ . '::fillOrderMetaData');
add_action('woocommerce_order_status_changed', __CLASS__ . '::manageSaleOrder', 10, 3);
}
/**
* @param array $fields
* @return array
*/
public static function addNewFieldsToCheckout(array $fields): array
{
if (get_option('wc_evz_enable_vat_number') === 'on') {
if (get_option('wc_evz_eu_vat_number') === null || get_option('wc_evz_eu_vat_number') === '') {
$fields['billing']['vat_number'] = [
'label' => __('EU VAT number', 'woocommerce'), // Add custom field label
'placeholder' => _x('EU VAT number', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('form-row-wide'),
];
}
}
return $fields;
}
/**
* @param object $order
* @return void
*/
public static function fillOrderMetaData(object $order) {
if (!empty( $_POST['vat_number'])) {
$order->update_meta_data('vat_number', esc_attr(htmlspecialchars($_POST['vat_number'])));
}
}
/**
* @param int $orderId Woocommerce order identifier
* @param string $previousStatus Order previous status name
* @param string $newStatus Order new status name
* @return void
* @throws ResourceException
*/
public static function manageSaleOrder(int $orderId, string $previousStatus, string $newStatus)
{
$wcOrder = wc_get_order($orderId);
try {
switch ($newStatus) {
case "on-hold" :
EvolizSaleOrder::findOrCreate(self::$config, $wcOrder);
break;
case "processing" :
if ($previousStatus !== 'on-hold') {
EvolizSaleOrder::findOrCreate(self::$config, $wcOrder);
}
if ($wcOrder->get_payment_method() !== 'cod' && get_option('wc_evz_enable_invoicing', 'on') === 'on') {
EvolizSaleOrder::invoiceAndPay(self::$config, $wcOrder);
}
break;
case "completed" :
if (!in_array($previousStatus, ['on-hold', 'processing'])) {
EvolizSaleOrder::findOrCreate(self::$config, $wcOrder);
}
if (get_option('wc_evz_enable_invoicing', 'on') === 'on') {
EvolizSaleOrder::invoiceAndPay(self::$config, $wcOrder);
}
}
if (in_array($newStatus, ['on-hold', 'processing', 'completed'])) {
EvolizSettings::updateWooCommerceAppOnEvoliz(self::$config);
}
} catch (Exception $exception) {
writeLog("[ Order : $orderId ] " . $exception->getMessage() . "\n\n", $exception->getCode(), EVOLIZ_LOG_ERROR);
}
}
}