product details
This commit is contained in:
@@ -1,134 +1,41 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements addons for standard wiaas package types
|
||||
*
|
||||
* Class Wiaas_Package_Addon
|
||||
*/
|
||||
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' ) );
|
||||
add_filter('wiaas_package_types', array( __CLASS__, 'register_addon_package_type' ) );
|
||||
}
|
||||
|
||||
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 );
|
||||
/**
|
||||
* Registers addon as available wiaas package type
|
||||
* @param $package_types
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function register_addon_package_type($package_types) {
|
||||
$package_types[] = 'add_on';
|
||||
|
||||
// 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();
|
||||
return $package_types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves addons configured for provided wiaas standard package type
|
||||
* @param $package
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_package_addons($package) {
|
||||
$addon_ids = $package->get_meta( self::$package_addons_meta_key );
|
||||
$addon_ids = $package->get_meta( '__wiaas_package_addons' );
|
||||
|
||||
$addons = array();
|
||||
|
||||
@@ -141,6 +48,16 @@ class Wiaas_Package_Addon {
|
||||
|
||||
return $addons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets addons for provided wiaas standard package type
|
||||
* @param $package
|
||||
* @param $children_ids
|
||||
*/
|
||||
public static function set_package_addons($package, $children_ids) {
|
||||
$package->update_meta_data( '__wiaas_package_addons', $children_ids );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Addon::init();
|
||||
@@ -1,136 +1,63 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementas groups of optional packages available for wiaas standard package type
|
||||
*
|
||||
* Class Wiaas_Package_Option_Groups
|
||||
*/
|
||||
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' ) );
|
||||
add_filter('wiaas_package_types', array( __CLASS__, 'register_option_package_type' ) );
|
||||
}
|
||||
|
||||
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 );
|
||||
/**
|
||||
* Register option as ne of available wiaas package types
|
||||
* @param $package_types
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function register_option_package_type($package_types) {
|
||||
$package_types[] = 'option';
|
||||
|
||||
// Add option items to the cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_options_to_cart' ), 10, 6 );
|
||||
return $package_types;
|
||||
}
|
||||
|
||||
private static function _remove_options_hooks() {
|
||||
/**
|
||||
* Retrives options group name for package option
|
||||
* @param $package
|
||||
* @param $option_package
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get_group_name_for_package_option($package, $option_package) {
|
||||
$option_groups = $package->get_meta( '_wiaas_package_option_groups' );
|
||||
|
||||
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;
|
||||
foreach ($option_groups as $option_group) {
|
||||
foreach ($option_group['options'] as $group_option_id) {
|
||||
if ((int) $group_option_id === $option_package->get_id()) {
|
||||
return $option_group['name'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve groups of optional packages configured for provided package
|
||||
* @param $package
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_package_option_groups($package) {
|
||||
|
||||
$groups_data = $package->get_meta( self::$package_option_groups_meta_key );
|
||||
$groups_data = $package->get_meta( '_wiaas_package_option_groups' );
|
||||
|
||||
$option_groups = array();
|
||||
|
||||
@@ -153,6 +80,16 @@ class Wiaas_Package_Option_Groups {
|
||||
|
||||
return $option_groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set groups of optional packages for provided package
|
||||
* @param $package
|
||||
* @param $groups_data
|
||||
*/
|
||||
public static function set_package_option_groups($package, $groups_data) {
|
||||
$package->update_meta_data( '_wiaas_package_option_groups', $groups_data );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Option_Groups::init();
|
||||
@@ -1,204 +0,0 @@
|
||||
<?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_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();
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class Wiaas_Package_Type
|
||||
*/
|
||||
|
||||
|
||||
class Wiaas_Package_Type {
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_action('init', array( __CLASS__, 'register_package_type_taxonomy' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register wiaas package type taxonomy
|
||||
*/
|
||||
public static function register_package_type_taxonomy() {
|
||||
|
||||
$labels = array(
|
||||
'name' => _x( 'Package type', 'taxonomy general name', 'wiaas' ),
|
||||
'singular_name' => _x( 'Package type', 'taxonomy singular name', 'wiaas' ),
|
||||
'search_items' => __( 'Search Package types', 'wiaas' ),
|
||||
'all_items' => __( 'All Package types', 'wiaas' ),
|
||||
'parent_item' => __( 'Parent Package type', 'wiaas' ),
|
||||
'parent_item_colon' => __( 'Parent Package type:', 'wiaas' ),
|
||||
'edit_item' => __( 'Edit Package type', 'wiaas' ),
|
||||
'update_item' => __( 'Update Package type', 'wiaas' ),
|
||||
'add_new_item' => __( 'Add New Package type', 'wiaas' ),
|
||||
'new_item_name' => __( 'New Package type Name', 'wiaas' ),
|
||||
'menu_name' => __( 'Package type', 'wiaas' ),
|
||||
);
|
||||
|
||||
$args = array(
|
||||
'hierarchical' => false,
|
||||
'labels' => $labels,
|
||||
'show_ui' => false,
|
||||
'show_admin_column' => true,
|
||||
'query_var' => true,
|
||||
'rewrite' => array( 'slug' => 'package_type' ),
|
||||
);
|
||||
|
||||
register_taxonomy( 'package_type', array( 'product' ), $args );
|
||||
|
||||
|
||||
// Register available package types
|
||||
// standard package type is default package type for wiaas packages
|
||||
|
||||
$types = apply_filters('wiaas_package_types', array('standard'));
|
||||
|
||||
foreach ($types as $type) {
|
||||
wp_insert_term($type, 'package_type');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve available wiaas package types
|
||||
* @return array
|
||||
*/
|
||||
public static function get_available_package_types() {
|
||||
$types = get_terms( array(
|
||||
'taxonomy' => 'package_type',
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
return array_map(function($type) {
|
||||
return $type->name;
|
||||
}, $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve package type for provided package id
|
||||
* @param $package_id
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public static function get_package_type($package_id) {
|
||||
$terms = wp_get_object_terms($package_id, 'package_type');
|
||||
$package_type = isset($terms[0]) ? $terms[0]->name : null;
|
||||
return $package_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set package type for provided package id
|
||||
* @param $package_id
|
||||
* @param $type
|
||||
*/
|
||||
public static function set_package_type($package_id, $type) {
|
||||
|
||||
wp_delete_object_term_relationships( $package_id, 'package_type' );
|
||||
|
||||
if (isset($type)) {
|
||||
wp_set_object_terms($package_id, $type, 'package_type', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Type::init();
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Collect optional packages for provided order item package
|
||||
* @param $order_items
|
||||
* @param $parent_order_item
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect optional packages cart items for provided cart item
|
||||
* @param $cart_item
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_get_cart_item_options($cart_item) {
|
||||
$option_cart_items = array();
|
||||
|
||||
if (isset($cart_item['_wiaas_option_items'])) {
|
||||
$option_cart_items_ids = $cart_item['_wiaas_option_items'];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect addon packages for provided order item package
|
||||
* @param $order_items
|
||||
* @param $parent_order_item
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect addon packages cart items for provided cart item
|
||||
* @param $cart_item
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_get_cart_item_addons($cart_item) {
|
||||
$addon_cart_items = array();
|
||||
|
||||
if (isset($cart_item['_wiaas_addon_items'])) {
|
||||
$addon_cart_items_ids = $cart_item['_wiaas_addon_items'];
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user