/** * 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; }
/** * Add 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; } $subscription_schemes = WCS_ATT_Schemes::get_product_subscription_schemes($product); $has_product_level_schemes = empty($subscription_schemes) ? false : true; if ($has_product_level_schemes) { $force_subscription = get_post_meta($product->id, '_wcsatt_force_subscription', true); if ($force_subscription === 'yes') { $subscription_scheme = current($subscription_schemes); $overridden_prices = WCS_ATT_Schemes::get_subscription_scheme_prices($product, $subscription_scheme); $suffix = ''; $_cloned = clone $product; $_cloned->is_converted_to_sub = 'yes'; $_cloned->subscription_period = $subscription_scheme['subscription_period']; $_cloned->subscription_period_interval = $subscription_scheme['subscription_period_interval']; $_cloned->subscription_length = $subscription_scheme['subscription_length']; if (!empty($overridden_prices)) { $_cloned->regular_price = $overridden_prices['regular_price']; $_cloned->price = $overridden_prices['price']; $_cloned->sale_price = $overridden_prices['sale_price']; $_cloned->subscription_price = $overridden_prices['price']; } self::$bypass_price_html_filter = true; $price = $_cloned->get_price_html(); self::$bypass_price_html_filter = false; $price = WC_Subscriptions_Product::get_price_string($_cloned, array('price' => $price)); if (count($subscription_schemes) > 1 && false === strpos($price, $_cloned->get_price_html_from_text())) { $price = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), $_cloned->get_price_html_from_text(), $price); } } else { $price_overrides_exist = WCS_ATT_Schemes::subscription_price_overrides_exist($subscription_schemes); $lowest_scheme_price = $product->price; $lowest_scheme_sale_price = $product->sale_price; $lowest_scheme_regular_price = $product->regular_price; $lowest_scheme_price_html = ''; $from_price = ''; if ($price_overrides_exist) { foreach ($subscription_schemes as $subscription_scheme) { $overridden_prices = WCS_ATT_Schemes::get_subscription_scheme_prices($product, $subscription_scheme); if (!empty($overridden_prices)) { if ($overridden_prices['price'] < $lowest_scheme_price) { $lowest_scheme_price = $overridden_prices['price']; $lowest_scheme_sale_price = $overridden_prices['sale_price']; $lowest_scheme_regular_price = $overridden_prices['regular_price']; } } } if ($lowest_scheme_price < $product->price) { $_cloned = clone $product; $_cloned->is_converted_to_sub = 'yes'; $_cloned->subscription_period = $subscription_scheme['subscription_period']; $_cloned->subscription_period_interval = $subscription_scheme['subscription_period_interval']; $_cloned->subscription_length = $subscription_scheme['subscription_length']; $_cloned->price = $lowest_scheme_price; $_cloned->sale_price = $lowest_scheme_price; $_cloned->regular_price = $lowest_scheme_regular_price; self::$bypass_price_html_filter = true; $lowest_scheme_price_html = $_cloned->get_price_html(); $lowest_scheme_price_html = WC_Subscriptions_Product::get_price_string($_cloned, array('price' => $lowest_scheme_price_html)); self::$bypass_price_html_filter = false; if (count($subscription_schemes) > 1) { $from_price = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), _x('<span class="from">from </span>', 'min-price: 1 plan available', WCS_ATT::TEXT_DOMAIN), $lowest_scheme_price_html); } else { $from_price = sprintf(_x('%1$s%2$s', 'Price range: from', WCS_ATT::TEXT_DOMAIN), _x('<span class="for">for </span>', 'min-price: multiple plans available', WCS_ATT::TEXT_DOMAIN), $lowest_scheme_price_html); } } } if ($price_overrides_exist) { $suffix = ' <small class="wcsatt-sub-options">' . sprintf(_n('– or subscribe %s', '– or subscribe %s', count($subscription_schemes), WCS_ATT::TEXT_DOMAIN), $from_price) . '</small>'; } else { $suffix = ' <small class="wcsatt-sub-options">' . sprintf(_n('– subscription plan available', '– subscription plans available', count($subscription_schemes), WCS_ATT::TEXT_DOMAIN), $from_price) . '</small>'; } $price = sprintf(__('%1$s%2$s', 'price html sub options suffix', WCS_ATT::TEXT_DOMAIN), $price, $suffix); } } return $price; }
* @return string */ public static function filter_price_html($price, $product) { $subscription_schemes = WCS_ATT_Schemes::get_product_subscription_schemes($product); $has_product_level_schemes = empty($subscription_schemes) ? false : true; if ($has_product_level_schemes) { $force_subscription = get_post_meta($product->id, '_wcsatt_force_subscription', true); if ($force_subscription === 'yes') { $subscription_scheme = current($subscription_schemes); $suffix = ''; if (count($subscription_schemes) > 1) { $suffix = ' <small class="wcsatt-sub-options">' . __('(additional subscription options available)', WCS_ATT::TEXT_DOMAIN) . '</small>'; } $_cloned = clone $product; $_cloned->is_converted_to_sub = 'yes'; $_cloned->subscription_period = $subscription_scheme['subscription_period']; $_cloned->subscription_period_interval = $subscription_scheme['subscription_period_interval']; $_cloned->subscription_length = $subscription_scheme['subscription_length']; $price = WC_Subscriptions_Product::get_price_string($_cloned, array('price' => $price)); $price = sprintf(__('%1$s%2$s', 'price html sub options suffix', WCS_ATT::TEXT_DOMAIN), $price, $suffix); } else { $suffix = ' <small class="wcsatt-sub-options">' . __('(also available as subscription)', WCS_ATT::TEXT_DOMAIN) . '</small>'; $price = sprintf(__('%1$s%2$s', 'price html sub options suffix', WCS_ATT::TEXT_DOMAIN), $price, $suffix); } } return $price; } } WCS_ATT_Display::init();