array( 'title' => 'Purchase', 'package_pay_period' => 0, 'services_contract_period' => 0, 'max_contract_period' => 36, 'period_unit' => 'month', 'labe' ), 'purchase_24' => array( 'title' => 'Purchase with 24M commitment', 'package_pay_period' => 0, 'services_contract_period' => 24, 'max_contract_period' => 36, 'period_unit' => 'month' ), 'managed_36' => array( 'title' => 'Managed service 36M rent', 'package_pay_period' => 36, 'services_contract_period'=> 36, 'max_contract_period' => 36, 'period_unit' => 'month' ) ); /** * Payment fields for pricing rules * @var array */ private static $payment_fields = array( 'minimal_fixed_price' => 0, 'principal_amount' => 0, 'minimal_services_price' => 0 ); /** * Meta key for pricing rules * @var string */ private static $package_prices_meta_key = '_wiaas_pricing_rules'; public static function init() { add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 ); add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) ); add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 ); add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1); add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1); } public static function add_cart_item_data($cart_item_data, $package_id) { if ( isset( $_POST[ 'price_id' ]) && WC_Product_Factory::get_product_type( $package_id ) === 'bundle') { $selected_price_id = $_POST['price_id']; $package = wc_get_product( $package_id ); $configured_package_prices = self::get_package_prices($package); $selected_price = $configured_package_prices[$selected_price_id]; if (isset($selected_price)) { $cart_item_data['_wiaas_payment'] = $selected_price; } } return $cart_item_data; } /** * Get configuration for available payment types * @return array */ public static function get_available_pay_types() { return self::$pay_types; } /** * Get empty payment rule with initialized fields to default values * @return array */ public static function get_empty_pricing_rule() { return self::$payment_fields; } /** * Update package cart item with `minimal_fixed_price` as its price * so resulting totals would be sum of these prices * @param $cart */ public static function on_calculate_totals($cart) { foreach ($cart->cart_contents as $key => $cart_item) { if (isset($cart_item['_wiaas_payment'])) { $price = $cart_item['_wiaas_payment']; WC()->cart->cart_contents[ $key ]['data']->set_price( $price['minimal_fixed_price'] ); } if (isset($cart_item['bundled_by'])) { WC()->cart->cart_contents[ $key ]['data']->set_price( 0 ); } } } /** * Persist used payment type informations for package in corresponding order line item * @param $order_item * @param $cart_item_key * @param $cart_item * @param $order * * @return mixed */ public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ) { if (wc_pb_is_bundle_container_cart_item($cart_item) && isset($cart_item['_wiaas_payment'])) { $payment = $cart_item['_wiaas_payment']; $order_item->add_meta_data( '_wiaas_payment_type', $payment['payment_type'], true ); $order_item->add_meta_data( '_wiaas_service_price', $payment['minimal_services_price'], true ); $order_item->add_meta_data( '_wiaas_service_contract_period', $payment['services_contract_period'], true ); $order_item->add_meta_data( '_wiaas_max_contract_period', $payment['max_contract_period'], true ); $order_item->add_meta_data( '_wiaas_period_unit', $payment['period_unit'], true ); $order_item->add_meta_data( '_wiaas_recurring_price', $payment['recurrent_price'], true ); $order_item->add_meta_data( '_wiaas_pay_period', $payment['package_pay_period'], true ); } } public static function hidden_order_item_meta( $hidden ) { return array_merge( $hidden, array( '_wiaas_payment_type', '_wiaas_service_price', '_wiaas_service_contract_period', '_wiaas_max_contract_period', '_wiaas_period_unit', '_wiaas_recurring_price', '_wiaas_pay_period' ) ); } /** * Retrieve configured payment prices for package * @param $package * * @return array */ public static function get_package_prices($package) { $pricing_rules = $package->get_meta( self::$package_prices_meta_key ); $prices = array(); foreach ($pricing_rules as $type => $pricing_rule) { $pay_type = self::$pay_types[$type]; $prices[$type] = array( 'id' => $type, 'payment_type' => $pay_type['title'], 'max_contract_period' => $pay_type['max_contract_period'], 'package_pay_period' => floatval($pay_type['package_pay_period']), 'period_unit' => $pay_type['period_unit'], 'services_contract_period' => $pay_type['services_contract_period'], 'minimal_fixed_price' => floatval($pricing_rule['minimal_fixed_price']), 'principal_amount' => floatval($pricing_rule['principal_amount']), 'recurrent_price' => 0, 'minimal_services_price' => floatval($pricing_rule['minimal_services_price']), ); } return $prices; } /** * Persist payment prices configuration for package * @param $package * @param $pricing_rules */ public static function set_package_prices($package, $pricing_rules) { if ( isset( $pricing_rules ) ) { $package->update_meta_data( self::$package_prices_meta_key, $pricing_rules ); } else { $package->delete_meta_data( self::$package_prices_meta_key ); } $package->save_meta_data(); } } Wiaas_Package_Pricing::init();