146 lines
4.5 KiB
PHP
146 lines
4.5 KiB
PHP
<?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(); |