コード例 #1
0
    /**
     * Return subscription length
     * $units_to and $units_from should be passed in a singular form (e.g. day)
     * 
     * @access public
     * @param string $units_to
     * @param string $units_from
     * @param int $value
     * @return mixed
     */
    public static function get_period_length_in($units_to, $units_from, $value)
    {
        // Get time units
        $time_units = Subscriptio::get_time_units();

        // Check if given units are supported
        if (!isset($time_units[$units_from])) {
            return false;
        }

        // Extend with more units to convert to
        $time_units = array_merge(array(
            'second'    => array(
                'seconds'   => 1,
            ),
            'minute'    => array(
                'seconds'   => 60,
            ),
            'hour'      => array(
                'seconds'   => 3600,
            ),
        ), Subscriptio::get_time_units());

        // Check if units to convert to are supported
        if (!isset($time_units[$units_to])) {
            return false;
        }

        // Check if $value is a number
        if (!is_numeric($value) || $value < 0) {
            return false;
        }

        // Calculate value in seconds
        $value_in_seconds = $value * $time_units[$units_from]['seconds'];

        // Use scale of 1:10080 (1 minute = 1 week) when debugging
        $value_in_seconds = Subscriptio::$debug ? $value_in_seconds / 10080 : $value_in_seconds;

        // Calculate value in required units
        return round($value_in_seconds / $time_units[$units_to]['seconds']);
    }
コード例 #2
0
ファイル: simple-product-meta.php プロジェクト: qhuit/dcosta
                <option value="<?php echo $unit_key; ?>" <?php echo $_subscriptio_price_time_unit == $unit_key ? 'selected="selected"' : ''; ?>><?php echo call_user_func($unit['translation_callback'], $unit_key, 2); ?></option>
            <?php endforeach; ?>
        </select>
    </p>

    <p class="form-field _subscriptio_free_trial_field">
        <label for="_subscriptio_free_trial_time_value"><?php _e('Free trial', 'subscriptio'); ?></label>
        <input type="text" class="input-text subscriptio_product_page_half_width" id="_subscriptio_free_trial_time_value" name="_subscriptio_free_trial_time_value" placeholder="<?php _e('e.g. 3', 'subscriptio'); ?>" value="<?php echo $_subscriptio_free_trial_time_value; ?>">
        <select id="_subscriptio_free_trial_time_unit" name="_subscriptio_free_trial_time_unit" class="select subscriptio_product_page_half_width" style="margin-left: 3px;">
            <?php foreach (Subscriptio::get_time_units() as $unit_key => $unit): ?>
                <option value="<?php echo $unit_key; ?>" <?php echo $_subscriptio_free_trial_time_unit == $unit_key ? 'selected="selected"' : ''; ?>><?php echo call_user_func($unit['translation_callback'], $unit_key, 2); ?></option>
            <?php endforeach; ?>
        </select>
    </p>

    <p class="form-field _subscriptio_signup_fee_field">
        <label for="_subscriptio_signup_fee"><?php _e('Sign-up fee', 'subscriptio'); ?> <?php echo (function_exists('get_woocommerce_currency_symbol') ? '(' . get_woocommerce_currency_symbol() . ')' : ''); ?></label>
        <input type="text" class="short" name="_subscriptio_signup_fee" id="_subscriptio_signup_fee" placeholder="<?php _e('e.g. 9.99', 'subscriptio'); ?>" value="<?php echo $_subscriptio_signup_fee; ?>">
    </p>

    <p class="form-field _subscriptio_max_length_field">
        <label for="_subscriptio_max_length_time_value"><?php _e('Max length', 'subscriptio'); ?></label>
        <input type="text" class="input-text subscriptio_product_page_half_width" id="_subscriptio_max_length_time_value" name="_subscriptio_max_length_time_value" placeholder="<?php _e('e.g. 90', 'subscriptio'); ?>" value="<?php echo $_subscriptio_max_length_time_value; ?>">
        <select id="_subscriptio_max_length_time_unit" name="_subscriptio_max_length_time_unit" class="select subscriptio_product_page_half_width" style="margin-left: 3px;">
            <?php foreach (Subscriptio::get_time_units() as $unit_key => $unit): ?>
                <option value="<?php echo $unit_key; ?>" <?php echo $_subscriptio_max_length_time_unit == $unit_key ? 'selected="selected"' : ''; ?>><?php echo call_user_func($unit['translation_callback'], $unit_key, 2); ?></option>
            <?php endforeach; ?>
        </select>
    </p>

</div>
コード例 #3
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;
                }
            }
        }
    }