コード例 #1
0
    /**
     * Load existing subscription transaction
     * 
     * @access public
     * @return void
     */
    public function populate()
    {
        if (!$this->id) {
            return false;
        }

        // Get action
        $actions = self::get_actions();
        $post_terms = wp_get_post_terms($this->id, 'sub_transaction_action');
        $this->action = $post_terms[0]->slug;
        $this->action_title = $actions[$this->action]['title'];

        // Get status
        $results = self::get_results();
        $post_terms = wp_get_post_terms($this->id, 'sub_transaction_result');
        $this->result = $post_terms[0]->slug;
        $this->result_title = $results[$this->result]['title'];

        // Get other fields
        $post_meta = Subscriptio::unwrap_post_meta(get_post_meta($this->id));

        // Load other properties from meta
        foreach (array('subscription_id', 'order_id', 'product_id', 'variation_id', 'time', 'iso_date', 'note') as $property) {
            $this->$property = isset($post_meta[$property]) ? maybe_unserialize($post_meta[$property]) : null;
        }
    }
コード例 #2
0
    /**
     * Check if payment on specific order has already been applied to this subscription
     * 
     * @access public
     * @param int $order_id
     * @return bool
     */
    public function paid_by_order($order_id)
    {
        $paid_by_orders = Subscriptio::unwrap_post_meta(get_post_meta($this->id, 'paid_by_orders'));
        $paid_by_orders = is_array($paid_by_orders) ? $paid_by_orders : array($paid_by_orders);

        if (empty($paid_by_orders) || !in_array((string) $order_id, $paid_by_orders)) {
            return false;
        }

        return true;
    }
コード例 #3
0
    /**
     * Create renewal order
     * Based on the WooCommerce procedure found in class-wc-checkout.php
     * 
     * @access public
     * @param object $subscription
     * @return int
     */
    public static function create_renewal_order($subscription)
    {
        // Get instance of main plugin class to access settings
        $subscriptio = Subscriptio::get_instance();

        // Since WooCommerce 2.2 order statuses are stored as post statuses
        $post_status = Subscriptio::wc_version_gte('2.2') ? 'wc-pending' : 'publish';

        // Prepare post properties
        $order_data = array(
            'post_type' 	=> 'shop_order',
            'post_title' 	=> sprintf(__('Order – %s', 'subscriptio'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'subscriptio'))),
            'post_status' 	=> $post_status,
            'ping_status'	=> 'closed',
            'post_excerpt' 	=> $subscription->renewal_customer_note,
            'post_author' 	=> 1,
            'post_password'	=> uniqid('order_'),
        );

        // Insert post into database
        $order_id = wp_insert_post($order_data, true);

        // Successfully inserted order post?
        if (is_wp_error($order_id)) {
            throw new Exception(__('Unable to create renewal order - failed inserting post.', 'subscriptio'));
        }

        // Load user meta
        $user_meta = Subscriptio::unwrap_post_meta(get_user_meta($subscription->user_id));

        // Insert billing and shipping details
        $billing_shipping_fields = array(
            'billing'  => array(
                '_first_name',
                '_last_name',
                '_company',
                '_address_1',
                '_address_2',
                '_city',
                '_state',
                '_postcode',
                '_country',
                '_email',
                '_phone',
            ),
        );

        // Check if subscription needs shipping
        if ($subscription->needs_shipping()) {
            $billing_shipping_fields['shipping'] = array(
                '_first_name',
                '_last_name',
                '_company',
                '_address_1',
                '_address_2',
                '_city',
                '_state',
                '_postcode',
                '_country',
            );
        }

        // Iterate over billing/shipping fields and save them
        foreach ($billing_shipping_fields as $type => $fields) {
            foreach ($fields as $field) {

                // Billing fields
                if ($type == 'billing' && isset($user_meta[$type . $field])) {
                    $field_value = $user_meta[$type . $field];
                }

                // Shipping fields
                else if ($type == 'shipping' && isset($subscription->shipping_address['_' . $type . $field])) {
                    $field_value = $subscription->shipping_address['_' . $type . $field];
                }

                // In case some field does not exist
                else {
                    $field_value = '';
                }

                // Save field to post meta
                update_post_meta($order_id, '_' . $type . $field, $field_value);
            }
        }

        // Add other meta fields
        $other_meta_fields = array(
            '_order_shipping'       => $subscription->renewal_order_shipping,
            '_order_shipping_tax'   => $subscription->renewal_order_shipping_tax,
            '_cart_discount'        => $subscription->renewal_cart_discount,
            '_order_discount'       => $subscription->renewal_order_discount,
            '_order_tax'            => $subscription->renewal_order_tax,
            '_order_total'          => $subscription->renewal_order_total,
            '_customer_user'        => $subscription->user_id,
            '_order_currency'       => $subscription->renewal_order_currency,
            '_order_key'            => 'wc_' . apply_filters('woocommerce_generate_order_key', uniqid('order_')),
            '_prices_include_tax'   => $subscription->renewal_prices_include_tax,
            '_customer_ip_address'  => $subscription->renewal_customer_ip_address,
            '_customer_user_agent'  => $subscription->renewal_customer_user_agent,
            '_payment_method'       => '',  // Not yet paid
            '_payment_method_title' => '',  // Not yet paid
            '_subscriptio_renewal'  => 'yes',
        );

        foreach ($other_meta_fields as $field_key => $field_value) {
            update_post_meta($order_id, $field_key, $field_value);
        }

        // Check if subscription product is variable
        $product_id_to_use = !empty($subscription->variation_id) ? $subscription->variation_id : $subscription->product_id;

        // Check if product still exists
        if (Subscriptio::product_is_active($product_id_to_use)) {

            // Load product object
            $product = new WC_Product($product_id_to_use);

            // Get product name
            $product_title = $product->get_title();

            // Update product name on subscription if it was changed
            if ($product_title != $subscription->product_name) {
                $subscription->update_subscription_details(array(
                    'product_name'  => $product_title,
                ));
            }
        }

        // If not - use saved product "snapshot" from previous order
        else {
            $product_title = $subscription->product_name;
        }

        // Add line item (product) to order
        $item_id = wc_add_order_item($order_id, array(
            'order_item_name'   => $product_title,
            'order_item_type'   => 'line_item',
        ));

        if (!$item_id) {
            throw new Exception(__('Unable to add product to renewal order.', 'subscriptio'));
        }

        // Add line item meta
        $item_meta = array(
            '_qty'                  => !empty($subscription->quantity) ? $subscription->quantity : 1,
            '_tax_class'            => $subscription->renewal_tax_class,
            '_product_id'           => $subscription->product_id,
            '_variation_id'         => !empty($subscription->variation_id) ? $subscription->variation_id : '',
            '_line_subtotal'        => wc_format_decimal($subscription->renewal_line_subtotal),
            '_line_subtotal_tax'    => wc_format_decimal($subscription->renewal_line_subtotal_tax),
            '_line_total'           => wc_format_decimal($subscription->renewal_line_total),
            '_line_tax'             => wc_format_decimal($subscription->renewal_line_tax),
        );

        foreach ($item_meta as $item_meta_key => $item_meta_value) {
            wc_add_order_item_meta($item_id, $item_meta_key, $item_meta_value);
        }

        // Save shipping info (if any)
        if (!empty($subscription->shipping)) {
            $shipping_item_id = wc_add_order_item($order_id, array(
                'order_item_name'   => $subscription->shipping['name'],
                'order_item_type'   => 'shipping',
            ));

            wc_add_order_item_meta($shipping_item_id, 'method_id', $subscription->shipping['method_id']);
            wc_add_order_item_meta($shipping_item_id, 'cost', wc_format_decimal($subscription->shipping['cost']));
        }

        // Save taxes (if any)
        foreach ($subscription->taxes as $tax) {
            $tax_item_id = wc_add_order_item($order_id, array(
                'order_item_name'   => $tax['name'],
                'order_item_type'   => 'tax',
            ));

            wc_add_order_item_meta($tax_item_id, 'rate_id', $tax['rate_id']);
            wc_add_order_item_meta($tax_item_id, 'label', $tax['label']);
            wc_add_order_item_meta($tax_item_id, 'compound', $tax['compound']);
            wc_add_order_item_meta($tax_item_id, 'tax_amount', wc_format_decimal($tax['tax_amount'], 4));
            wc_add_order_item_meta($tax_item_id, 'shipping_tax_amount', wc_format_decimal($tax['shipping_tax_amount'], 4));
        }

        // Schedule payment due reminders
        if ($payment_due_timestamp = wp_next_scheduled('subscriptio_scheduled_payment', $subscription->id)) {
            foreach ($subscription->get_reminders('pre_payment_due', $payment_due_timestamp) as $timestamp) {
                Subscriptio_Scheduler::schedule_reminder($subscription->id, $timestamp);
            }
        }

        // Update appropriate subscription fields with new order id
        $subscription->update_subscription_details(array(
            'last_order_id' => $order_id,
            'all_order_ids' => $order_id,
        ));

        // Create a new order object
        $order = new WC_Order($order_id);

        // Set status to pending (pre- WooCommerce 2.2)
        if (!Subscriptio::wc_version_gte('2.2')) {
            $order->update_status('pending');
        }

        // Send New Order email
        Subscriptio_Mailer::send('new_order', $order);

        return $order_id;
    }
コード例 #4
0
    /**
     * Return formatted subscription price to be displayed on product pages and cart
     * 
     * @access public
     * @param int $id
     * @param bool $is_checkout
     * @param int $quantity
     * @param float $price_now
     * @param bool $is_variable
     * @param bool $variations_equal
     * @return string
     */
    public static function get_formatted_subscription_price($id, $is_checkout = false, $quantity = 1, $price_now = null, $is_variable = false, $variations_equal = false)
    {
        $meta = Subscriptio::unwrap_post_meta(get_post_meta($id));

        $recurring_price = Subscriptio_Subscription_Product::get_recurring_price($id);

        // Check if product is configured properly, if not - revert to standard price display
        if (!isset($meta['_subscriptio_price_time_unit']) || !isset($meta['_subscriptio_price_time_value'])) {
            return Subscriptio::get_formatted_price($recurring_price, get_woocommerce_currency());
        }

        // Cart/checkout page?
        if ($is_checkout) {

            $recurring_price_html = self::format_recurring_amount(($recurring_price * $quantity), $meta['_subscriptio_price_time_unit'], $meta['_subscriptio_price_time_value']);

            // Payable now differs from recurring amount?
            if ($price_now != $recurring_price) {
                $html_now = Subscriptio::get_formatted_price(($price_now * $quantity), get_woocommerce_currency());
                return sprintf(__('%1$s now then %2$s', 'subscriptio'), $html_now, $recurring_price_html);
            }
            else {
                return self::format_recurring_amount(($price_now * $quantity), $meta['_subscriptio_price_time_unit'], $meta['_subscriptio_price_time_value']);
            }
        }

        // Other pages
        else {

            $recurring_price_html = self::format_recurring_amount($recurring_price, $meta['_subscriptio_price_time_unit'], $meta['_subscriptio_price_time_value']);

            // Any signup fee?
            if (!empty($meta['_subscriptio_signup_fee'])) {
                $signup_fee = Subscriptio::get_formatted_price($meta['_subscriptio_signup_fee'], get_woocommerce_currency());
            }

            // Any free trial?
            if (!empty($meta['_subscriptio_free_trial_time_value'])) {
                $time_units = Subscriptio::get_time_units();

                if (isset($time_units[$meta['_subscriptio_free_trial_time_unit']]) && is_numeric($meta['_subscriptio_free_trial_time_value'])) {
                    $free_trial = $meta['_subscriptio_free_trial_time_value'] . ' ' . call_user_func($time_units[$meta['_subscriptio_free_trial_time_unit']]['translation_callback'], $meta['_subscriptio_free_trial_time_unit'], $meta['_subscriptio_free_trial_time_value']);
                }
                else {
                    $free_trial = '';
                }
            }

            // Free trial & Signup fee
            if (!empty($meta['_subscriptio_signup_fee']) && !empty($meta['_subscriptio_free_trial_time_value'])) {
                if ($is_variable && !$variations_equal) {
                    return sprintf(__('From %1$s', 'subscriptio'), $recurring_price_html);
                }
                else {
                    return sprintf(__('%1$s with a free trial of %2$s and a sign-up fee of %3$s', 'subscriptio'), $recurring_price_html, $free_trial, $signup_fee);
                }
            }

            // Free trial
            else if (!empty($meta['_subscriptio_free_trial_time_value'])) {
                if ($is_variable && !$variations_equal) {
                    return sprintf(__('From %1$s', 'subscriptio'), $recurring_price_html);
                }
                else {
                    return sprintf(__('%1$s with a free trial of %2$s', 'subscriptio'), $recurring_price_html, $free_trial);
                }
            }

            // Signup fee
            else if (!empty($meta['_subscriptio_signup_fee'])) {
                if ($is_variable && !$variations_equal) {
                    return sprintf(__('From %1$s', 'subscriptio'), $recurring_price_html);
                }
                else {
                    return sprintf(__('%1$s with a sign-up fee of %2$s', 'subscriptio'), $recurring_price_html, $signup_fee);
                }
            }

            // Plain recurring price
            else {
                if ($is_variable && !$variations_equal) {
                    return sprintf(__('From %s', 'subscriptio'), $recurring_price_html);
                }
                else {
                    return $recurring_price_html;
                }
            }
        }
    }