From 22d18f69772ef6318571c5449efca53a4b6062b3 Mon Sep 17 00:00:00 2001 From: Marcin Pietrzak Date: Fri, 13 Dec 2024 12:37:37 +0100 Subject: [PATCH] single product display --- .../class-iworks-omnibus-integration.php | 25 -- ...nibus-integration-woocommerce-settings.php | 24 -- ...iworks-omnibus-integration-woocommerce.php | 391 +++--------------- .../v4/class-iworks-omnibus-integration.php | 159 ++++--- readme.txt | 4 +- 5 files changed, 176 insertions(+), 427 deletions(-) diff --git a/includes/iworks/omnibus/class-iworks-omnibus-integration.php b/includes/iworks/omnibus/class-iworks-omnibus-integration.php index 1ced36e..c9216d0 100644 --- a/includes/iworks/omnibus/class-iworks-omnibus-integration.php +++ b/includes/iworks/omnibus/class-iworks-omnibus-integration.php @@ -383,31 +383,6 @@ protected function add_message( $price, $price_lowest, $format_price_callback = if ( isset( $price_lowest['price_sale'] ) ) { $price_to_show = $price_lowest['price_sale']; } - /** - * WooCommerce: include tax - */ - if ( 'no' === get_option( 'woocommerce_prices_include_tax' ) ) { - if ( 'yes' === get_option( $this->get_name( 'include_tax' ), 'yes' ) ) { - if ( - isset( $price_lowest['price_including_tax'] ) - && $price_lowest['price_including_tax'] > $price_to_show - ) { - $price_to_show = $price_lowest['price_including_tax']; - } else { - global $product; - if ( is_object( $product ) ) { - $tax = new WC_Tax(); - if ( ! empty( $tax ) ) { - $taxes = $tax->get_rates( $product->get_tax_class() ); - if ( ! empty( $taxes ) ) { - $t = array_shift( $taxes ); - $price_to_show = ( 100 + $t['rate'] ) * $price_to_show / 100; - } - } - } - } - } - } if ( is_callable( $format_price_callback ) ) { $price_to_show = $format_price_callback( $price_to_show ); } diff --git a/includes/iworks/omnibus/integration/class-iworks-omnibus-integration-woocommerce-settings.php b/includes/iworks/omnibus/integration/class-iworks-omnibus-integration-woocommerce-settings.php index da0e27a..0c2f081 100644 --- a/includes/iworks/omnibus/integration/class-iworks-omnibus-integration-woocommerce-settings.php +++ b/includes/iworks/omnibus/integration/class-iworks-omnibus-integration-woocommerce-settings.php @@ -112,30 +112,6 @@ protected function get_settings_for_default_section() { 'id' => $this->get_name( 'sectionend' ), ), ); - /** - * WooCommerce Tax - */ - if ( - 'yes' === get_option( 'woocommerce_calc_taxes', 'no' ) - && 'no' === get_option( 'woocommerce_prices_include_tax', 'no' ) - ) { - $settings[] = array( - 'title' => __( 'Tax', 'omnibus' ), - 'type' => 'title', - 'id' => $this->get_name( 'tax' ), - ); - $settings[] = array( - 'title' => __( 'Include tax', 'omnibus' ), - 'id' => $this->get_name( 'include_tax' ), - 'default' => 'yes', - 'type' => 'checkbox', - 'desc' => __( 'Display price with tax', 'omnibus' ), - ); - $settings[] = array( - 'type' => 'sectionend', - 'id' => $this->get_name( 'tax-sectionend' ), - ); - } /** * Products */ diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php index 66663d2..3615855 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php @@ -1,7 +1,7 @@ get_type() ) { + case 'simple': + $this->maybe_add_price_log( $product ); + break; + case 'variable': + foreach ( $product->get_available_variations() as $variation ) { + $variant = wc_get_product( $variation['variation_id'] ); + $this->maybe_add_price_log( $variant ); + } + break; + default: + l( $product->get_type() ); + } + } + /** * Maybe add price log * @@ -259,7 +268,8 @@ public function action_woocommerce_product_options_pricing() { if ( ! $this->should_it_show_up( $post_id ) ) { return; } - $price_lowest = $this->woocommerce_get_lowest_price_in_history( $post_id ); + $product = wc_get_product( $post_id ); + $price_lowest = $this->get_lowest_price_by_post_id( $post_id, $product->get_sale_price() ); $this->print_header( 'description' ); $this->woocommerce_wp_text_input_price( $price_lowest ); $this->woocommerce_wp_text_input_date( $price_lowest ); @@ -276,7 +286,8 @@ public function action_woocommerce_variation_options_pricing( $loop, $variation_ if ( ! $this->should_it_show_up( $post_id ) ) { return; } - $price_lowest = $this->woocommerce_get_lowest_price_in_history( $post_id ); + $product = wc_get_product( $post_id ); + $price_lowest = $this->get_lowest_price_by_post_id( $post_id, $product->get_sale_price() ); $this->print_header( 'form-row form-row-full' ); $configuration = array( 'wrapper_class' => 'form-row form-row-first', @@ -294,18 +305,10 @@ public function action_woocommerce_variation_options_pricing( $loop, $variation_ $this->woocommerce_wp_checkbox_short( $post_id, $configuration, $loop ); } - public function maybe_add_text( $state ) { - if ( $state && 'inform' === get_option( $this->get_name( 'missing' ) ) ) { - return $this->get_message_text( 'no_data' ); - } - return $state; - } - /** * helper to decide show it or no */ private function should_it_show_up( $post_id ) { - add_filter( 'iworks_omnibus_show', array( $this, 'maybe_add_text' ) ); /** * for admin */ @@ -375,11 +378,14 @@ private function should_it_show_up( $post_id ) { * variation */ $product = wc_get_product( $post_id ); - if ( 'variation' === $product->get_type() ) { - if ( 'no' === get_option( $this->get_name( 'variation' ), 'yes' ) ) { + switch ( $product->get_type() ) { + case 'variable'; return apply_filters( 'iworks_omnibus_show', false ); - } - return apply_filters( 'iworks_omnibus_show', true ); + case 'variation': + if ( 'no' === get_option( $this->get_name( 'variation' ), 'yes' ) ) { + return apply_filters( 'iworks_omnibus_show', false ); + } + return apply_filters( 'iworks_omnibus_show', true ); } } if ( 'yes' === get_option( $this->get_name( 'single' ), 'yes' ) ) { @@ -437,56 +443,7 @@ public function filter_woocommerce_get_price_html( $price, $product ) { if ( is_string( $should_it_show_up ) ) { return $price . $should_it_show_up; } - $price_lowest = $this->get_lowest_price( $product ); - if ( empty( $price_lowest ) ) { - return $price; - } - return $this->add_message( $price, $price_lowest, 'wc_price' ); - } - - /** - * get lowest price - */ - private function get_lowest_price( $product ) { - /** - * get price - * - * @since 2.0.2 - */ - $price = $this->get_price( $product ); - if ( empty( $price ) ) { - return; - } - $product_type = $product->get_type(); - switch ( $product_type ) { - case 'variable': - $price_lowest = $this->woocommerce_get_price_html_for_variable( $price, $product ); - return apply_filters( 'iworks_omnibus_integration_woocommerce_price_lowest', $price_lowest, $product ); - case 'variation': - break; - default: - if ( - get_post_type() === $product_type - || get_post_type() === 'product' - ) { - if ( - 'no' === get_option( $this->get_name( $product_type ), 'yes' ) - ) { - return $price; - } - } else { - if ( 'courses' === get_post_type() ) { - if ( - defined( 'TUTOR_VERSION' ) - && 'no' === get_option( $this->get_name( 'tutor' ), 'yes' ) - ) { - return $price; - } - } - } - } - $price_lowest = $this->woocommerce_get_lowest_price_in_history( $product->get_id() ); - return apply_filters( 'iworks_omnibus_integration_woocommerce_price_lowest', $price_lowest, $product ); + return $this->add_message_helper( $price, $product ); } /** @@ -508,75 +465,6 @@ public function filter_woocommerce_get_sections_products( $sections ) { return $sections; } - /** - * WooCommerce: get lowest price in history - * - * @since 1.0.0 - */ - private function woocommerce_get_lowest_price_in_history( $post_id ) { - $product = wc_get_product( $post_id ); - /** - * check is object - * - * @since 1.2.1 - */ - if ( ! is_object( $product ) ) { - return; - } - /** - * get price - * - * @since 2.0.2 - */ - $price = $this->get_price( $product ); - $lowest = $this->_get_lowest_price_in_history( $price, $post_id ); - if ( is_admin() ) { - if ( empty( $lowest ) ) { - $value = get_post_meta( $post_id, $this->get_name() ); - if ( empty( $value ) ) { - return $lowest; - } - } - } - switch ( get_option( $this->get_name( 'missing' ), 'current' ) ) { - /** - * Regular price when it was no lower in history - * - * @since 2.5.0 - */ - case 'regular': - if ( empty( $lowest ) ) { - $lowest = array( - 'price' => $product->get_regular_price(), - 'qty' => 1, - 'timestamp' => time(), - ); - $lowest['price_including_tax'] = wc_get_price_including_tax( $product, $lowest ); - } - break; - case 'current': - if ( empty( $lowest ) ) { - $lowest = array( - 'price' => $price, - 'timestamp' => time(), - ); - } - if ( isset( $lowest['price'] ) ) { - $lowest['qty'] = 1; - $lowest['price_including_tax'] = wc_get_price_including_tax( $product, $lowest ); - } - break; - } - if ( is_array( $lowest ) ) { - $lowest['product_id'] = $product->get_ID(); - if ( $product->is_type( 'variation' ) ) { - $lowest['variation_id'] = $lowest['product_id']; - $lowest['product_id'] = $product->get_parent_id(); - } - } - return $lowest; - } - /** * WooCommerce: get price HTML for variable product * @@ -586,9 +474,9 @@ private function woocommerce_get_price_html_for_variable( $price, $product ) { if ( 'no' === get_option( $this->get_name( 'variable' ), 'yes' ) ) { return $price; } - $price_lowest = $this->woocommerce_get_lowest_price_in_history( $product->get_ID() ); + $price_lowest = $this->get_lowest_price_by_post_id( $product->get_id(), $product->get_sale_price() ); foreach ( $product->get_available_variations() as $variable ) { - $o = $this->woocommerce_get_lowest_price_in_history( $variable['variation_id'] ); + $o = $this->get_lowest_price_by_post_id( $variable['variation_id'] ); if ( empty( $o ) ) { continue; } @@ -613,7 +501,9 @@ private function woocommerce_get_price_html_for_variable( $price, $product ) { */ private function woocommerce_wp_text_input_price( $price_lowest, $configuration = array() ) { $value = __( 'no data', 'omnibus' ); - if ( ! empty( $price_lowest ) + if ( + ! is_wp_error( $price_lowest ) + && is_array( $price_lowest ) && isset( $price_lowest['price_sale'] ) && ! empty( $price_lowest['price_sale'] ) ) { @@ -650,7 +540,7 @@ private function woocommerce_wp_text_input_date( $price_lowest, $configuration = array( 'id' => $this->meta_name . '_date', 'custom_attributes' => array( 'disabled' => 'disabled' ), - 'value' => empty( $price_lowest ) ? + 'value' => is_wp_error( $price_lowest ) ? esc_html__( 'no data', 'omnibus' ) : date_i18n( get_option( 'date_format' ), isset( $price_lowest['timestamp'] ) ? $price_lowest['timestamp'] : '' ), 'data_type' => 'text', @@ -714,11 +604,7 @@ public function run( $context = 'view', $post_id = null, $message = null ) { if ( empty( $product ) ) { return; } - $price_lowest = $this->get_lowest_price( $product ); - if ( empty( $price_lowest ) ) { - return; - } - $message = $this->add_message( '', $price_lowest, 'wc_price', $message ); + $message = $this->add_message_helper( '', $product, $message ); if ( 'return' === $context ) { return $message; } @@ -798,22 +684,6 @@ public function shortcode( $atts, $content = '' ) { return $content; } - /** - * get price helper - * - * @since 2.0.2 - */ - private function get_price( $product ) { - switch ( $product->get_type() ) { - case 'simple': - $price = $this->_get_v4_lowest_price_in_history( $product->get_id() ); - break; - default: - l( $product->get_type() ); - } - } - - /** * Add configuration link to plugin_row_meta. * @@ -845,18 +715,13 @@ public function filter_add_link_omnibus_configuration( $actions, $plugin_file, $ * * @since 2.1.5 */ - public function filter_woocommerce_cart_item_price( $price, $cart_item, $cart_item_key ) { + public function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) { if ( 'yes' === get_option( $this->get_name( 'on_sale' ), 'yes' ) ) { if ( ! $cart_item['data']->is_on_sale() ) { - return $price; + return $price_html; } } - $price_lowest = $this->get_lowest_price( $cart_item['data'] ); - l( $price_lowest ); - if ( empty( $price_lowest ) ) { - return $price; - } - return $this->add_message( $price, $price_lowest, 'wc_price' ); + return $this->add_message_helper( $price_html, $product ); } /** @@ -878,42 +743,18 @@ public function action_check_and_add_message() { } /** - * Reviews Settings + * Add settings * - * @since 2.3.0 - */ - private function settings_review() { - if ( 'yes' !== get_option( 'woocommerce_enable_reviews', 'yes' ) ) { - return array(); - } - $settings = array( - array( - 'title' => __( 'Reviews', 'omnibus' ), - 'type' => 'title', - 'id' => $this->get_name( 'reviews' ), - ), - array( - 'type' => 'sectionend', - 'id' => $this->get_name( 'reviews_sectionend' ), - ), - ); - return $settings; - } - - /** - * maybe save product price */ - public function action_shutdown_maybe_save_product_price() { - if ( ! is_singular( 'product' ) ) { - return; - } - } - public function filter_woocommerce_get_settings_pages( $settings ) { $settings[] = include __DIR__ . '/class-iworks-omnibus-integration-woocommerce-settings.php'; return $settings; } + /** + * add data on admin list + * + */ public function filter_iworks_omnibus_message_template_for_admin_list( $template ) { if ( ! is_admin() ) { return $template; @@ -1062,7 +903,6 @@ public function action_woocommerce_save_maybe_save_short( $product ) { $this->update_post_meta_short( $id, filter_input( INPUT_POST, $meta_key ) ); } - /** * get WooCommerce product lowest price * @@ -1080,117 +920,6 @@ public function filter_wc_get_lowest_price( $price = array(), $product_id = null return $this->get_lowest_price( $product ); } - /** - * add extra data to price log - * - * @since 2.3.2 - * - */ - public function filter_add_price_log_data( $data, $product_id ) { - $product = wc_get_product( $product_id ); - if ( empty( $product ) ) { - return $data; - } - if ( ! is_array( $data ) ) { - $data = array(); - } - return wp_parse_args( - $this->get_prices_by_product( $product ), - $data - ); - } - - /** - * get product prices - * - * @since 3.0.0 - */ - private function get_prices_by_product( $product ) { - return array( - 'price_regular' => $product->get_regular_price(), - 'price_sale' => $product->get_sale_price(), - ); - } - - /** - * WooCommerce Omnibus Price History Meta Box - * - * @since 2.4.0 - */ - public function action_add_meta_boxes_product_history( $post ) { - if ( true ) { - return; - } - if ( ! $post instanceof WP_Post ) { - return; - } - add_meta_box( - 'iworks-omnibus-history', - __( 'Omnibus Price History', 'omnibus' ), - array( $this, 'meta_box_history_html' ) - ); - } - - public function meta_box_history_html( $post ) { - $products = $this->get_products_ids( $post ); - $log = array(); - foreach ( $products as $post_id ) { - $changes = get_post_meta( $post_id, $this->meta_price_log_name ); - if ( is_array( $changes ) ) { - foreach ( $changes as $one ) { - $one['post_id'] = $post_id; - $log[] = $one; - } - } - } - if ( empty( $log ) ) { - esc_html_e( 'There is no price history recorded.', 'omnibus' ); - return; - } - echo ''; - echo ''; - echo ''; - printf( '', esc_html__( 'Product', 'omnibus' ) ); - printf( '', esc_html__( 'Regular Price', 'omnibus' ) ); - printf( '', esc_html__( 'Sale Price', 'omnibus' ) ); - printf( '', esc_html__( 'Date', 'omnibus' ) ); - echo ''; - echo ''; - - echo ''; - usort( $log, array( $this, 'usort_log_array' ) ); - foreach ( $log as $one ) { - echo ''; - printf( '', get_the_title( $one['post_id'] ) ); - printf( '', $one['price'] ); - printf( '', empty( $one['price_sale'] ) ? '—' : $one['price_sale'] ); - printf( '', date_i18n( 'Y-m-d H:i', $one['timestamp'] ) ); - echo ''; - - d( $one ); - } - echo ''; - echo '
%s%s%s%s
%s%s%s%s
'; - } - - /** - * Get products list - * - * @param \WP_Post $post - * - * @return array a - */ - public function get_products_ids( $post ) { - $product = wc_get_product( $post->ID ); - if ( ! $product instanceof WC_Product ) { - return array(); - } - if ( $product instanceof WC_Product_Variable ) { - return $product->get_children(); - } - return array( $post->ID ); - } - /** * action to call save_price_history() * @@ -1257,4 +986,20 @@ private function delete_settings() { $wpdb->query( $query ); } + /** + * helper function to parent->add_message() + * + * @since 4.0.0 + */ + private function add_message_helper( $price_html, $product, $message = null ) { + $price_lowest = $this->get_lowest_price_by_post_id( $product->get_id(), $product->get_sale_price() ); + return $this->add_message( + $price_html, + $product->get_regular_price(), + $product->get_sale_price(), + $price_lowest, + 'wc_price', + $message + ); + } } diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php index 9e15e69..6821a83 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php @@ -72,7 +72,6 @@ private function add_log( $data ) { ) { $data['price_sale_from'] = date( $this->mysql_data_format ); } - l( $data ); $wpdb->insert( $wpdb->iworks_omnibus, $data, @@ -236,8 +235,25 @@ public function filter_get_log_array( $log, $id ) { return apply_filters( 'iworks/omnibus/logger/v4/get/log/array', array(), $id ); } - protected function _get_v4_lowest_price_in_history( $post_id ) { - return new WP_Error( 'no_price', __( 'There is no price data in history.', 'omnibus' ) ); + protected function get_lowest_price_by_post_id( $post_id, $sale_price ) { + global $wpdb; + $query = $wpdb->prepare( + "select * from $wpdb->iworks_omnibus where + post_id = %d + and price_sale <> %f + and price_sale_from <= %s + and price_sale_from > %s + order by price_sale asc limit 1", + $post_id, + $sale_price, + date( $this->mysql_data_format ), + date( $this->mysql_data_format, strtotime( sprintf( '-%d days', $this->get_days() ) ) ) + ); + $data = $wpdb->get_row( $query, ARRAY_A ); + if ( empty( $data ) ) { + return new WP_Error( 'no_price', __( 'There is no price data in history.', 'omnibus' ) ); + } + return $data; } /** @@ -245,27 +261,53 @@ protected function _get_v4_lowest_price_in_history( $post_id ) { * * @since 4.0.0 * - * @param $price + * @param $price_html + * @param $price_sale * @param $price_lowest * @param callback $format_price_callback Price format callback function. - * @param string $message Message template. */ - protected function add_message( $price, $price_lowest, $format_price_callback = null, $message = null ) { + protected function add_message( $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback = null, $message = null ) { + if ( is_wp_error( $price_lowest ) ) { + $missing_price_messsage_status = get_option( $this->get_name( 'missing' ) ); + switch ( $missing_price_messsage_status ) { + case 'inform': + case 'current': + case 'regular': + return $this->add_message_runner( + $price_html, + $price_regular, + $price_sale, + $price_lowest, + $format_price_callback, + $this->get_message_text( $missing_price_messsage_status, $price_regular, $price_sale, $format_price_callback ) + ); + default: + l( get_option( $this->get_name( 'missing' ) ) ); + } + return $price_html; + } if ( ! is_array( $price_lowest ) ) { - return $price; + return $price_html; } - if ( ! isset( $price_lowest['price'] ) ) { - return $price; + if ( ! isset( $price_lowest['price_sale'] ) ) { + return $price_html; } - if ( empty( $price_lowest['price'] ) ) { - return $price; + if ( empty( $price_lowest['price_sale'] ) ) { + return $price_html; } + /** + * run + */ + return $this->add_message_runner( $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback, $message ); + } + + private function add_message_runner( $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback, $message ) { /** * Set message template if it is needed */ if ( empty( $message ) ) { - /* translators: %2$s: rich html price */ - $message = __( 'Previous lowest price was %2$s.', 'omnibus' ); + /* translators: do not translate placeholders in braces */ + $message = __( '{price} Lowest price from {days} days before the discount.', 'omnibus' ); if ( 'custom' === get_option( $this->get_name( 'message_settings' ), 'no' ) || 'yes' === get_option( $this->get_name( 'message_settings' ), 'no' ) @@ -282,42 +324,20 @@ protected function add_message( $price, $price_lowest, $format_price_callback = * * @since 2.3.0 */ - $message = apply_filters( 'iworks_omnibus_message_template', $message, $price, $price_lowest ); + $message = apply_filters( 'iworks_omnibus_message_template', $message, $price_html, $price_sale, $price_lowest ); if ( empty( $message ) ) { - return $price; + return $price_html; } /** - * price to show + * handle no price but message */ - $price_to_show = $price_lowest['price']; - if ( isset( $price_lowest['price_sale'] ) ) { - $price_to_show = $price_lowest['price_sale']; + if ( is_wp_error( $price_lowest ) ) { + return $price_html . $message; } /** - * WooCommerce: include tax + * price to show */ - if ( 'no' === get_option( 'woocommerce_prices_include_tax' ) ) { - if ( 'yes' === get_option( $this->get_name( 'include_tax' ), 'yes' ) ) { - if ( - isset( $price_lowest['price_including_tax'] ) - && $price_lowest['price_including_tax'] > $price_to_show - ) { - $price_to_show = $price_lowest['price_including_tax']; - } else { - global $product; - if ( is_object( $product ) ) { - $tax = new WC_Tax(); - if ( ! empty( $tax ) ) { - $taxes = $tax->get_rates( $product->get_tax_class() ); - if ( ! empty( $taxes ) ) { - $t = array_shift( $taxes ); - $price_to_show = ( 100 + $t['rate'] ) * $price_to_show / 100; - } - } - } - } - } - } + $price_to_show = $price_lowest['price_sale']; if ( is_callable( $format_price_callback ) ) { $price_to_show = $format_price_callback( $price_to_show ); } @@ -338,7 +358,7 @@ protected function add_message( $price, $price_lowest, $format_price_callback = esc_attr( $attribute_value ) ); } - $price .= apply_filters( + $price_html .= apply_filters( 'iworks_omnibus_message', sprintf( '

%s

', @@ -355,16 +375,16 @@ protected function add_message( $price, $price_lowest, $format_price_callback = * * @since 2.1.7 */ - $price = preg_replace( '/{days}/', $this->get_days(), $price ); - $price = preg_replace( '/{price}/', $price_to_show, $price ); + $price_html = $this->replace_options_based_placeholders( $price_html ); + $price_html = preg_replace( '/{price}/', $price_to_show, $price_html ); if ( isset( $price_lowest['timestamp'] ) ) { - $price = preg_replace( '/{timestamp}/', $price_lowest['timestamp'], $price ); - $price = preg_replace( '/{when}/', date_i18n( get_option( 'date_format' ), $price_lowest['timestamp'] ), $price ); + $price_html = preg_replace( '/{timestamp}/', $price_lowest['timestamp'], $price_html ); + $price_html = preg_replace( '/{when}/', date_i18n( get_option( 'date_format' ), $price_lowest['timestamp'] ), $price_html ); } /** * use filter `iworks_omnibus_message_html` */ - $message = apply_filters( 'iworks_omnibus_message_html', $price, $price_lowest ); + $message = apply_filters( 'iworks_omnibus_message_html', $price_html, $price_lowest ); /** * use filter `orphan_replace` * @@ -377,17 +397,34 @@ protected function add_message( $price, $price_lowest, $format_price_callback = return $message; } - - protected function get_message_text( $type ) { + protected function get_message_text( $type, $price_regular = null, $price_sale = null, $format_price_callback = null ) { + $message = ''; switch ( $type ) { case 'no_data': - $message = esc_html__( 'No Previous Price', 'omnibus' ); + case 'inform': + $message = esc_html__( 'There is no recorded price from {days} days before the discount.', 'omnibus' ); if ( 'yes' === get_option( $this->get_name( 'message_settings' ) ) ) { $message = get_option( $this->get_name( 'message_no_data' ) ); } - return $this->message_wrapper( $message ); + break; + case 'current': + case 'regular': + $message = __( '{price} Lowest price from {days} days before the discount.', 'omnibus' ); + if ( 'yes' === get_option( $this->get_name( 'message_settings' ) ) ) { + $message = get_option( $this->get_name( 'message' ) ); + } + $price_to_show = 'regular' === $type ? $price_regular : $price_sale; + if ( is_callable( $format_price_callback ) ) { + $price_to_show = $format_price_callback( $price_to_show ); + } + $message = preg_replace( '/{price}/', $price_to_show, $message ); + break; } - return ''; + if ( ! empty( $message ) ) { + $message = $this->replace_options_based_placeholders( $message ); + $message = $this->message_wrapper( $message ); + } + return $message; } /** @@ -439,7 +476,10 @@ protected function maybe_add_last_saved_prices( $data ) { protected function get_last_saved_prices_by_id( $post_id ) { global $wpdb; $query = $wpdb->prepare( - "select * from $wpdb->iworks_omnibus where post_id = %d and price_sale_from <= %s order by price_sale_from desc limit 1", + "select * from $wpdb->iworks_omnibus where + post_id = %d + and price_sale_from <= %s + order by price_sale_from desc limit 1", $post_id, date( $this->mysql_data_format ) ); @@ -459,5 +499,16 @@ protected function get_last_saved_prices_by_id( $post_id ) { public function _get_lowest_price_in_history( $post_id ) { } + + /** + * replace options based placeholder in a message + * + * @since 4.0.0 + */ + private function replace_options_based_placeholders( $message ) { + $message = preg_replace( '/{days}/', $this->get_days(), $message ); + + return $message; + } } diff --git a/readme.txt b/readme.txt index 5850c4c..6734a36 100644 --- a/readme.txt +++ b/readme.txt @@ -193,10 +193,12 @@ The minimum WooCommerce version required is 5.5.0. Project maintained on github at [iworks/omnibus](PLUGIN_GITHUB_WEBSITE). = 4.0.0 (2024-xx-xx) = -* The [iWorks Rate](https://github.com/iworks/iworks-rate) module has been updated to 2.2.0. +* The [iWorks Rate](https://github.com/iworks/iworks-rate) module has been updated to 2.2.1. * Display Omnibus with or without tax has been removed. * The `load_plugin_textdomain()` function call has been fixed. * The ability to auto clear older prices has been added. [#23](https://github.com/iworks/omnibus/issues/23) +* A dedicated price log table has been added to increase performance and stability. +* Removed the option to show the omnibus price for the variant product. = 3.0.3 (2024-08-09) = * The omnibus price for variant in admin panel has been fixed.