class-aw-subscriptions-integration.php 5.21 KB
<?php
/**
 *
 *
 * @package  WC_Active Woo Integration
 * @category Integration
 * @author   Jason Kadlec http://crazyeffective.com
 */


if ( ! class_exists( 'AW_WC_Subscription_Integration' ) ) :

class AW_WC_Subscription_Integration{

    /**
    * Set up the class, including it's hooks & filters, when the file is loaded.
    *
    **/
    public static function init() {

        $subsSettings = WC_Admin_Settings::get_option( 'woocommerce_active-woo_settings' );
        if (isset($subsSettings['wc_subs_sync'])) {
		$subsSettings = $subsSettings['wc_subs_sync'];
        }
	$subscription_version = (int) WC_Subscriptions::$version;

        // exit if Woo Subscription Sync is disabled
        if ( ! 'yes' == $subsSettings ) {
            return;
        }

        /**
        * Show notice if Subscriptions version is lower than 2
        */
        if ( $subscription_version < 2 ) {
            add_action('admin_notices', __CLASS__ . '::version_notice' );
            return;
        }

        // add skuCANC tag
        add_action( 'woocommerce_subscription_status_cancelled', __CLASS__ . '::add_tag_skuCANC' );

        // add skuCOMPLETE tag
        add_action( 'woocommerce_subscription_status_expired', __CLASS__ . '::add_tag_skuCOMPLETE' );

        // add skuSUSP tag
        add_action( 'woocommerce_subscription_status_on-hold', __CLASS__ . '::add_tag_skuSUSP' );

        // add skuTrialEnd tag
        add_action( 'woocommerce_scheduled_subscription_trial_end', __CLASS__ . '::add_tag_skuTrialEnd' );

        // add skuPAYF tag
        add_action( 'woocommerce_subscription_payment_failed', __CLASS__ . '::add_tag_skuPAYF' );

        // add skuCANC tag
        add_action( 'subscriptions_cancelled_for_order', __CLASS__ . '::add_tag_skuCANC' );

        // // add skuCANC tag
        add_action( 'subscriptions_expired_for_order', __CLASS__ . '::add_tag_skuCANC' );

    }

    public static function version_notice(){

        $notice  = '';
        $notice .= '<div class="notice notice-error is-dismissible"> ';
            $notice .= '<p><strong>You must update Subscriptions to use the ActiveWoo Subscription sync feature.</strong></p>';
            $notice .= '<button type="button" class="notice-dismiss">';
                $notice .= '<span class="screen-reader-text">Dismiss this notice.</span>';
            $notice .= '</button>';
        $notice .= '</div>';

        echo $notice;
    }

    /**
    * Saves required tags to ActiveCampaign Contact
    * @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as cancelled.
    * @param String tag suffix to add before SKU of Products
    */
    public static function save_tags( $order, $suffix ) {

        // Get WC_Order if $order param isn't a object of WC_Order
        if ( ! $order instanceof WC_Order ) {
            $order = new WC_Order( $order );
        }

        $items = $order->get_items();
        $email = $order->billing_email;
        $tags  = array();

        foreach ($items as $item) {  //loop through the items in the purchase
            $product = new WC_Product($item['product_id']);
            $tags[] = $product->get_sku() . $suffix ;
        }

        $awc      = new WC_Integration_Active_Woo();
        $ac       = new aw_ActiveCampaign( $awc->api_url, $awc->api_key );
        $data     = array( 'email' => $email, 'tags' => $tags );
        $response = $ac->api("contact/tag/add", $data);

        if ( $response->success ) {
            // save the tags to post meta
            update_post_meta ( $order->id, 'aw_subs_tags', $tags );
        }

    }

      /**
    * To add tag with CANC suffix
    * @param WC_Order|WC_subscription $subscription depends based on triggered by which Action
    */
    public static function add_tag_skuCANC( $subscription ) {

        // check if $subscription a WC_Order object
        // may triggered by subscriptions_expired_for_order and
        // subscriptions_expired_for_order action

        if ( $subscription instanceof WC_Order ) {
            // WC_Order
            self::save_tags( $subscription, "CANC" );
        }elseif( $subscription instanceof WC_Subscription ){
            // WC_Subscription
            self::save_tags( $subscription->order->id, "CANC" );
        }else{
            // order id
            self::save_tags( $subscription, "CANC" );
        }

    }

    /**
    * To add tag with COMPLETE suffix
    * @param WC_subscription $subscription
    */
    public static function add_tag_skuCOMPLETE( $subscription ) {

        self::save_tags( $subscription->order->id, "COMPLETE" );

    }

    /**
    * To add tag with SUSP suffix
    * @param WC_subscription $subscription
    */
    public static function add_tag_skuSUSP( $subscription ) {

        self::save_tags( $subscription->order->id, "SUSP" );

    }

    /**
    * To add tag with TrialEnd suffix
    * @param WC_subscription $subscription
    */
    public static function add_tag_skuTrialEnd( $subscription ) {

        self::save_tags( $subscription->order->id, "TrialEnd" );

    }

    /**
    * To add tag with PAYF suffix
    * @param WC_subscription $subscription
    */
    public static function add_tag_skuPAYF( $subscription ) {

        self::save_tags( $subscription->order->id, "PAYF" );

    }

} // endif

endif;