1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
<?php
include_once( 'prdd-common.php' );
class prdd_checkout {
public static function prdd_custom_woocommerce_hidden_order_itemmeta( $arr ) {
$arr[] = '_prdd_date';
$arr[] = '_prdd_time_slot';
$arr[] = '_prdd_wpa_prices';
return $arr;
}
public static function prdd_order_item_meta( $item_meta, $cart_item ) {
if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) < 0 ) {
return;
}
global $wpdb, $woocommerce;
$order_item_ids = $ticket_content = array();
$sub_query = "";
$i = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$delivery = array();
$global_settings = json_decode( get_option( 'woocommerce_prdd_global_settings' ) );
$_product = $values[ 'data' ];
$post_id = prdd_common::prdd_get_product_id( $values[ 'product_id' ] );
$parent_id = ( version_compare( get_option( 'woocommerce_version' ), "3.0.0" ) < 0 ) ? $_product->get_parent() : prdd_common::prdd_get_parent_id( $post_id );
if( array_key_exists( "variation_id", $values ) ) {
$variation_id = $values[ 'variation_id' ];
} else {
$variation_id = '';
}
if ( isset( $values[ 'prdd_delivery' ] ) ) {
$delivery = $values[ 'prdd_delivery' ];
}
$quantity = $values[ 'quantity' ];
if( version_compare( get_option( 'woocommerce_version ' ), '3.0.0', ">=" ) ) {
$post_title = $_product->get_name();
} else {
$post_title = $_product->get_title();
}
$post_title = preg_replace( '/\\s+/', ' ', $post_title );
if ( count( $order_item_ids ) > 0 ) {
$order_item_ids_to_exclude = implode( ",", $order_item_ids );
$sub_query = " AND order_item_id NOT IN ( " . $order_item_ids_to_exclude . ")";
}
$query = "SELECT order_item_id,order_id FROM `" . $wpdb->prefix . "woocommerce_order_items`
WHERE order_id = %s AND order_item_name LIKE %s" . $sub_query;
$results = $wpdb->get_results( $wpdb->prepare( $query, $item_meta, $post_title . '%' ) );
$order_item_ids[] = $results[0]->order_item_id;
$order_id = $results[0]->order_id;
$order_obj = new WC_order( $order_id );
$details = $product_ids = array();
$order_items = $order_obj->get_items();
$name = get_option( 'delivery_item-meta-date' );
if( isset( $values[ 'prdd_expected_delivery_date' ] ) ) {
$name = "Delivery Expected";
$display = $values[ 'prdd_expected_delivery_date' ];
wc_add_order_item_meta( $results[0]->order_item_id, $name, $display, true );
} else {
if ( isset( $values[ 'prdd_delivery' ] ) ) {
$prdd_settings = get_post_meta( $post_id, 'woocommerce_prdd_settings', true );
$details = array();
if ( $delivery[0]['date'] != "" ) {
$date_select = $delivery[0]['date'];
wc_add_order_item_meta( $results[0]->order_item_id, $name, sanitize_text_field( $date_select, true ) );
}
if ( array_key_exists( 'hidden_date', $delivery[0] ) && $delivery[0][ 'hidden_date' ] != "" ) {
$date_booking = date( 'Y-m-d', strtotime( $delivery[0]['hidden_date'] ) );
wc_add_order_item_meta( $results[0]->order_item_id, '_prdd_date', sanitize_text_field( $date_booking, true ) );
}
if ( array_key_exists( 'time_slot', $delivery[0] ) && $delivery[0][ 'time_slot' ] != "" ) {
$time_slot_to_display = $time_slot_to_save = '';
$time_select = $delivery[0][ 'time_slot' ];
$time_exploded = explode( "-", $time_select );
$saved_settings = json_decode( get_option( 'woocommerce_prdd_global_settings' ) );
if ( isset( $saved_settings ) ) {
$time_format = $saved_settings->prdd_time_format;
} else {
$time_format = "12";
}
$time_slot_to_display = '';
$from_time = trim( $time_exploded[0] );
if( isset( $time_exploded[1] ) ) {
$to_time = trim( $time_exploded[1] );
} else {
$to_time = '';
}
if ( $time_format == '12' ) {
$from_time = date( 'h:i A', strtotime( $time_exploded[0] ) );
if(isset($time_exploded[1])){
$to_time = date( 'h:i A', strtotime( $time_exploded[1] ) );
}
}
$query_from_time = date( 'G:i', strtotime( $time_exploded[0] ) );
if( isset( $time_exploded[1] ) ) {
$query_to_time = date( 'G:i', strtotime( $time_exploded[1] ) );
$time_slot_to_save = $query_from_time . ' - '. $query_to_time;
} else {
$query_to_time = '';
$time_slot_to_save = $query_from_time;
}
if( $to_time != '' ) {
$time_slot_to_display = $from_time.' - '.$to_time;
} else {
$time_slot_to_display = $from_time;
}
wc_add_order_item_meta( $results[ 0 ]->order_item_id, get_option( 'delivery_item-meta-time' ), $time_slot_to_display, true );
wc_add_order_item_meta( $results[ 0 ]->order_item_id, '_prdd_time_slot', $time_slot_to_save, true );
}
}
if ( version_compare( WOOCOMMERCE_VERSION, "2.5" ) < 0 ) {
continue;
} else {
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'item_meta_array_' . $results[ 0 ]->order_item_id;
$item_meta_array = wp_cache_get( $cache_key, 'orders' );
if ( false !== $item_meta_array ) {
$metadata = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d AND meta_key IN (%s,%s,%s,%s) ORDER BY meta_id", absint( $results[ 0 ]->order_item_id ), $name, '_prdd_date', get_option( 'delivery_item-meta-time' ), '_prdd_time_slot' ) );
foreach ( $metadata as $metadata_row ) {
$item_meta_array[ $metadata_row->meta_id ] = (object) array( 'key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value );
}
wp_cache_set( $cache_key, $item_meta_array, 'orders' );
}
}
$gcal = new TsGcal();
if( $gcal->get_api_mode() == "directly" ) {
$event_details = prdd_common::prdd_get_event_details( $values, $order_id );
$gcal->insert_event( $event_details, $results[ 0 ]->order_item_id, false );
}
}
do_action( 'prdd_update_order', $values, $results[0] );
}
}
}
?>