public static function order_status_changed($id, $status = '', $new_status = '')
{
$rules = get_option('woorule_rules', array());
foreach ($rules as $k => $rule) {
$enabled = get_option($rule['enabled']['id']) === 'yes' ? true : false;
if ($enabled) {
$which_event = get_option($rule['occurs']['id']);
$is_opt_in_rule = false;
$want_in = true;
if (isset($rule['show_opt_in'])) {
$is_opt_in_rule = get_option($rule['show_opt_in']['id']) === 'yes' ? true : false;
}
if ($is_opt_in_rule) {
$want_in = get_post_meta($id, 'woorule_opt_in_' . $k, false);
if (!empty($want_in)) {
$want_in = $want_in[0] === 'yes' ? true : false;
}
}
if ($want_in && $new_status === $which_event) {
$integration = new WC_Integration_RuleMailer();
$order = new WC_Order($id);
$user = $order->get_user();
$currency = $order->get_order_currency();
$order_subtotal = $order->order_total - ($order->order_shipping_tax + $order->order_shipping) - $order->cart_discount;
$items = $order->get_items();
$brands = array();
$categories = array();
$tags = array();
foreach ($items as $item) {
$p = new WC_Product_Simple($item['product_id']);
$brands[] = $p->get_attribute('brand');
// this is bullshit
$cat = strip_tags($p->get_categories(''));
$tag = strip_tags($p->get_tags(''));
if (!empty($cat)) {
$categories[] = $cat;
}
if (!empty($tag)) {
$tags[] = $tag;
}
}
$subscription = array('apikey' => $integration->api_key, 'update_on_duplicate' => get_option($rule['update_on_duplicate']['id']) === 'yes' ? true : false, 'auto_create_tags' => get_option($rule['auto_create_tags']['id']) === 'yes' ? true : false, 'auto_create_fields' => get_option($rule['auto_create_fields']['id']) === 'yes' ? true : false, 'tags' => explode(',', get_option($rule['tags']['id'])), 'subscribers' => array('email' => $order->billing_email, 'phone_number' => $order->billing_phone, 'fields' => array(array('key' => 'Order.Number', 'value' => $order->get_order_number()), array('key' => 'Order.Date', 'value' => $order->order_date), array('key' => 'Order.FirstName', 'value' => $order->billing_first_name), array('key' => 'Order.LastName', 'value' => $order->billing_last_name), array('key' => 'Order.Street1', 'value' => $order->billing_address_1), array('key' => 'Order.Street2', 'value' => $order->billing_address_2), array('key' => 'Order.City', 'value' => $order->billing_city), array('key' => 'Order.Country', 'value' => $order->billing_country), array('key' => 'Order.State', 'value' => $order->billing_state), array('key' => 'Order.Subtotal', 'value' => $order_subtotal), array('key' => 'Order.Discount', 'value' => $order->cart_discount), array('key' => 'Order.Shipping', 'value' => $order->order_shipping + $order->order_shipping_tax), array('key' => 'Order.Total', 'value' => $order->order_total), array('key' => 'Order.Vat', 'value' => $order->order_tax))));
if (!empty($categories)) {
$subscription['subscribers']['fields'][] = array('key' => 'Order.Categories', 'value' => $categories, 'type' => 'multiple');
}
if (!empty($tags)) {
$subscription['subscribers']['fields'][] = array('key' => 'Order.Tags', 'value' => array($tags), 'type' => 'multiple');
}
if (!empty($brands)) {
$subscription['subscribers']['fields'][] = array('key' => 'Order.Brands', 'value' => $brands, 'type' => 'multiple');
}
$api = WP_RuleMailer_API::get_instance();
$api::subscribe($integration->api_url, $subscription);
}
}
}
}