/** * Converts a cart item to a subscription. * * @param array $cart_item * @return array */ public static function convert_to_sub($cart_item) { if ($active_subscription_scheme = WCS_ATT_Schemes::get_active_subscription_scheme($cart_item)) { $cart_item['data']->is_converted_to_sub = 'yes'; $subscription_prices = WCS_ATT_Scheme_Prices::get_active_subscription_scheme_prices($cart_item, $active_subscription_scheme); if (!empty($subscription_prices)) { $cart_item['data']->price = $subscription_prices['price']; $cart_item['data']->regular_price = $subscription_prices['regular_price']; $cart_item['data']->sale_price = $subscription_prices['sale_price']; $cart_item['data']->subscription_price = $subscription_prices['price']; } $cart_item['data']->subscription_period = $active_subscription_scheme['subscription_period']; $cart_item['data']->subscription_period_interval = $active_subscription_scheme['subscription_period_interval']; $cart_item['data']->subscription_length = $active_subscription_scheme['subscription_length']; } else { $cart_item['data']->is_converted_to_sub = 'no'; } return apply_filters('wcsatt_cart_item', $cart_item); }
/** * Bundled cart items inherit the subscription schemes of their parent if: * - parent is statically priced, or * - parent has subscription schemes defined at product-level. * * @param array $schemes * @param array $cart_item * @param string $scope * @return array */ public static function get_bundled_item_schemes($schemes, $cart_item, $scope) { $container_key = self::has_bundle_type_container($cart_item); if (false !== $container_key) { if (isset(WC()->cart->cart_contents[$container_key])) { $container_cart_item = WC()->cart->cart_contents[$container_key]; if (self::overrides_child_schemes($container_cart_item)) { $schemes = WCS_ATT_Schemes::get_subscription_schemes($container_cart_item, $scope); foreach ($schemes as $scheme) { if (WCS_ATT_Scheme_Prices::has_subscription_price_override($scheme) && $scheme['subscription_pricing_method'] === 'override') { $scheme['subscription_pricing_method'] = 'inherit'; $scheme['subscription_discount'] = ''; } } } } } return $schemes; }
/** * Filter get_sale_price() calls to take price overrides into account. * * @param double $price unmodified reg price * @param WC_Product $product the bundled product * @return double modified reg price */ public static function filter_get_sale_price($sale_price, $product) { $subscription_scheme = self::$price_overriding_scheme; if ($subscription_scheme) { self::$price_overriding_scheme = false; $prices_array = array('price' => $product->get_price(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $sale_price); self::$price_overriding_scheme = $subscription_scheme; $overridden_prices = self::get_subscription_scheme_prices($prices_array, $subscription_scheme); $sale_price = $overridden_prices['sale_price']; } return $sale_price; }
/** * Adds subscription price string info to products with attached subscription schemes. * * @param string $price * @param WC_Product $product * @return string */ public static function filter_price_html($price, $product) { if (self::$bypass_price_html_filter) { return $price; } if ($product->get_price() === '') { return $price; } $product_level_schemes = WCS_ATT_Schemes::get_product_subscription_schemes($product); if (!empty($product_level_schemes)) { $has_variable_price = false; $subscription_scheme = current($product_level_schemes); if ($price_overrides_exist = WCS_ATT_Scheme_Prices::subscription_price_overrides_exist($product_level_schemes)) { $lowest_scheme_price_data = WCS_ATT_Scheme_Prices::get_lowest_price_subscription_scheme_data($product, $product_level_schemes); $subscription_scheme = $lowest_scheme_price_data['scheme']; } // Reinstantiate variable products to re-populate a filtered version of the 'prices_array' property. Otherwise, a clone should do... but re-instantiate just in case. $_product = wc_get_product($product->id); // ...and let this be filterable. $_product = apply_filters('wcsatt_overridden_subscription_prices_product', $_product, $subscription_scheme, $product); $_product->is_converted_to_sub = 'yes'; $_product->subscription_period = isset($subscription_scheme['subscription_period']) ? $subscription_scheme['subscription_period'] : ''; $_product->subscription_period_interval = isset($subscription_scheme['subscription_period_interval']) ? $subscription_scheme['subscription_period_interval'] : ''; $_product->subscription_length = isset($subscription_scheme['subscription_length']) ? $subscription_scheme['subscription_length'] : ''; // Add price method filters. WCS_ATT_Scheme_Prices::add_price_filters($_product, $subscription_scheme); if (count($product_level_schemes) > 1) { $has_variable_price = true; } else { if ('variable' === $product->product_type && $_product->get_variation_price('min') !== $_product->get_variation_price('max')) { $has_variable_price = true; // If all variations prices are overridden, they will be equal. if (isset($subscription_scheme['subscription_pricing_method']) && $subscription_scheme['subscription_pricing_method'] === 'override') { $has_variable_price = false; } } elseif ('bundle' === $product->product_type && $product->get_bundle_price('min') !== $product->get_bundle_price('max')) { $has_variable_price = true; } elseif ('composite' === $product->product_type && $product->get_composite_price('min') !== $product->get_composite_price('max')) { $has_variable_price = true; } } $force_subscription = get_post_meta($product->id, '_wcsatt_force_subscription', true); if ($force_subscription === 'yes') { self::$bypass_price_html_filter = true; $price = $_product->get_price_html(); self::$bypass_price_html_filter = false; $price = WC_Subscriptions_Product::get_price_string($_product, array('price' => $price)); if ($has_variable_price && false === strpos($price, $_product->get_price_html_from_text())) { $price = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), $_product->get_price_html_from_text(), $price); } } else { $suffix_price_html = ''; // Discount format vs Price format. Experimental use only. if (apply_filters('wcsatt_price_html_discount_format', false, $product) && WCS_ATT_Scheme_Prices::has_subscription_price_override($subscription_scheme) && $subscription_scheme['subscription_pricing_method'] === 'inherit') { $discount = $subscription_scheme['subscription_discount']; $discount_html = '</small> <span class="wcsatt-sub-discount">' . sprintf(__('%s% off', WCS_ATT::TEXT_DOMAIN), $discount) . '</span><small>'; $suffix_price_html = sprintf(__('at%1$s%2$s', WCS_ATT::TEXT_DOMAIN), $has_variable_price ? __(' up to', WCS_ATT::TEXT_DOMAIN) : '', $discount_html); } else { self::$bypass_price_html_filter = true; $lowest_scheme_price_html = $_product->get_price_html(); self::$bypass_price_html_filter = false; $lowest_scheme_price_data = isset($lowest_scheme_price_data) ? $lowest_scheme_price_data : array(); $lowest_scheme_price_html = WC_Subscriptions_Product::get_price_string($_product, apply_filters('wcsatt_get_single_product_lowest_price_string', array('price' => $lowest_scheme_price_html), $lowest_scheme_price_data)); if ($has_variable_price) { $suffix_price_html = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), _x('<span class="from">from </span>', 'subscribe from price', WCS_ATT::TEXT_DOMAIN), str_replace($_product->get_price_html_from_text(), '', $lowest_scheme_price_html)); } else { $suffix_price_html = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), _x('<span class="for">for </span>', 'subscribe for price', WCS_ATT::TEXT_DOMAIN), $lowest_scheme_price_html); } } if ($price_overrides_exist) { $suffix = ' <small class="wcsatt-sub-options">' . sprintf(_n('– or subscribe %s', '– subscription plans available %s', count($product_level_schemes), WCS_ATT::TEXT_DOMAIN), $suffix_price_html) . '</small>'; } else { $suffix = ' <small class="wcsatt-sub-options">' . sprintf(_n('– subscription available', '– subscription plans available', count($product_level_schemes), WCS_ATT::TEXT_DOMAIN), $suffix_price_html) . '</small>'; } $price = sprintf(_x('%1$s%2$s', 'price html sub options suffix', WCS_ATT::TEXT_DOMAIN), $price, $suffix); } WCS_ATT_Scheme_Prices::remove_price_filters(); } return $price; }