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
<?php
namespace SIW\WooCommerce\Export;
use SIW\Plato\Export_Application as Plato_Export_Application;
use SIW\Properties;
class Order {
protected $failed_count = 0;
protected $success_count = 0;
public static function init() {
$self = new self();
add_filter( 'woocommerce_order_actions', [ $self, 'add_order_action'] );
add_action( 'woocommerce_order_action_siw_export_to_plato', [ $self, 'export_order'] );
add_action( 'woocommerce_order_status_processing', [ $self, 'export_order'] );
}
public function add_order_action( array $actions ) {
global $theorder;
if ( $theorder->is_paid() ) {
$actions['siw_export_to_plato'] = __( 'Exporteer naar PLATO', 'siw' );
}
return $actions;
}
function export_order( $order ) {
if ( ! is_object( $order ) ) {
$order = new \WC_Order( $order );
}
$order_data = $this->get_order_data( $order );
foreach ( $order->get_items() as $item_id => $item_data ) {
$product = $order->get_product_from_item( $item_data );
$result = $this->export_application( $order_data, $product );
$order->add_order_note( $result['message'] );
}
if ( 0 != $this->failed_count ) {
$order->update_meta_data( '_exported_to_plato', 'failed' );
$order->save();
}
elseif ( 0 != $this->success_count ) {
$order->update_meta_data( '_exported_to_plato', 'success' );
$order->save();
}
}
protected function export_application( $order_data, $product ) {
$projectcode = $product->get_sku();
$order_data['choice1'] = $projectcode;
$export = new Plato_Export_Application;
$result = $export->run( $order_data );
if ( true == $result['success'] ) {
$this->success_count++;
}
else {
$this->failed_count++;
}
return $result;
}
protected function get_order_data( \WC_Order $order ) {
return [
'firstname' => $order->get_billing_first_name(),
'lastname' => $order->get_billing_last_name(),
'sex' => $order->get_meta( '_billing_gender' ),
'birthdate' => date( 'Y-m-d', strtotime( $order->get_meta( '_billing_dob' ) ) ),
'email' => $order->get_billing_country(),
'nationality' => $order->get_meta( '_billing_nationality' ),
'telephone' => $order->get_billing_phone(),
'address1' => sprintf( '%s %s', $order->get_billing_address_1(), $order->get_meta( '_billing_housenumber' ) ),
'zip' => $order->get_billing_postcode(),
'city' => $order->get_billing_city(),
'country' => 'NLD',
'occupation' => 'OTH',
'emergency_contact' => sprintf( '%s %s', $order->get_meta( 'emergencyContactName' ), $order->get_meta( 'emergencyContactPhone' ) ),
'language1' => $order->get_meta( 'language1' ),
'language2' => $order->get_meta( 'language2' ),
'language3' => $order->get_meta( 'language3' ),
'langlevel1' => $order->get_meta( 'language1Skill' ),
'langlevel2' => $order->get_meta( 'language2Skill' ),
'langlevel3' => $order->get_meta( 'language3Skill' ),
'special_needs' => $order->get_meta( 'healthIssues' ),
'experience' => $order->get_meta( 'volunteerExperience' ),
'motivation' => $order->get_meta( 'motivation' ),
'together_with' => $order->get_meta( 'togetherWith' ),
'req_sent_by' => Properties::NAME,
'req_sender_email' => Properties::EMAIL,
'date_filed' => date( 'Y-m-d' ),
];
}
}