yoone-wc-product-bundles/includes/integration/class-yoone-pb-apfs-integra...

138 lines
5.9 KiB
PHP

<?php
/**
* Integration: WooCommerce All Products for Subscriptions (APFS)
*
* Enables subscription scheme UI and cart behavior on custom product type 'yoone_bundle'.
* - Adds 'yoone_bundle' to supported product types.
* - Marks key features as supported via 'wcsatt_product_supports_feature'.
* - Ensures the SATT admin tab shows on 'yoone_bundle' products.
* - Propagates the chosen plan from the bundle container to its children in cart.
*/
defined('ABSPATH') || exit;
class Yoone_PB_APFS_Integration {
public static function init() {
// Extend supported product types.
add_filter('wcsatt_supported_product_types', array(__CLASS__, 'extend_supported_types'));
// Declare features support for our product type.
add_filter('wcsatt_product_supports_feature', array(__CLASS__, 'product_supports_feature'), 10, 3);
// Show SATT tab on yoone_bundle admin screens.
add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'add_show_if_class_for_tab'), 20, 1);
// After APFS applies a scheme on a cart item, propagate the scheme from container to its children.
add_action('wcsatt_applied_cart_item_subscription_scheme', array(__CLASS__, 'propagate_scheme_to_children'), 10, 2);
// Tweak single-product option data for yoone_bundle to avoid $0.00 confusion.
add_filter('wcsatt_single_product_one_time_option_data', array(__CLASS__, 'filter_single_option_price_html'), 10, 3);
add_filter('wcsatt_single_product_subscription_option_data', array(__CLASS__, 'filter_single_option_price_html'), 10, 4);
}
/**
* Add 'yoone_bundle' to APFS supported product types.
*
* @param array $types
* @return array
*/
public static function extend_supported_types($types) {
$types[] = Yoone_Product_Bundles::TYPE; // 'yoone_bundle'
return array_values(array_unique($types));
}
/**
* Adjust the price HTML shown in APFS single-product options for yoone_bundle.
* For our zero-priced container, show a friendly message instead of $0.00.
*
* @param array $data
* @param WC_Product $product
* @param string $key_or_scheme_key
* @param mixed $scheme Optional.
* @return array
*/
public static function filter_single_option_price_html($data, $product = null, $key_or_scheme_key = null, $scheme = null) {
try {
if ($product && is_a($product, 'WC_Product') && $product->get_type() === Yoone_Product_Bundles::TYPE) {
// Replace price_html with hint, keep descriptions intact.
$data['price_html'] = '<span class="yoone-bundle-option-price-hint">' . esc_html__('Price depends on selected items.', 'yoone-product-bundles') . '</span>';
}
} catch (\Throwable $e) {
// Fail silently to avoid breaking APFS rendering.
}
return $data;
}
/**
* Declare APFS features supported by yoone_bundle products.
* Mirrors capabilities of official 'bundle' type so APFS renders single-product options.
*
* @param bool $supported
* @param WC_Product $product
* @param string $feature
* @return bool
*/
public static function product_supports_feature($supported, $product, $feature) {
if (! $product || ! is_a($product, 'WC_Product')) {
return $supported;
}
if ($product->get_type() !== Yoone_Product_Bundles::TYPE) {
return $supported;
}
switch ($feature) {
case 'subscription_schemes':
case 'subscription_scheme_switching':
case 'subscription_content_switching':
case 'subscription_scheme_options_product_single':
case 'subscription_scheme_options_product_cart':
case 'subscription_management_add_to_subscription_product_single':
return true;
}
return $supported;
}
/**
* Ensure SATT admin tab is visible for yoone_bundle products.
*
* @param array $tabs
* @return array
*/
public static function add_show_if_class_for_tab($tabs) {
if (isset($tabs['satt'])) {
if (! isset($tabs['satt']['class']) || ! is_array($tabs['satt']['class'])) {
$tabs['satt']['class'] = array();
}
$tabs['satt']['class'][] = 'show_if_' . Yoone_Product_Bundles::TYPE; // show_if_yoone_bundle
}
return $tabs;
}
/**
* Ensure children of a yoone_bundle container inherit the same subscription scheme when it changes.
*
* @param array $cart_item
* @param string $cart_item_key
*/
public static function propagate_scheme_to_children($cart_item, $cart_item_key) {
if (empty($cart_item['yoone_bundle_container_id'])) {
// Not a container; skip.
return;
}
if (! isset($cart_item['wcsatt_data'])) {
return;
}
$active_scheme = isset($cart_item['wcsatt_data']['active_subscription_scheme']) ? $cart_item['wcsatt_data']['active_subscription_scheme'] : null;
if (null === $active_scheme) {
// No explicit choice made; let APFS defaults handle children.
return;
}
$container_id = $cart_item['yoone_bundle_container_id'];
foreach (WC()->cart->get_cart() as $child_key => $child_item) {
if (isset($child_item['yoone_bundle_parent_id']) && $child_item['yoone_bundle_parent_id'] === $container_id) {
if (! isset(WC()->cart->cart_contents[$child_key]['wcsatt_data'])) {
WC()->cart->cart_contents[$child_key]['wcsatt_data'] = array();
}
WC()->cart->cart_contents[$child_key]['wcsatt_data']['active_subscription_scheme'] = $active_scheme;
}
}
}
}
// Initialize after plugins are loaded to ensure APFS hooks are available.
add_action('plugins_loaded', array('Yoone_PB_APFS_Integration', 'init'), 20);