Add package addons and options
This commit is contained in:
@@ -51,10 +51,33 @@ class Wiaas_Package_Pricing {
|
||||
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_product_get_price', array( __CLASS__, 'on_get_price' ), 10, 2 );
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
|
||||
add_filter('woocommerce_checkout_create_order_line_item_object', array( __CLASS__, 'record_price_for_order_line_item' ), 10, 4);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,30 +97,24 @@ class Wiaas_Package_Pricing {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customer selected package price from session when adding package to cart
|
||||
* @param $base_price
|
||||
* @param $product
|
||||
*
|
||||
* @return mixed
|
||||
* 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_get_price($base_price, $package) {
|
||||
if ( empty( $package ) ||
|
||||
empty( $base_price ) ||
|
||||
$package->get_type() !== 'bundle' ||
|
||||
!isset(WC()->session)) {
|
||||
return $base_price;
|
||||
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 );
|
||||
}
|
||||
}
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,12 +126,32 @@ class Wiaas_Package_Pricing {
|
||||
*
|
||||
* @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']);
|
||||
}
|
||||
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'])) {
|
||||
|
||||
return $order_item;
|
||||
$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'
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user