Add package addons and options
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Package_Addon {
|
||||
|
||||
private static $package_addons_meta_key = '_wiaas_package_addons';
|
||||
|
||||
private static $cart_item_addon_items_key = '_wiaas_addon_items';
|
||||
|
||||
private static $cart_item_addon_parent_key = '_wiaas_addon_for';
|
||||
|
||||
public static function init() {
|
||||
|
||||
self::_add_addons_hooks();
|
||||
|
||||
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' ) );
|
||||
}
|
||||
|
||||
private static function _add_addons_hooks() {
|
||||
// Add bundle-specific cart item data based on posted vars.
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
// Add addon items to the cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_addons_to_cart' ), 10, 6 );
|
||||
}
|
||||
|
||||
private static function _remove_addons_hooks() {
|
||||
|
||||
remove_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ));
|
||||
|
||||
remove_action('woocommerce_add_to_cart', array( __CLASS__, 'add_addons_to_cart' ));
|
||||
}
|
||||
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ){
|
||||
if (isset($cart_item[self::$cart_item_addon_items_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_addon_items_key, $cart_item[self::$cart_item_addon_items_key] );
|
||||
}
|
||||
if (isset($cart_item[self::$cart_item_addon_parent_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_addon_parent_key, $cart_item[self::$cart_item_addon_parent_key], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
self::$cart_item_addon_items_key,
|
||||
self::$cart_item_addon_parent_key,
|
||||
) );
|
||||
}
|
||||
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
|
||||
// If cart item that will be added is actually requested package
|
||||
if ($_POST['package_id'] = $package_id) {
|
||||
// Prepare additional data for later use.
|
||||
if ( ! isset( $cart_item_data[self::$cart_item_addon_items_key] ) ) {
|
||||
$cart_item_data[self::$cart_item_addon_items_key ] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
public static function add_addons_to_cart($cart_item_key, $package_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
||||
|
||||
$is_addon_parent = $_POST['package_id'] = $package_id && isset($cart_item_data[self::$cart_item_addon_items_key]);
|
||||
$has_selected_addons = isset($_POST['addons']) && is_array($_POST['addons']);
|
||||
|
||||
self::_remove_addons_hooks();
|
||||
|
||||
if ($is_addon_parent && $has_selected_addons) {
|
||||
$addons_ids = $_POST['addons'];
|
||||
|
||||
foreach ($addons_ids as $addon_id) {
|
||||
$addon_package = wc_get_product($addon_id);
|
||||
if (is_object($addon_package)) {
|
||||
|
||||
$addon_cart_item_key = WC()->cart->add_to_cart($addon_id);
|
||||
|
||||
if ($addon_cart_item_key) {
|
||||
WC()->cart->cart_contents[$addon_cart_item_key][self::$cart_item_addon_parent_key] = $cart_item_key;
|
||||
|
||||
WC()->cart->cart_contents[ $cart_item_key ][self::$cart_item_addon_items_key][] = $addon_cart_item_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::_add_addons_hooks();
|
||||
}
|
||||
|
||||
public static function get_cart_item_addons($cart_item) {
|
||||
$addon_cart_items = array();
|
||||
|
||||
if (isset($cart_item[self::$cart_item_addon_items_key])) {
|
||||
$addon_cart_items_ids = $cart_item[self::$cart_item_addon_items_key];
|
||||
|
||||
foreach ($addon_cart_items_ids as $addon_cart_item_id) {
|
||||
$addon_cart_item = WC()->cart->get_cart_item($addon_cart_item_id);
|
||||
|
||||
if (isset($addon_cart_item)) {
|
||||
$addon_cart_items[] = $addon_cart_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $addon_cart_items;
|
||||
}
|
||||
|
||||
public static function get_order_item_addons($order_items, $parent_order_item) {
|
||||
$addon_order_items = array();
|
||||
|
||||
if (isset($parent_order_item['wiaas_addon_items']) && isset($parent_order_item['bundle_cart_key'])) {
|
||||
foreach ($order_items as $order_item) {
|
||||
if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_addon_for'] === $parent_order_item['bundle_cart_key']) {
|
||||
$addon_order_items[] = $order_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $addon_order_items;
|
||||
}
|
||||
|
||||
public static function set_package_addons($package, $children_ids) {
|
||||
$package->update_meta_data( self::$package_addons_meta_key, $children_ids );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
|
||||
public static function get_package_addons($package) {
|
||||
$addon_ids = $package->get_meta( self::$package_addons_meta_key );
|
||||
|
||||
$addons = array();
|
||||
|
||||
foreach ($addon_ids as $addon_id) {
|
||||
$addon_package = wc_get_product( $addon_id );
|
||||
if (is_object($addon_package)) {
|
||||
$addons[] = $addon_package;
|
||||
}
|
||||
}
|
||||
|
||||
return $addons;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Addon::init();
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Package_Option_Groups {
|
||||
|
||||
private static $package_option_groups_meta_key = '_wiaas_package_option_groups';
|
||||
|
||||
private static $cart_item_option_items_key = '_wiaas_option_items';
|
||||
|
||||
private static $cart_item_option_parent_key = '_wiaas_option_for';
|
||||
|
||||
public static function init() {
|
||||
|
||||
self::_add_options_hooks();
|
||||
|
||||
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' ) );
|
||||
}
|
||||
|
||||
private static function _add_options_hooks() {
|
||||
// Add bundle-specific cart item data based on posted vars.
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
// Add option items to the cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_options_to_cart' ), 10, 6 );
|
||||
}
|
||||
|
||||
private static function _remove_options_hooks() {
|
||||
|
||||
remove_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ));
|
||||
|
||||
remove_action('woocommerce_add_to_cart', array( __CLASS__, 'add_options_to_cart' ));
|
||||
}
|
||||
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ){
|
||||
if (isset($cart_item[self::$cart_item_option_items_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_option_items_key, $cart_item[self::$cart_item_option_items_key] );
|
||||
}
|
||||
if (isset($cart_item[self::$cart_item_option_parent_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_option_parent_key, $cart_item[self::$cart_item_option_parent_key], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
self::$cart_item_option_items_key,
|
||||
self::$cart_item_option_parent_key,
|
||||
) );
|
||||
}
|
||||
|
||||
public static function set_package_option_groups($package, $groups_data) {
|
||||
$package->update_meta_data( self::$package_option_groups_meta_key, $groups_data );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
|
||||
// If cart item that will be added is actually requested package
|
||||
if ($_POST['package_id'] = $package_id) {
|
||||
// Prepare additional data for later use.
|
||||
if ( ! isset( $cart_item_data[self::$cart_item_option_items_key] ) ) {
|
||||
$cart_item_data[self::$cart_item_option_items_key ] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
public static function add_options_to_cart($cart_item_key, $package_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
||||
|
||||
$is_option_parent = $_POST['package_id'] = $package_id && isset($cart_item_data[self::$cart_item_option_items_key]);
|
||||
$has_selected_options = isset($_POST['options']) && is_array($_POST['options']);
|
||||
|
||||
if ($is_option_parent && $has_selected_options) {
|
||||
|
||||
self::_remove_options_hooks();
|
||||
|
||||
$options_ids = $_POST['options'];
|
||||
|
||||
foreach ($options_ids as $option_id) {
|
||||
$option_package = wc_get_product($option_id);
|
||||
if (is_object($option_package)) {
|
||||
|
||||
$option_cart_item_key = WC()->cart->add_to_cart($option_id);
|
||||
|
||||
if ($option_cart_item_key) {
|
||||
WC()->cart->cart_contents[$option_cart_item_key][self::$cart_item_option_parent_key] = $cart_item_key;
|
||||
|
||||
WC()->cart->cart_contents[ $cart_item_key ][self::$cart_item_option_items_key][] = $option_cart_item_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::_add_options_hooks();
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_cart_item_options($cart_item) {
|
||||
$option_cart_items = array();
|
||||
|
||||
if (isset($cart_item[self::$cart_item_option_items_key])) {
|
||||
$option_cart_items_ids = $cart_item[self::$cart_item_option_items_key];
|
||||
|
||||
foreach ($option_cart_items_ids as $option_cart_item_id) {
|
||||
$option_cart_item = WC()->cart->get_cart_item($option_cart_item_id);
|
||||
|
||||
if (isset($option_cart_item)) {
|
||||
$option_cart_items[] = $option_cart_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $option_cart_items;
|
||||
}
|
||||
|
||||
public static function get_order_item_options($order_items, $parent_order_item) {
|
||||
$option_order_items = array();
|
||||
|
||||
if (isset($parent_order_item['wiaas_option_items']) && isset($parent_order_item['bundle_cart_key'])) {
|
||||
foreach ($order_items as $order_item) {
|
||||
if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_option_for'] === $parent_order_item['bundle_cart_key']) {
|
||||
$option_order_items[] = $order_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $option_order_items;
|
||||
}
|
||||
|
||||
public static function get_package_option_groups($package) {
|
||||
|
||||
$groups_data = $package->get_meta( self::$package_option_groups_meta_key );
|
||||
|
||||
$option_groups = array();
|
||||
|
||||
foreach ($groups_data as $group_data) {
|
||||
$group = array(
|
||||
'id' => $group_data['id'],
|
||||
'name' => $group_data['name'],
|
||||
'default' => $group_data['default'],
|
||||
'options' => array()
|
||||
);
|
||||
|
||||
foreach ($group_data['options'] as $option_id) {
|
||||
$option_package = wc_get_product( $option_id );
|
||||
if (is_object($option_package)) {
|
||||
$group['options'][] = $option_package;
|
||||
}
|
||||
}
|
||||
$option_groups[] = $group;
|
||||
}
|
||||
|
||||
return $option_groups;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Option_Groups::init();
|
||||
@@ -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