Add support for package payment methods and basic checkout proccess
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Wiaas_Package_Pricing
|
||||
*/
|
||||
class Wiaas_Package_Pricing {
|
||||
|
||||
/**
|
||||
* Available pay types for wiaas packages
|
||||
* @var array
|
||||
*/
|
||||
private static $pay_types = array(
|
||||
'purchase' => 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_filter( 'woocommerce_product_get_price', array( __CLASS__, 'on_get_price' ), 10, 2 );
|
||||
|
||||
add_filter('woocommerce_checkout_create_order_line_item_object', array( __CLASS__, 'record_price_for_order_line_item' ), 10, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customer selected package price from session when adding package to cart
|
||||
* @param $base_price
|
||||
* @param $product
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function on_get_price($base_price, $package) {
|
||||
if ( empty( $package ) ||
|
||||
empty( $base_price ) ||
|
||||
$package->get_type() !== 'bundle' ||
|
||||
!isset(WC()->session)) {
|
||||
return $base_price;
|
||||
}
|
||||
$result_price = $base_price;
|
||||
|
||||
$preferred_price_id = WC()->session->get('wiaas_price_' . $package->get_id(), null);
|
||||
|
||||
if (isset($preferred_price_id)) {
|
||||
$prices = self::get_package_prices($package);
|
||||
$preferred_price = $prices[$preferred_price_id];
|
||||
$result_price = $preferred_price['minimal_fixed_price'];
|
||||
}
|
||||
|
||||
return $result_price;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 record_price_for_order_line_item($order_item, $cart_item_key, $cart_item, $order) {
|
||||
if (wc_pb_is_bundle_container_cart_item($cart_item)) {
|
||||
$order_item->add_meta_data('_wiaas_payment', $cart_item['_wiaas_price']);
|
||||
}
|
||||
|
||||
return $order_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
Reference in New Issue
Block a user