| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (!defined('ABSPATH')) {
|
| exit;
|
| }
|
|
|
|
|
|
|
|
|
| function format_availability_date($date) {
|
| if (empty($date)) {
|
| return '';
|
| }
|
|
|
|
|
| if (preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}[+-]\d{4}$/', $date)) {
|
| return $date;
|
| }
|
|
|
|
|
| $timestamp = strtotime($date);
|
| if ($timestamp === false) {
|
| return '';
|
| }
|
|
|
|
|
| $timezone = wp_timezone();
|
| $offset = $timezone->getOffset(new DateTime('@' . $timestamp));
|
| $offset_formatted = sprintf('%s%02d%02d',
|
| ($offset >= 0 ? '+' : '-'),
|
| floor(abs($offset) / 3600),
|
| floor((abs($offset) % 3600) / 60)
|
| );
|
|
|
| return date('Y-m-d\TH:i', $timestamp) . $offset_formatted;
|
| }
|
|
|
|
|
|
|
|
|
| function get_formatted_date_for_display($date) {
|
| if (empty($date)) {
|
| return '';
|
| }
|
|
|
| if (preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}[+-]\d{4}$/', $date)) {
|
| return substr($date, 0, 10);
|
| }
|
|
|
| return $date;
|
| }
|
|
|
|
|
|
|
|
|
| function add_availability_date_field() {
|
| global $post;
|
|
|
| $saved_date = get_post_meta($post->ID, '_availability_date', true);
|
| $display_date = get_formatted_date_for_display($saved_date);
|
|
|
| woocommerce_wp_text_input([
|
| 'id' => '_availability_date',
|
| 'label' => __('Availability Date', 'woocommerce'),
|
| 'placeholder' => __('YYYY-MM-DD', 'woocommerce'),
|
| 'desc_tip' => true,
|
| 'description' => __('Enter the date when this product will be available', 'woocommerce'),
|
| 'type' => 'date',
|
| 'value' => $display_date,
|
| ]);
|
| }
|
| add_action('woocommerce_product_options_inventory_product_data', 'add_availability_date_field');
|
|
|
|
|
|
|
|
|
| function add_availability_date_to_variations($loop, $variation_data, $variation) {
|
| $saved_date = get_post_meta($variation->ID, '_availability_date', true);
|
| $display_date = get_formatted_date_for_display($saved_date);
|
|
|
| woocommerce_wp_text_input([
|
| 'id' => '_availability_date_' . $variation->ID,
|
| 'name' => '_availability_date[' . $variation->ID . ']',
|
| 'label' => __('Availability Date', 'woocommerce'),
|
| 'placeholder' => __('YYYY-MM-DD', 'woocommerce'),
|
| 'desc_tip' => true,
|
| 'description' => __('Enter the date when this variation will be available', 'woocommerce'),
|
| 'type' => 'date',
|
| 'value' => $display_date,
|
| ]);
|
| }
|
| add_action('woocommerce_product_after_variable_attributes', 'add_availability_date_to_variations', 10, 3);
|
|
|
|
|
|
|
|
|
| function save_availability_date_field($post_id) {
|
| $product = wc_get_product($post_id);
|
| if (!$product) {
|
| return;
|
| }
|
|
|
| $product_type = $product->get_type();
|
|
|
| if ($product_type === 'variable') {
|
|
|
| if (isset($_POST['_availability_date']) && !is_array($_POST['_availability_date'])) {
|
| $date = format_availability_date(sanitize_text_field($_POST['_availability_date']));
|
| update_post_meta($post_id, '_availability_date', $date);
|
| }
|
|
|
|
|
| if (isset($_POST['variable_post_id'])) {
|
| foreach ($_POST['variable_post_id'] as $variation_id) {
|
| $variation_date_key = '_availability_date_' . $variation_id;
|
| if (isset($_POST[$variation_date_key])) {
|
| $date = format_availability_date(sanitize_text_field($_POST[$variation_date_key]));
|
| update_post_meta($variation_id, '_availability_date', $date);
|
| }
|
| }
|
| }
|
| } else {
|
|
|
| if (isset($_POST['_availability_date'])) {
|
| $date = format_availability_date(sanitize_text_field($_POST['_availability_date']));
|
| update_post_meta($post_id, '_availability_date', $date);
|
| }
|
| }
|
| }
|
| add_action('woocommerce_process_product_meta', 'save_availability_date_field', 10, 1);
|
| add_action('woocommerce_save_product_variation', 'save_availability_date_field', 10, 1);
|
|
|
|
|
|
|
|
|
| function add_availability_date_feed_attribute($attributes) {
|
| $attributes['Main attributes']['availability_date'] = 'Availability Date';
|
| return $attributes;
|
| }
|
| add_filter('adt_product_feed_attributes', 'add_availability_date_feed_attribute');
|
|
|
|
|
|
|
|
|
| function add_availability_date_to_feed($product_data, $feed, $product) {
|
| if ($product->is_type('variation')) {
|
| $variation_date = get_post_meta($product->get_id(), '_availability_date', true);
|
| if ($variation_date) {
|
| $product_data['availability_date'] = $variation_date;
|
| } else {
|
| $parent_date = get_post_meta($product->get_parent_id(), '_availability_date', true);
|
| if ($parent_date) {
|
| $product_data['availability_date'] = $parent_date;
|
| }
|
| }
|
| } else if ($product->is_type('variable')) {
|
| $variations = $product->get_available_variations();
|
| $earliest_date = null;
|
|
|
| foreach ($variations as $variation) {
|
| $variation_date = get_post_meta($variation['variation_id'], '_availability_date', true);
|
| if ($variation_date && (!$earliest_date || $variation_date < $earliest_date)) {
|
| $earliest_date = $variation_date;
|
| }
|
| }
|
|
|
| if ($earliest_date) {
|
| $product_data['availability_date'] = $earliest_date;
|
| } else {
|
| $parent_date = get_post_meta($product->get_id(), '_availability_date', true);
|
| if ($parent_date) {
|
| $product_data['availability_date'] = $parent_date;
|
| }
|
| }
|
| } else {
|
| $availability_date = get_post_meta($product->get_id(), '_availability_date', true);
|
| if ($availability_date) {
|
| $product_data['availability_date'] = $availability_date;
|
| }
|
| }
|
|
|
| return $product_data;
|
| }
|
| add_filter('adt_get_product_data', 'add_availability_date_to_feed', 10, 3);
|
| |
| |
Comments