Guest User

class-wcml-custom-prices.php

a guest
Oct 30th, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.22 KB | None | 0 0
  1. <?php
  2.  
  3. use WCML\Utilities\DB;
  4. use WPML\FP\Fns;
  5. use WPML\FP\Maybe;
  6. use WPML\FP\Obj;
  7. use function WCML\functions\isStandAlone;
  8. use function WPML\FP\invoke;
  9.  
  10. class WCML_Custom_Prices {
  11.  
  12.     /** @var woocommerce_wpml */
  13.     private $woocommerce_wpml;
  14.     /** @var wpdb */
  15.     private $wpdb;
  16.  
  17.     public function __construct( woocommerce_wpml $woocommerce_wpml, wpdb $wpdb ) {
  18.         $this->woocommerce_wpml = $woocommerce_wpml;
  19.         $this->wpdb             = $wpdb;
  20.     }
  21.  
  22.     public function add_hooks() {
  23.         add_filter( 'init', [ $this, 'custom_prices_init' ] );
  24.     }
  25.  
  26.     public function custom_prices_init() {
  27.  
  28.         if ( is_admin() ) {
  29.             if ( isStandAlone() ) {
  30.                 // In the full mode, this is done in the product sync logic.
  31.                 add_action( 'save_post_product', [ $this, 'save_custom_prices' ] );
  32.                 add_action( 'save_post_product_variation', [ $this, 'sync_product_variations_custom_prices' ] );
  33.                 add_action( 'woocommerce_ajax_save_product_variations', [ $this, 'sync_product_variations_custom_prices_on_ajax' ] );
  34.             }
  35.  
  36.             add_action( 'woocommerce_variation_options', [ $this, 'add_individual_variation_nonce' ], 10, 3 );
  37.  
  38.             // custom prices for different currencies for products/variations [BACKEND].
  39.             add_action( 'woocommerce_product_options_pricing', [ $this, 'woocommerce_product_options_custom_pricing' ] );
  40.             add_action( 'woocommerce_product_after_variable_attributes', [ $this, 'woocommerce_product_after_variable_attributes_custom_pricing' ], 10, 3 );
  41.  
  42.         } else {
  43.             add_filter( 'woocommerce_product_is_on_sale', [ $this, 'filter_product_is_on_sale' ], 10, 2 );
  44.         }
  45.  
  46.         add_action( 'woocommerce_variation_is_visible', [ $this, 'filter_product_variations_with_custom_prices' ], 10, 2 );
  47.  
  48.         add_filter( 'loop_shop_post_in', [ $this, 'filter_products_with_custom_prices' ], 100 );
  49.         add_filter( 'woocommerce_is_purchasable', [ $this, 'check_product_with_custom_prices' ], 10, 2 );
  50.  
  51.         add_action( 'wc_after_products_starting_sales', [ $this, 'maybe_set_sale_prices' ] );
  52.         add_action( 'wc_after_products_ending_sales', [ $this, 'maybe_remove_sale_prices' ] );
  53.     }
  54.  
  55.     public function add_individual_variation_nonce( $loop, $variation_data, $variation ) {
  56.  
  57.         wp_nonce_field( 'wcml_save_custom_prices_variation_' . $variation->ID, '_wcml_custom_prices_variation_' . $variation->ID . '_nonce' );
  58.  
  59.     }
  60.  
  61.     /**
  62.      * @param int  $product_id
  63.      * @param bool $currency
  64.      *
  65.      * @return array|false
  66.      */
  67.     public function get_product_custom_prices( $product_id, $currency = false ) {
  68.         if ( empty( $currency ) ) {
  69.             $currency = $this->woocommerce_wpml->multi_currency->get_client_currency();
  70.         }
  71.  
  72.         if ( wcml_get_woocommerce_currency_option() === $currency ) {
  73.             return false;
  74.         }
  75.  
  76.         $cache_key           = $product_id . '_' . $currency;
  77.         $cache_group         = 'product_custom_prices';
  78.         $cache_found         = false;
  79.         $cache_custom_prices = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  80.         if ( $cache_found ) {
  81.             return $cache_custom_prices;
  82.         }
  83.  
  84.         $product_meta  = get_post_custom( $this->woocommerce_wpml->products->get_original_product_id( $product_id ) );
  85.         $custom_prices = [];
  86.  
  87.         if ( ! empty( $product_meta['_wcml_custom_prices_status'][0] ) ) {
  88.             foreach ( wcml_price_custom_fields( $product_id ) as $key ) {
  89.                 if ( isset( $product_meta[ $key . '_' . $currency ][0] ) ) {
  90.                     $custom_prices[ $key ] = $product_meta[ $key . '_' . $currency ][0];
  91.                 }
  92.             }
  93.         }
  94.  
  95.         if ( ! isset( $custom_prices['_price'] ) ) {
  96.             return false;
  97.         }
  98.  
  99.         $current__price_value = $custom_prices['_price'];
  100.  
  101.         if ( $this->is_date_range_set( $product_meta, $currency ) && ! $this->is_on_sale_date_range( $product_meta, $currency ) ) {
  102.             $custom_prices['_sale_price'] = '';
  103.             $custom_prices['_price']      = $custom_prices['_regular_price'];
  104.         }
  105.  
  106.         if ( $custom_prices['_price'] !== $current__price_value ) {
  107.             update_post_meta( $product_id, '_price_' . $currency, $custom_prices['_price'] );
  108.         }
  109.  
  110.         // detemine min/max variation prices.
  111.         if ( ! empty( $product_meta['_min_variation_price'] ) ) {
  112.  
  113.             static $product_min_max_prices = [];
  114.  
  115.             if ( ! isset( $product_min_max_prices[ $product_id ] ) && 'product' === get_post_type( $product_id ) ) {
  116.  
  117.                 $product_min_max_prices[ $product_id ] = [];
  118.  
  119.                 // get variation ids.
  120.                 $variation_ids = $this->wpdb->get_col( $this->wpdb->prepare( "SELECT ID FROM {$this->wpdb->posts} WHERE post_parent = %d", $product_id ) );
  121.  
  122.                 // get all prices for the above variations.
  123.                 $rows = $this->wpdb->get_results(
  124.                     "SELECT post_id, meta_key, meta_value FROM {$this->wpdb->postmeta}
  125.                     WHERE meta_key IN ('_price', '_regular_price', '_sale_price', '_price_$currency', '_regular_price_$currency', '_sale_price_$currency')
  126.                         AND post_id IN (" . DB::prepareIn( $variation_ids, '%d' ) . ')'
  127.                 );
  128.  
  129.                 // $extractPricesByType :: array, string => array
  130.                 $extractPricesByType = function( $rows, $key ) use ( $currency, $variation_ids ) {
  131.                     $prices   = wp_list_pluck( wp_list_filter( $rows, [ 'meta_key' => $key . '_' . $currency ] ), $key . '_' . $currency, 'post_id' );
  132.                     $defaults = wp_list_pluck( wp_list_filter( $rows, [ 'meta_key' => $key ] ), $key, 'post_id' );
  133.  
  134.                     // calculate missing prices automatically.
  135.                     foreach ( $variation_ids as $id ) {
  136.                         if ( empty( $prices[ $id ] ) && isset( $defaults[ $id ] ) ) {
  137.                             $prices[ $id ] = apply_filters( 'wcml_raw_price_amount', $defaults[ $id ] );
  138.                         }
  139.                     }
  140.  
  141.                     return $prices;
  142.                 };
  143.  
  144.                 $regular_prices = $extractPricesByType( $rows, '_regular_price' );
  145.                 $sale_prices    = $extractPricesByType( $rows, '_sale_price' );
  146.                 $prices         = $extractPricesByType( $rows, '_price' );
  147.  
  148.                 if ( ! empty( $regular_prices ) ) {
  149.                     $product_min_max_prices[ $product_id ]['_min_variation_regular_price'] = min( $regular_prices );
  150.                     $product_min_max_prices[ $product_id ]['_max_variation_regular_price'] = max( $regular_prices );
  151.                 }
  152.  
  153.                 if ( ! empty( $sale_prices ) ) {
  154.                     $product_min_max_prices[ $product_id ]['_min_variation_sale_price'] = min( $sale_prices );
  155.                     $product_min_max_prices[ $product_id ]['_max_variation_sale_price'] = max( $sale_prices );
  156.                 }
  157.  
  158.                 if ( ! empty( $prices ) ) {
  159.                     $product_min_max_prices[ $product_id ]['_min_variation_price'] = min( $prices );
  160.                     $product_min_max_prices[ $product_id ]['_max_variation_price'] = max( $prices );
  161.                 }
  162.             }
  163.  
  164.             if ( isset( $product_min_max_prices[ $product_id ]['_min_variation_regular_price'] ) ) {
  165.                 $custom_prices['_min_variation_regular_price'] = $product_min_max_prices[ $product_id ]['_min_variation_regular_price'];
  166.             }
  167.  
  168.             if ( isset( $product_min_max_prices[ $product_id ]['_max_variation_regular_price'] ) ) {
  169.                 $custom_prices['_max_variation_regular_price'] = $product_min_max_prices[ $product_id ]['_max_variation_regular_price'];
  170.             }
  171.  
  172.             if ( isset( $product_min_max_prices[ $product_id ]['_min_variation_sale_price'] ) ) {
  173.                 $custom_prices['_min_variation_sale_price'] = $product_min_max_prices[ $product_id ]['_min_variation_sale_price'];
  174.             }
  175.             if ( isset( $product_min_max_prices[ $product_id ]['_max_variation_sale_price'] ) ) {
  176.                 $custom_prices['_max_variation_sale_price'] = $product_min_max_prices[ $product_id ]['_max_variation_sale_price'];
  177.             }
  178.  
  179.             if ( isset( $product_min_max_prices[ $product_id ]['_min_variation_price'] ) ) {
  180.                 $custom_prices['_min_variation_price'] = $product_min_max_prices[ $product_id ]['_min_variation_price'];
  181.             }
  182.             if ( isset( $product_min_max_prices[ $product_id ]['_max_variation_price'] ) ) {
  183.                 $custom_prices['_max_variation_price'] = $product_min_max_prices[ $product_id ]['_max_variation_price'];
  184.             }
  185.         }
  186.  
  187.         $custom_prices = apply_filters( 'wcml_product_custom_prices', $custom_prices, $product_id, $currency );
  188.  
  189.         wp_cache_set( $cache_key, $custom_prices, $cache_group );
  190.  
  191.         return $custom_prices;
  192.     }
  193.  
  194.     private function is_date_range_set( $product_meta, $currency ) {
  195.  
  196.         $get_currency_schedule = function ( $suffix ) use ( $product_meta ) {
  197.             return Obj::path( [
  198.                     '_sale_price_dates_from' . $suffix,
  199.                     0
  200.                 ], $product_meta ) || Obj::path( [ '_sale_price_dates_to' . $suffix, 0 ], $product_meta );
  201.         };
  202.  
  203.         return $get_currency_schedule( '' ) || $get_currency_schedule( "_$currency" );
  204.     }
  205.  
  206.     private function is_on_sale_date_range( $product_meta, $currency ) {
  207.  
  208.         if (
  209.             isset( $product_meta[ '_sale_price_dates_from_' . $currency ] ) &&
  210.             isset( $product_meta[ '_sale_price_dates_to_' . $currency ] ) &&
  211.             $product_meta[ '_sale_price_dates_from_' . $currency ][0] &&
  212.             $product_meta[ '_sale_price_dates_to_' . $currency ][0]
  213.         ) {
  214.  
  215.             if (
  216.                 current_time( 'timestamp' ) > $product_meta[ '_sale_price_dates_from_' . $currency ][0] &&
  217.                 current_time( 'timestamp' ) < $product_meta[ '_sale_price_dates_to_' . $currency ][0]
  218.             ) {
  219.                 return true;
  220.             }
  221.         } elseif (
  222.             isset( $product_meta['_sale_price_dates_from'] ) &&
  223.             isset( $product_meta['_sale_price_dates_to'] ) &&
  224.             current_time( 'timestamp' ) > $product_meta['_sale_price_dates_from'][0] &&
  225.             current_time( 'timestamp' ) < $product_meta['_sale_price_dates_to'][0]
  226.         ) {
  227.             return true;
  228.         }
  229.  
  230.         return false;
  231.     }
  232.  
  233.     public function woocommerce_product_options_custom_pricing() {
  234.         global $pagenow;
  235.  
  236.         $this->load_custom_prices_js_css();
  237.  
  238.         if ( ( isset( $_GET['post'] ) && ( get_post_type( $_GET['post'] ) !== 'product' || ! $this->woocommerce_wpml->products->is_original_product( $_GET['post'] ) ) ) ||
  239.             ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'product' && isset( $_GET['source_lang'] ) ) ) {
  240.             return;
  241.         }
  242.  
  243.         $product_id = 'new';
  244.  
  245.         if ( $pagenow === 'post.php' && isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == 'product' ) {
  246.             $product_id = $_GET['post'];
  247.         }
  248.  
  249.         $this->custom_pricing_output( $product_id );
  250.  
  251.         do_action( 'wcml_after_custom_prices_block', $product_id );
  252.  
  253.         wp_nonce_field( 'wcml_save_custom_prices', '_wcml_custom_prices_nonce' );
  254.  
  255.     }
  256.  
  257.     public function woocommerce_product_after_variable_attributes_custom_pricing( $loop, $variation_data, $variation ) {
  258.  
  259.         if ( $this->woocommerce_wpml->products->is_original_product( $variation->post_parent ) ) {
  260.  
  261.             echo '<tr><td>';
  262.             $this->custom_pricing_output( $variation->ID );
  263.             echo '</td></tr>';
  264.  
  265.         }
  266.  
  267.     }
  268.  
  269.     private function load_custom_prices_js_css() {
  270.         wp_register_style( 'wpml-wcml-prices', WCML_PLUGIN_URL . '/res/css/wcml-prices.css', null, WCML_VERSION );
  271.         wp_register_script( 'wcml-tm-scripts-prices', WCML_PLUGIN_URL . '/res/js/prices' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
  272.  
  273.         wp_enqueue_style( 'wpml-wcml-prices' );
  274.         wp_enqueue_script( 'wcml-tm-scripts-prices' );
  275.     }
  276.  
  277.     private function custom_pricing_output( $post_id = false ) {
  278.  
  279.         $custom_prices_ui = new WCML_Custom_Prices_UI( $this->woocommerce_wpml, $post_id );
  280.         $custom_prices_ui->show();
  281.  
  282.     }
  283.  
  284.     public function check_product_with_custom_prices( $is_purchasable, WC_Product $product ) {
  285.  
  286.         if ( $this->is_filtering_products_with_custom_prices_enabled() && is_product() ) {
  287.  
  288.             $original_id = $this->woocommerce_wpml->products->get_original_product_id( $product->get_id() );
  289.  
  290.             if ( ! $this->is_custom_prices_set_for_product( $original_id ) ) {
  291.                 return false;
  292.             }
  293.         }
  294.  
  295.         return $is_purchasable;
  296.     }
  297.  
  298.     public function filter_product_variations_with_custom_prices( $is_visible, $variation_id ) {
  299.  
  300.         if ( $this->is_filtering_products_with_custom_prices_enabled() && is_product() ) {
  301.  
  302.             $orig_child_id = $this->woocommerce_wpml->products->get_original_product_id( $variation_id );
  303.  
  304.             if ( ! $this->is_custom_prices_set_for_product( $orig_child_id ) ) {
  305.                 return false;
  306.             }
  307.         }
  308.  
  309.         return $is_visible;
  310.     }
  311.  
  312.     // display products with custom prices only if enabled "Show only products with custom prices in secondary currencies" option on settings page.
  313.     public function filter_products_with_custom_prices( $filtered_posts ) {
  314.  
  315.         if ( $this->is_filtering_products_with_custom_prices_enabled() ) {
  316.    
  317.             $matched_products       = [];
  318.             $matched_products_query = $this->wpdb->get_results(
  319.                 "
  320.                 SELECT DISTINCT ID, post_parent, post_type FROM {$this->wpdb->posts}
  321.                 INNER JOIN {$this->wpdb->postmeta} ON ID = post_id
  322.                 WHERE post_type IN ( 'product', 'product_variation' )
  323.                 AND post_status = 'publish'
  324.                 AND meta_key = '_wcml_custom_prices_status'
  325.                 AND meta_value = 1
  326.                 ",
  327.                 OBJECT_K
  328.             );
  329.    
  330.             if ( $matched_products_query ) {
  331.    
  332.                 $client_currency = $this->woocommerce_wpml->multi_currency->get_client_currency();
  333.    
  334.                 remove_filter( 'get_post_metadata', [ $this->woocommerce_wpml->multi_currency->prices, 'product_price_filter' ], 10 );
  335.    
  336.                 foreach ( $matched_products_query as $product ) {
  337.                     if ( ! get_post_meta( $product->ID, '_price_' . $client_currency, true ) ) {
  338.                         continue;
  339.                     }
  340.                     if ( $product->post_type == 'product' ) {
  341.                         $matched_products[] = apply_filters( 'translate_object_id', $product->ID, 'product', true );
  342.                     }
  343.                     if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) ) {
  344.                         $matched_products[] = apply_filters( 'translate_object_id', $product->post_parent, get_post_type( $product->post_parent ), true );
  345.                     }
  346.                 }
  347.    
  348.                 // Now add grouped products only if **all** of their children have the custom price meta key
  349.                 $grouped_products = [];
  350.                 $all_products = wc_get_products( [
  351.                     'type' => 'grouped',
  352.                     'limit' => -1,
  353.                 ] );
  354.    
  355.                 foreach ( $all_products as $grouped_product ) {
  356.                     $children_ids = $grouped_product->get_children();
  357.                    
  358.                     // Flag to determine if all children have the custom prices
  359.                     $all_children_have_custom_prices = true;
  360.    
  361.                     foreach ( $children_ids as $child_id ) {
  362.                         if ( ! in_array( $child_id, $matched_products ) ) {
  363.                             // If one child does not have custom prices, set flag to false and break the loop
  364.                             $all_children_have_custom_prices = false;
  365.                             break;
  366.                         }
  367.                     }
  368.    
  369.                     // Add the grouped product only if all children have custom prices
  370.                     if ( $all_children_have_custom_prices ) {
  371.                         $grouped_products[] = $grouped_product->get_id();
  372.                     }
  373.                 }
  374.    
  375.                 // Merge grouped products into matched products array
  376.                 $matched_products = array_merge( $matched_products, $grouped_products );
  377.    
  378.                 add_filter( 'get_post_metadata', [ $this->woocommerce_wpml->multi_currency->prices, 'product_price_filter' ], 10, 4 );
  379.             }
  380.    
  381.             // Filter the ids.
  382.             if ( sizeof( $filtered_posts ) == 0 ) {
  383.                 $filtered_posts   = $matched_products;
  384.                 $filtered_posts[] = 0; // Avoid empty array issues
  385.             } else {
  386.                 $filtered_posts   = array_intersect( $filtered_posts, $matched_products );
  387.                 $filtered_posts[] = 0; // Avoid empty array issues
  388.             }
  389.         }
  390.    
  391.         return $filtered_posts;
  392.     }  
  393.  
  394.     /**
  395.      * @return bool
  396.      */
  397.     private function is_filtering_products_with_custom_prices_enabled() {
  398.  
  399.         return wcml_is_multi_currency_on() &&
  400.                isset( $this->woocommerce_wpml->settings['display_custom_prices'] ) &&
  401.                $this->woocommerce_wpml->settings['display_custom_prices'] &&
  402.                $this->woocommerce_wpml->multi_currency->get_client_currency() !== wcml_get_woocommerce_currency_option();
  403.     }
  404.  
  405.     public function save_custom_prices( $post_id ) {
  406.         $nonce = filter_input( INPUT_POST, '_wcml_custom_prices_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  407.  
  408.         if ( isset( $_POST['_wcml_custom_prices'] ) && isset( $nonce ) && wp_verify_nonce( $nonce, 'wcml_save_custom_prices' ) && ! $this->woocommerce_wpml->products->is_variable_product( $post_id ) ) {
  409.             if ( isset( $_POST['_wcml_custom_prices'][ $post_id ] ) || isset( $_POST['_wcml_custom_prices']['new'] ) ) {
  410.                 $wcml_custom_prices_option = isset( $_POST['_wcml_custom_prices'][ $post_id ] ) ? $_POST['_wcml_custom_prices'][ $post_id ] : $_POST['_wcml_custom_prices']['new'];
  411.             } else {
  412.                 $current_option            = get_post_meta( $post_id, '_wcml_custom_prices_status', true );
  413.                 $wcml_custom_prices_option = $current_option ? $current_option : 0;
  414.             }
  415.             update_post_meta( $post_id, '_wcml_custom_prices_status', $wcml_custom_prices_option );
  416.  
  417.             if ( $wcml_custom_prices_option == 1 ) {
  418.                 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
  419.                 foreach ( $currencies as $code => $currency ) {
  420.                     $sale_price    = wc_format_decimal( $_POST['_custom_sale_price'][ $code ] );
  421.                     $regular_price = wc_format_decimal( $_POST['_custom_regular_price'][ $code ] );
  422.                     $schedule      = $_POST['_wcml_schedule'][ $code ];
  423.                     $date_from     = $schedule && isset( $_POST['_custom_sale_price_dates_from'][ $code ] ) ? strtotime( $_POST['_custom_sale_price_dates_from'][ $code ] ) : '';
  424.                     $date_to       = $schedule && isset( $_POST['_custom_sale_price_dates_to'][ $code ] ) ? strtotime( $_POST['_custom_sale_price_dates_to'][ $code ] ) : '';
  425.  
  426.                     $custom_prices = apply_filters(
  427.                         'wcml_update_custom_prices_values',
  428.                         [
  429.                             '_regular_price'         => $regular_price,
  430.                             '_sale_price'            => $sale_price,
  431.                             '_wcml_schedule'         => $schedule,
  432.                             '_sale_price_dates_from' => $date_from,
  433.                             '_sale_price_dates_to'   => $date_to,
  434.                         ],
  435.                         $code,
  436.                         $post_id
  437.                     );
  438.                     $product_price = $this->update_custom_prices( $post_id, $custom_prices, $code );
  439.  
  440.                     do_action( 'wcml_after_save_custom_prices', $post_id, $product_price, $custom_prices, $code );
  441.                 }
  442.             }
  443.         }
  444.     }
  445.  
  446.     public function update_custom_prices( $post_id, $custom_prices, $code ) {
  447.         $price = null;
  448.  
  449.         $defaults = [
  450.             '_sale_price_dates_to'   => '',
  451.             '_sale_price_dates_from' => '',
  452.             '_sale_price'            => '',
  453.         ];
  454.  
  455.         $custom_prices = array_merge( $defaults, $custom_prices );
  456.  
  457.         foreach ( $custom_prices as $custom_price_key => $custom_price_value ) {
  458.             update_post_meta( $post_id, $custom_price_key . '_' . $code, $custom_price_value );
  459.         }
  460.  
  461.         if ( $this->is_sale_price_valid( $custom_prices ) ) {
  462.             $price = stripslashes( $custom_prices['_sale_price'] );
  463.         } else {
  464.             $price = stripslashes( $custom_prices['_regular_price'] );
  465.             $this->validate_and_update_sale_price_dates( $post_id, $code, $custom_prices );
  466.         }
  467.  
  468.         update_post_meta( $post_id, '_price_' . $code, $price ? $price : null );
  469.  
  470.         return $price;
  471.     }
  472.  
  473.     /**
  474.      * @param int    $post_id
  475.      * @param string $code
  476.      * @param array  $custom_prices
  477.      */
  478.     private function validate_and_update_sale_price_dates( $post_id, $code, array $custom_prices ) {
  479.         $date_from = $custom_prices['_sale_price_dates_from'];
  480.         $date_to   = $custom_prices['_sale_price_dates_to'];
  481.         if ( $date_to ) {
  482.             if ( $date_to < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
  483.                 update_post_meta( $post_id, '_sale_price_dates_from_' . $code, '' );
  484.                 update_post_meta( $post_id, '_sale_price_dates_to_' . $code, '' );
  485.             } elseif ( ! $date_from ) {
  486.                 update_post_meta(
  487.                     $post_id,
  488.                     '_sale_price_dates_from_' . $code,
  489.                     strtotime( 'NOW', current_time( 'timestamp' ) )
  490.                 );
  491.             }
  492.         }
  493.     }
  494.  
  495.     /**
  496.      * @param array $custom_prices
  497.      *
  498.      * @return bool
  499.      */
  500.     protected function is_sale_price_valid( array $custom_prices ) {
  501.         $sale_price_dates_from = $custom_prices['_sale_price_dates_from'];
  502.         $sale_price_dates_to   = $custom_prices['_sale_price_dates_to'];
  503.         $not_depend_on_date    = $sale_price_dates_to === '' && $sale_price_dates_from === '';
  504.         $valid_sale_date       = $sale_price_dates_from < strtotime( 'NOW', current_time( 'timestamp' ) );
  505.  
  506.         return $custom_prices['_sale_price'] !== '' && ( $not_depend_on_date || $valid_sale_date );
  507.     }
  508.  
  509.     /**
  510.      * @param int $product_id
  511.      */
  512.     public function sync_product_variations_custom_prices_on_ajax( $product_id ) {
  513.         Maybe::fromNullable( wc_get_product( $product_id ) )
  514.             ->map( invoke( 'get_children' ) )
  515.             ->map( Fns::map( [ $this, 'sync_product_variations_custom_prices' ] ) );
  516.     }
  517.  
  518.     /**
  519.      * @param int $product_id
  520.      */
  521.     public function sync_product_variations_custom_prices( $product_id ) {
  522.  
  523.         if ( isset( $_POST['_wcml_custom_prices'][ $product_id ] ) ) {
  524.  
  525.             // save custom prices for variation.
  526.             $nonce = filter_input( INPUT_POST, '_wcml_custom_prices_variation_' . $product_id . '_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  527.             if ( isset( $_POST['_wcml_custom_prices'][ $product_id ] ) && isset( $nonce ) && wp_verify_nonce( $nonce, 'wcml_save_custom_prices_variation_' . $product_id ) ) {
  528.                 update_post_meta( $product_id, '_wcml_custom_prices_status', $_POST['_wcml_custom_prices'][ $product_id ] );
  529.                 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
  530.  
  531.                 if ( $_POST['_wcml_custom_prices'][ $product_id ] == 1 ) {
  532.                     foreach ( $currencies as $code => $currency ) {
  533.                         $sale_price    = wc_format_decimal( $_POST['_custom_variation_sale_price'][ $code ][ $product_id ] );
  534.                         $regular_price = wc_format_decimal( $_POST['_custom_variation_regular_price'][ $code ][ $product_id ] );
  535.                         $date_from     = strtotime( $_POST['_custom_variation_sale_price_dates_from'][ $code ][ $product_id ] );
  536.                         $date_to       = strtotime( $_POST['_custom_variation_sale_price_dates_to'][ $code ][ $product_id ] );
  537.                         $schedule      = $_POST['_wcml_schedule'][ $code ][ $product_id ];
  538.  
  539.                         $custom_prices = apply_filters(
  540.                             'wcml_update_custom_prices_values',
  541.                             [
  542.                                 '_regular_price'         => $regular_price,
  543.                                 '_sale_price'            => $sale_price,
  544.                                 '_wcml_schedule'         => $schedule,
  545.                                 '_sale_price_dates_from' => $date_from,
  546.                                 '_sale_price_dates_to'   => $date_to,
  547.                             ],
  548.                             $code,
  549.                             $product_id
  550.                         );
  551.                         $price         = $this->update_custom_prices( $product_id, $custom_prices, $code );
  552.                     }
  553.                 }
  554.             }
  555.         }
  556.     }
  557.  
  558.     /**
  559.      * @param WC_Product $product_object
  560.      *
  561.      * @return bool
  562.      */
  563.     private function is_on_sale( $product_object ) {
  564.         $custom_prices = $this->get_product_custom_prices( $product_object->get_id() );
  565.  
  566.         return $custom_prices
  567.                && array_key_exists( '_sale_price', $custom_prices )
  568.                && array_key_exists( '_regular_price', $custom_prices )
  569.                && '' !== $custom_prices['_sale_price']
  570.                && $custom_prices['_sale_price'] !== $custom_prices['_regular_price'];
  571.     }
  572.  
  573.     /**
  574.      * @param bool       $on_sale
  575.      * @param WC_Product $product_object
  576.      *
  577.      * @return bool
  578.      */
  579.     public function filter_product_is_on_sale( $on_sale, $product_object ) {
  580.         if (
  581.             ! $on_sale &&
  582.             WCML_MULTI_CURRENCIES_INDEPENDENT === $this->woocommerce_wpml->settings['enable_multi_currency'] &&
  583.             $this->is_custom_prices_set_for_product( $product_object->get_id() )
  584.         ) {
  585.             $on_sale = $this->is_on_sale( $product_object );
  586.         }
  587.  
  588.         return $on_sale;
  589.     }
  590.  
  591.     /**
  592.      * @param int $product_id
  593.      *
  594.      * @return mixed
  595.      */
  596.     private function is_custom_prices_set_for_product( $product_id ) {
  597.         /**
  598.          * Allow to override the detection of custom prices.
  599.          *
  600.          * @param bool $check
  601.          * @param int  $productId
  602.          */
  603.         return (bool) apply_filters( 'wcml_product_has_custom_prices', (bool) get_post_meta( $product_id, '_wcml_custom_prices_status', true ), $product_id );
  604.     }
  605.  
  606.     /**
  607.      * WC when starts the sale copies price from _sale_price into _price field
  608.      * we should do the same for _sale_price_{currency} and _price_{currency}
  609.      *
  610.      * @param array $product_ids
  611.      */
  612.     public function maybe_set_sale_prices( $product_ids ) {
  613.         foreach ( $product_ids as $product_id ) {
  614.             if ( $this->is_custom_prices_set_for_product( $product_id ) ) {
  615.                 foreach ( $this->woocommerce_wpml->multi_currency->get_currencies() as $code => $currency ) {
  616.                     update_post_meta( $product_id, '_price_' . $code, get_post_meta( $product_id, '_sale_price_' . $code, true ) );
  617.                 }
  618.             }
  619.         }
  620.     }
  621.  
  622.     /**
  623.      * @param array $product_ids
  624.      */
  625.     public function maybe_remove_sale_prices( $product_ids ) {
  626.  
  627.         foreach ( $product_ids as $product_id ) {
  628.  
  629.             $product            = wc_get_product( $product_id );
  630.             $is_product_on_sale = $product && $product->get_sale_price();
  631.  
  632.             if ( $is_product_on_sale || ! $this->is_custom_prices_set_for_product( $product_id ) ) {
  633.                 continue;
  634.             }
  635.  
  636.             foreach ( $this->woocommerce_wpml->multi_currency->get_currencies() as $code => $currency ) {
  637.  
  638.                 $is_auto_schedule_set = ! get_post_meta( $product_id, '_wcml_schedule_' . $code, true );
  639.  
  640.                 if ( $is_auto_schedule_set ) {
  641.                     update_post_meta( $product_id, '_price_' . $code, get_post_meta( $product_id, '_regular_price_' . $code, true ) );
  642.                     update_post_meta( $product_id, '_sale_price_' . $code, '' );
  643.                 }
  644.             }
  645.         }
  646.     }
  647. }
  648.  
Advertisement
Add Comment
Please, Sign In to add comment