Plugin Directory

Changeset 2215920


Ignore:
Timestamp:
12/20/2019 07:56:56 PM (6 years ago)
Author:
travisnorthcutt
Message:

Version 1.3.0

Location:
convertkit-for-woocommerce
Files:
15 added
3 edited

Legend:

Unmodified
Added
Removed
  • convertkit-for-woocommerce/trunk/components/integration/integration.php

    r2123418 r2215920  
    122122            if ( 'yes' === $this->send_purchases ){
    123123                add_action( 'woocommerce_payment_complete',        array( $this, 'send_payment' ), 99999, 1 );
     124                add_action( 'woocommerce_order_status_changed',    array( $this, 'handle_cod_or_check_order_completion' ), 99999, 4 );
    124125            }
    125126        }
     
    513514
    514515    /**
    515      * For each subscription (sequence, tag, form) attached to a product,
    516      * perform the relevant actions (subscribe & add order note)
    517      *
    518      * @param array $subscriptions
    519      * @param string $email
    520      * @param string $name
    521      * @param string $order_id
    522      */
    523     public function process_convertkit_subscriptions( $subscriptions, $email, $name, $order_id ) {
    524 
    525         foreach ( $subscriptions as $subscription_raw ) {
    526             list( $subscription['type'], $subscription['id'] ) = explode( ':', $subscription_raw );
    527 
    528             $subscription['function'] = "ckwc_convertkit_api_add_subscriber_to_{$subscription['type']}";
    529 
    530             $this->process_item_subscription( $subscription, $email, $name, $order_id );
    531         }
    532     }
    533 
    534     /**
    535      * For each subscription (sequence, tag, form) attached to a product,
    536      * perform the relevant actions (subscribe & add order note)
    537      *
    538      * @param array $subscription
    539      * @param string $email
    540      * @param string $name
    541      * @param string $order_id
    542      */
    543     public function process_item_subscription( $subscription, $email, $name, $order_id ) {
    544         if ( function_exists( $subscription['function'] ) ) {
    545             $response = call_user_func( $subscription['function'], $subscription['id'], $email, $name );
    546 
    547             if ( ! is_wp_error( $response ) ) {
    548                 $options = ckwc_get_subscription_options();
    549                 $items   = array();
    550                 foreach ( $options as $option ) {
    551                     if ( $subscription['type'] !== $option['key'] ) {
    552                         continue;
    553                     }
    554 
    555                     /**
    556                      * This ends up holding an array of the subscription items (tags, courses, or forms) our WP install knows about,
    557                      * which match the current subscription type, in `id => name` pairs
    558                      */
    559                     $items = $option['options'];
    560                 }
    561                 if ( $items ) {
    562                     // we then check if the item ID we sent is in this array, and if so add an order note
    563                     if ( isset( $items[ $subscription['id'] ] ) ) {
    564                         switch ( $subscription['type'] ) {
    565                             case 'tag':
    566                             case 'form':
    567                                 wc_create_order_note( $order_id,
    568                                                       sprintf( __( '[ConvertKit] Customer subscribed to the %s: %s',
    569                                                                    'woocommerce-convertkit' ), $subscription['type'],
    570                                                                $items[ $subscription['id'] ] ) );
    571                                 break;
    572 
    573                             // Sequences are called "courses" for legacy reasons, so they get a special case
    574                             case 'course':
    575                                 wc_create_order_note( $order_id,
    576                                                       sprintf( __( '[ConvertKit] Customer subscribed to the %s: %s',
    577                                                                    'woocommerce-convertkit' ), 'sequence',
    578                                                                $items[ $subscription['id'] ] ) );
    579                                 break;
    580                         }
    581                     }
    582                 }
    583             }
    584 
    585             if ( 'yes' === $this->get_option( 'debug' ) ) {
    586                 $this->debug_log( 'API call: ' . $subscription['type'] . "\nResponse: \n" . print_r( $response,
    587                                                                                                      true ) );
    588             }
    589         }
    590     }
    591 
    592     /**
    593      * @param bool|WC_Order|WC_Order_Refund $order
    594      *
    595      * @return string
    596      */
    597     public function email( $order ) {
    598         $email = version_compare( WC()->version, '3.0.0', '>=' ) ? $order->get_billing_email() : $order->billing_email;
    599 
    600         return apply_filters( 'convertkit_for_woocommerce_email', $email, $order );
    601     }
    602 
    603     /**
    604      * @param bool|WC_Order|WC_Order_Refund $order
    605      *
    606      * @return string
    607      */
    608     public function first_name( $order ) {
    609         $first_name = version_compare( WC()->version, '3.0.0',
    610                                        '>=' ) ? $order->get_billing_first_name() : $order->billing_first_name;
    611 
    612         return apply_filters( 'convertkit_for_woocommerce_first_name', $first_name, $order );
    613     }
    614 
    615     /**
    616      * @param bool|WC_Order|WC_Order_Refund $order
    617      *
    618      * @return string
    619      */
    620     public function last_name( $order ) {
    621         $last_name = version_compare( WC()->version, '3.0.0',
    622                                       '>=' ) ? $order->get_billing_last_name() : $order->billing_last_name;
    623 
    624         return apply_filters( 'convertkit_for_woocommerce_last_name', $last_name, $order );
    625     }
    626 
    627     /**
    628      * @param string $first_name
    629      * @param string $last_name
    630      *
    631      * @return string
    632      */
    633     public function name_format( $first_name, $last_name ) {
    634         switch ( $this->name_format ) {
    635             case 'first':
    636                 return $first_name;
    637                 break;
    638             case 'last':
    639                 return $last_name;
    640                 break;
    641             default:
    642                 return sprintf( "%s %s", $first_name, $last_name );
    643                 break;
    644 
    645         }
    646     }
    647 
    648     /**
    649      * Send order data to ConvertKit
    650      *
    651516     * @param int $order_id
    652      */
    653     public function send_payment( $order_id ){
     517     * @param string $status_old
     518     * @param string $status_new
     519     * @param WC_Order $order
     520     */
     521    public function handle_cod_or_check_order_completion( $order_id, $status_old, $status_new, $order ) {
    654522        $api_key_correct = ! empty( $this->api_key );
    655         $order = wc_get_order( $order_id );
    656         if ( $api_key_correct && ! is_wp_error( $order ) && $order ) {
     523        $correct_status = $status_new === 'completed';
     524        $payment_methods = array( 'cod', 'cheque', 'check' );
     525
     526        if ( $api_key_correct && $correct_status && in_array( $order->get_payment_method( null ), $payment_methods ) ) {
    657527
    658528            $products = array();
     
    660530            foreach( $order->get_items( ) as $item_key => $item ) {
    661531                $products[] = array(
    662                         'pid'        => $item->get_product()->get_id(),
    663                         'lid'        => $item_key,
    664                         'name'       => $item->get_name(),
    665                         'sku'        => $item->get_product()->get_sku(),
    666                         'unit_price' => $item->get_product()->get_price(),
    667                         'quantity'   => $item->get_quantity(),
    668                     );
     532                    'pid'        => $item->get_product()->get_id(),
     533                    'lid'        => $item_key,
     534                    'name'       => $item->get_name(),
     535                    'sku'        => $item->get_product()->get_sku(),
     536                    'unit_price' => $item->get_product()->get_price(),
     537                    'quantity'   => $item->get_quantity(),
     538                );
    669539            }
    670540
     
    702572                $this->debug_log( 'Send payment response WP Error: ' . $response->get_error_code() . ' ' . $response->get_error_message() );
    703573            } else {
     574                $order->add_order_note( 'Payment data sent to ConvertKit', 0, false );
     575                $this->debug_log( 'send payment response: ' . print_r( $response, true ) );
     576            }
     577
     578        }
     579    }
     580
     581    /**
     582     * For each subscription (sequence, tag, form) attached to a product,
     583     * perform the relevant actions (subscribe & add order note)
     584     *
     585     * @param array $subscriptions
     586     * @param string $email
     587     * @param string $name
     588     * @param string $order_id
     589     */
     590    public function process_convertkit_subscriptions( $subscriptions, $email, $name, $order_id ) {
     591
     592        foreach ( $subscriptions as $subscription_raw ) {
     593            list( $subscription['type'], $subscription['id'] ) = explode( ':', $subscription_raw );
     594
     595            $subscription['function'] = "ckwc_convertkit_api_add_subscriber_to_{$subscription['type']}";
     596
     597            $this->process_item_subscription( $subscription, $email, $name, $order_id );
     598        }
     599    }
     600
     601    /**
     602     * For each subscription (sequence, tag, form) attached to a product,
     603     * perform the relevant actions (subscribe & add order note)
     604     *
     605     * @param array $subscription
     606     * @param string $email
     607     * @param string $name
     608     * @param string $order_id
     609     */
     610    public function process_item_subscription( $subscription, $email, $name, $order_id ) {
     611        if ( function_exists( $subscription['function'] ) ) {
     612            $response = call_user_func( $subscription['function'], $subscription['id'], $email, $name );
     613
     614            if ( ! is_wp_error( $response ) ) {
     615                $options = ckwc_get_subscription_options();
     616                $items   = array();
     617                foreach ( $options as $option ) {
     618                    if ( $subscription['type'] !== $option['key'] ) {
     619                        continue;
     620                    }
     621
     622                    /**
     623                     * This ends up holding an array of the subscription items (tags, courses, or forms) our WP install knows about,
     624                     * which match the current subscription type, in `id => name` pairs
     625                     */
     626                    $items = $option['options'];
     627                }
     628                if ( $items ) {
     629                    // we then check if the item ID we sent is in this array, and if so add an order note
     630                    if ( isset( $items[ $subscription['id'] ] ) ) {
     631                        switch ( $subscription['type'] ) {
     632                            case 'tag':
     633                            case 'form':
     634                                wc_create_order_note( $order_id,
     635                                                      sprintf( __( '[ConvertKit] Customer subscribed to the %s: %s',
     636                                                                   'woocommerce-convertkit' ), $subscription['type'],
     637                                                               $items[ $subscription['id'] ] ) );
     638                                break;
     639
     640                            // Sequences are called "courses" for legacy reasons, so they get a special case
     641                            case 'course':
     642                                wc_create_order_note( $order_id,
     643                                                      sprintf( __( '[ConvertKit] Customer subscribed to the %s: %s',
     644                                                                   'woocommerce-convertkit' ), 'sequence',
     645                                                               $items[ $subscription['id'] ] ) );
     646                                break;
     647                        }
     648                    }
     649                }
     650            }
     651
     652            if ( 'yes' === $this->get_option( 'debug' ) ) {
     653                $this->debug_log( 'API call: ' . $subscription['type'] . "\nResponse: \n" . print_r( $response,
     654                                                                                                     true ) );
     655            }
     656        }
     657    }
     658
     659    /**
     660     * @param bool|WC_Order|WC_Order_Refund $order
     661     *
     662     * @return string
     663     */
     664    public function email( $order ) {
     665        $email = version_compare( WC()->version, '3.0.0', '>=' ) ? $order->get_billing_email() : $order->billing_email;
     666
     667        return apply_filters( 'convertkit_for_woocommerce_email', $email, $order );
     668    }
     669
     670    /**
     671     * @param bool|WC_Order|WC_Order_Refund $order
     672     *
     673     * @return string
     674     */
     675    public function first_name( $order ) {
     676        $first_name = version_compare( WC()->version, '3.0.0',
     677                                       '>=' ) ? $order->get_billing_first_name() : $order->billing_first_name;
     678
     679        return apply_filters( 'convertkit_for_woocommerce_first_name', $first_name, $order );
     680    }
     681
     682    /**
     683     * @param bool|WC_Order|WC_Order_Refund $order
     684     *
     685     * @return string
     686     */
     687    public function last_name( $order ) {
     688        $last_name = version_compare( WC()->version, '3.0.0',
     689                                      '>=' ) ? $order->get_billing_last_name() : $order->billing_last_name;
     690
     691        return apply_filters( 'convertkit_for_woocommerce_last_name', $last_name, $order );
     692    }
     693
     694    /**
     695     * @param string $first_name
     696     * @param string $last_name
     697     *
     698     * @return string
     699     */
     700    public function name_format( $first_name, $last_name ) {
     701        switch ( $this->name_format ) {
     702            case 'first':
     703                return $first_name;
     704                break;
     705            case 'last':
     706                return $last_name;
     707                break;
     708            default:
     709                return sprintf( "%s %s", $first_name, $last_name );
     710                break;
     711
     712        }
     713    }
     714
     715    /**
     716     * Send order data to ConvertKit
     717     *
     718     * @param int $order_id
     719     */
     720    public function send_payment( $order_id ){
     721        $api_key_correct = ! empty( $this->api_key );
     722        $order = wc_get_order( $order_id );
     723        if ( $api_key_correct && ! is_wp_error( $order ) && $order ) {
     724
     725            $products = array();
     726
     727            foreach( $order->get_items( ) as $item_key => $item ) {
     728                $products[] = array(
     729                        'pid'        => $item->get_product()->get_id(),
     730                        'lid'        => $item_key,
     731                        'name'       => $item->get_name(),
     732                        'sku'        => $item->get_product()->get_sku(),
     733                        'unit_price' => $item->get_product()->get_price(),
     734                        'quantity'   => $item->get_quantity(),
     735                    );
     736            }
     737
     738            $purchase_options = array(
     739                'api_secret' => $this->api_secret,
     740                'purchase' => array(
     741                    'transaction_id'   => $order->get_order_number(),
     742                    'email_address'    => $order->get_billing_email(),
     743                    'first_name'       => $order->get_billing_first_name(),
     744                    'currency'         => $order->get_currency(),
     745                    'transaction_time' => $order->get_date_created()->date( 'Y-m-d H:i:s' ),
     746                    'subtotal'         => (double) $order->get_subtotal(),
     747                    'tax'              => (double) $order->get_total_tax( 'edit' ),
     748                    'shipping'         => (double) $order->get_shipping_total( 'edit' ),
     749                    'discount'         => (double) $order->get_discount_total( 'edit' ),
     750                    'total'            => (double) $order->get_total( 'edit' ),
     751                    'status'           => 'paid',
     752                    'products'         => $products,
     753                    'integration'      => 'WooCommerce'
     754                )
     755            );
     756
     757            $query_args = is_null( $this->api_key ) ? array() : array(
     758                'api_key' => $this->api_key,
     759            );
     760            $body = $purchase_options;
     761            $args = array( 'method' => 'POST' );
     762
     763            $this->debug_log( 'send payment request: ' . print_r( $purchase_options, true ) );
     764
     765            $response = ckwc_convertkit_api_request( 'purchases', $query_args, $body, $args );
     766
     767            if ( is_wp_error( $response ) ){
     768                $order->add_order_note( 'Send payment to ConvertKit error: ' . $response->get_error_code() . ' ' . $response->get_error_message(), 0, 'ConvertKit plugin' );
     769                $this->debug_log( 'Send payment response WP Error: ' . $response->get_error_code() . ' ' . $response->get_error_message() );
     770            } else {
    704771                $order->add_order_note( 'Payment data sent to ConvertKit', 0, false );
    705772                $this->debug_log( 'send payment response: ' . print_r( $response, true ) );
  • convertkit-for-woocommerce/trunk/readme.txt

    r2123418 r2215920  
    44Tags: email, marketing, embed form, convertkit, capture, woocommerce
    55Requires at least: 3.6
    6 Tested up to: 5.2.2
    7 Stable tag: 1.2.0
     6Tested up to: 5.3.2
     7Stable tag: 1.3.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5050
    5151== Changelog ==
     52### 1.3.0  2019-12-20
     53* Add support for sending cash on delivery and check payment purchase info to ConvertKit
    5254
    5355### 1.2.0  2019-07-15
  • convertkit-for-woocommerce/trunk/woocommerce-convertkit.php

    r2123418 r2215920  
    44 * Plugin URI:  https://www.convertkit.com
    55 * Description: Integrates WooCommerce with ConvertKit allowing customers to be automatically sent to your ConvertKit account.
    6  * Version: 1.2.0
     6 * Version: 1.3.0
    77 * Author: ConvertKit
    88 * Author URI: https://www.convertkit.com
     
    2929
    3030if ( ! defined( 'CKWC_VERSION' ) ) {
    31     define( 'CKWC_VERSION', '1.2.0' );
     31    define( 'CKWC_VERSION', '1.3.0' );
    3232}
    3333
Note: See TracChangeset for help on using the changeset viewer.