spivurno
2/1/2013 - 4:44 PM

gistfile1.php

<?php

/**
Plugin Name: GW Modify Subscription Pricing
Plugin URI: http://kovick.com/
Description: A plugin for WooCommerce + Subscription Add-on + Gravity Form Products Add-on which only adds the pricing to the signup fee rather than to the recurring fee.
Version: 1.0
Author: David Smith
Author URI: http://ounceoftalent.com
License: GPL2
*/

/**
 * Modify Subscription Pricing Display
 */

class GWModifySubscriptionPricing {

    function __construct() {

        add_action('init', array( $this, 'hooks' ) );
        
        add_filter( 'woocommerce_add_cart_item', array( $this, 'update_cart_item' ), 11 );
        add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'update_cart_item'), 11 );

    }

    function hooks() {

        add_action( 'woocommerce_gform_base_price', array( &$this, 'modify_base_price' ), 10, 2 );
        add_action( 'woocommerce_gform_total_price', array( &$this, 'modify_total_price' ), 10, 2 );
        add_action( 'woocommerce_before_calculate_totals', array( $this, 'remove_subscription_calculation'), 11 );
        
    }

    function modify_base_price( $price, $product_data ) {
        
        if( !WC_Subscriptions_Product::is_subscription( $product_data->id ))
            return $price;
            
        return woocommerce_price( $this->get_subscription_sign_up_fee( $product_data->id ) );
    }

    function modify_total_price( $price, $product_data ) {

        if( !WC_Subscriptions_Product::is_subscription( $product_data->id ))
            return $price;
            
        $gform_total = isset($_POST['gform_total']) ? $_POST['gform_total'] : 0;
        $signup_fee = $this->get_subscription_sign_up_fee( $product_data->id );
        $total = $signup_fee + $gform_total;

        return woocommerce_price( $total ) . ' + ' . woocommerce_price( $product_data->get_price() ) . ' / month';
    }

    function remove_subscription_calculation() {
        // thought I might need to only remove this if there is a subscription, but that is the only time it would have run anyways
        remove_action('woocommerce_before_calculate_totals', array( 'WC_Subscriptions_Cart', 'add_calculation_price_filter') );
    }
    
    function update_cart_item( $cart_item ) {
        
        if( !WC_Subscriptions_Product::is_subscription( $cart_item['data']->id ))
            return $cart_item;
        
        $options = ( $cart_item['data']->price - ( $cart_item['data']->regular_price * 2) );
        
        $cart_item['data']->product_custom_fields['_subscription_sign_up_fee'][0] += $options;
        $cart_item['data']->set_price( $cart_item['data']->regular_price );
        
        return $cart_item;
    }



    // HELPERS //

    function get_subscription_sign_up_fee( $post_id ) {
        return get_post_meta( $post_id, '_subscription_sign_up_fee', true);
    }

    function is_subscription_product( $product ) {
        $product = is_object( $product ) ? $product : new WC_Product( $product );
        return $product && $product->product_type == 'subscription';
    }

}

$gw_modify_subscription_pricing = new GWModifySubscriptionPricing();