· 4 years ago · Dec 24, 2020, 11:26 AM
1<?php
2if ( ! defined ( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4}
5
6
7if ( ! class_exists ( 'YITH_YWGC_Backend' ) ) {
8
9 /**
10 *
11 * @class YITH_YWGC_Backend
12 *
13 * @since 1.0.0
14 * @author Lorenzo Giuffrida
15 */
16 class YITH_YWGC_Backend {
17
18 const YWGC_GIFT_CARD_LAST_VIEWED_ID = 'ywgc_last_viewed';
19
20 /**
21 * Single instance of the class
22 *
23 * @since 1.0.0
24 */
25 protected static $instance;
26
27 /**
28 * race condition active
29 *
30 * @since 2.0.3
31 */
32 protected static $rc_active;
33
34 /**
35 * Returns single instance of the class
36 *
37 * @since 1.0.0
38 */
39 public static function get_instance() {
40 if ( is_null ( self::$instance ) ) {
41 self::$instance = new self();
42 }
43
44 return self::$instance;
45 }
46
47 /**
48 * Constructor
49 *
50 * Initialize plugin and registers actions and filters to be used
51 *
52 * @since 1.0
53 * @author Lorenzo Giuffrida
54 */
55 protected function __construct() {
56
57 /**
58 * Enqueue scripts and styles
59 */
60 add_action ( 'admin_enqueue_scripts', array( $this, 'enqueue_backend_files' ) );
61
62 /**
63 * Add the "Gift card" type to product type list
64 */
65 add_filter ( 'product_type_selector', array($this, 'add_gift_card_product_type') );
66
67 /**
68 * * Save gift card data when a product of type "gift card" is saved
69 */
70 add_action ( 'save_post', array($this, 'save_gift_card'), 1, 2 );
71
72 /**
73 * Ajax call for adding and removing gift card amounts on product edit page
74 */
75 add_action ( 'wp_ajax_add_gift_card_amount', array($this, 'add_gift_card_amount_callback') );
76 add_action ( 'wp_ajax_remove_gift_card_amount', array($this, 'remove_gift_card_amount_callback') );
77
78 /**
79 * Hide some item meta from product edit page
80 */
81 add_filter ( 'woocommerce_hidden_order_itemmeta', array($this, 'hide_item_meta') );
82
83 if ( version_compare ( WC ()->version, '2.6.0', '<' ) ) {
84
85 /**
86 * Append gift card amount generation controls to general tab of product page, below the SKU element
87 */
88 add_action ( 'woocommerce_product_options_sku', array($this, 'show_gift_card_product_settings') );
89
90 } else {
91 /**
92 * Append gift card amount generation controls to general tab on product page
93 */
94 add_action ( 'woocommerce_product_options_general_product_data', array($this, 'show_gift_card_product_settings') );
95 }
96
97 /**
98 * Generate a valid card number for every gift card product in the order
99 */
100 add_action ( 'woocommerce_order_status_changed', array($this, 'order_status_changed'), 10, 3 );
101
102 add_action ( 'woocommerce_before_order_itemmeta', array($this, 'show_gift_card_code_on_order_item'), 10, 3 );
103
104 /**
105 * Set the CSS class 'show_if_gift-card in tax section
106 */
107 add_action ( 'woocommerce_product_options_general_product_data', array($this, 'show_tax_class_for_gift_cards') );
108
109 /**
110 * Custom condition to create gift card on cash on delivery only on complete status
111 */
112 add_filter ( 'ywgc_custom_condition_to_create_gift_card', array($this, 'ywgc_custom_condition_to_create_gift_card_call_back'), 10, 2 );
113
114 /**
115 * Set the CSS class 'show_if_gift-card in 'sold indidually' section
116 */
117 add_action( 'woocommerce_product_options_inventory_product_data', array($this, 'show_sold_individually_for_gift_cards') );
118
119 /**
120 * manage CSS class for the gift cards table rows
121 */
122 add_filter( 'post_class', array( $this, 'add_cpt_table_class' ), 10, 3 );
123
124 add_action( 'init', array( $this, 'redirect_gift_cards_link' ) );
125
126 add_action( 'load-upload.php', array( $this, 'set_gift_card_category_to_media' ) );
127
128 add_action( 'edited_term_taxonomy', array( $this, 'update_taxonomy_count' ), 10, 2 );
129
130 /*
131 * Save additional product attribute when a gift card product is saved
132 */
133 add_action( 'yith_gift_cards_after_product_save', array($this, 'save_gift_card_product') );
134
135 /**
136 * Show inventory tab in product tabs
137 */
138 add_filter( 'woocommerce_product_data_tabs', array($this, 'show_inventory_tab') );
139
140 add_action( 'yith_ywgc_product_settings_after_amount_list', array($this, 'show_advanced_product_settings') );
141
142 /**
143 * Show gift cards code and amount in order's totals section, in edit order page
144 */
145 add_action( 'woocommerce_admin_order_totals_after_tax', array($this, 'show_gift_cards_total_before_order_totals') );
146
147 /**
148 * Add filters on the Gift Card Post Type page
149 */
150 add_filter( 'views_edit-gift_card', array( $this, 'add_gift_cards_filters' ) );
151 add_action( 'pre_get_posts', array( $this, 'filter_gift_card_page_query' ) );
152
153 /*
154 * Filter display order item meta key to show
155 */
156 add_filter( 'woocommerce_order_item_display_meta_key',array($this,'show_as_string_order_item_meta_key'),10,1 );
157
158 /*
159 * Filter display order item meta value to show
160 */
161 add_filter( 'woocommerce_order_item_display_meta_value', array( $this,'show_formatted_date' ),10,3 );
162
163
164 add_action('woocommerce_order_status_changed', array($this,'update_gift_card_amount_on_order_status_change'),10,4);
165
166 /*
167 * Recalculate order totals on save order items (in order to show always the correct total for the order)
168 */
169 add_action('woocommerce_saved_order_items', array($this,'update_totals_on_save_order_items'),10,2);
170
171 add_action( 'add_meta_boxes' , array( $this, 'ywgc_remove_product_meta_boxes' ), 40 );
172
173 add_action( 'save_post', array( $this, 'set_gift_card_category_to_product' ) );
174
175
176 }
177
178 /**
179 * Show the gift card code under the order item, in the order admin page
180 *
181 * @param int $item_id
182 * @param array $item
183 * @param WC_product $_product
184 *
185 * @author Lorenzo Giuffrida
186 * @since 1.0.0
187 */
188 public function show_gift_card_code_on_order_item( $item_id, $item, $_product ) {
189
190 global $theorder;
191
192 $gift_ids = ywgc_get_order_item_giftcards ( $item_id );
193
194 if ( empty( $gift_ids ) ) {
195 return;
196 }
197
198 foreach ( $gift_ids as $gift_id ) {
199
200 $gc = new YITH_YWGC_Gift_Card( array( 'ID' => $gift_id ) );
201
202 ?>
203 <div>
204 <span class="ywgc-gift-code-label"><?php _e ( "Gift card code: ", 'yith-woocommerce-gift-cards' ); ?></span>
205 <a href="<?php echo admin_url ( 'edit.php?s=' . $gc->get_code () . '&post_type=gift_card&mode=list' ); ?>"
206 class="ywgc-card-code"><?php echo $gc->get_code (); ?></a>
207 </div>
208 <?php
209 }
210 }
211
212
213 /**
214 * Enqueue scripts on administration comment page
215 *
216 * @param $hook
217 */
218 function enqueue_backend_files( $hook ) {
219 global $post_type;
220
221 $screen = get_current_screen ();
222
223 // Enqueue style and script for the edit-gift_card screen id
224 if ( "edit-gift_card" == $screen->id ) {
225
226 // When viewing the gift card page, store the max id so all new gift cards will be notified next time
227 global $wpdb;
228 $last_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT max(id) FROM {$wpdb->prefix}posts WHERE post_type = %s", YWGC_CUSTOM_POST_TYPE_NAME ) );
229 update_option ( self::YWGC_GIFT_CARD_LAST_VIEWED_ID, $last_id );
230 }
231
232 if ( ( 'product' == $post_type ) || ( 'gift_card' == $post_type ) || ( 'shop_order' == $post_type ) || isset( $_REQUEST[ 'page' ] ) && $_REQUEST[ 'page' ] == 'yith_woocommerce_gift_cards_panel' ) {
233
234 // Add style and scripts
235 wp_enqueue_style ( 'ywgc-backend-css',
236 YITH_YWGC_ASSETS_URL . '/css/ywgc-backend.css',
237 array(),
238 YITH_YWGC_VERSION );
239
240 wp_register_script ( "ywgc-backend",
241
242 YITH_YWGC_SCRIPT_URL . yit_load_js_file ( 'ywgc-backend.js' ),
243 array(
244 'jquery',
245 'jquery-blockui',
246 ),
247 YITH_YWGC_VERSION,
248 true );
249
250 $date_format =get_option( 'ywgc_plugin_date_format_option', 'yy-mm-dd' );
251
252 wp_localize_script ( 'ywgc-backend',
253 'ywgc_data', array(
254 'loader' => apply_filters ( 'yith_gift_cards_loader', YITH_YWGC_ASSETS_URL . '/images/loading.gif' ),
255 'ajax_url' => admin_url ( 'admin-ajax.php' ),
256 'choose_image_text' => esc_html__( 'Choose Image', 'yith-woocommerce-gift-cards' ),
257 'date_format' => $date_format,
258 )
259 );
260
261 wp_enqueue_script ( "ywgc-backend" );
262 }
263
264 if ( "upload" == $screen->id ) {
265
266 wp_register_script ( "ywgc-categories",
267 YITH_YWGC_SCRIPT_URL . yit_load_js_file ( 'ywgc-categories.js' ),
268 array(
269 'jquery',
270 'jquery-blockui',
271 ),
272 YITH_YWGC_VERSION,
273 true );
274
275 $categories1_id = 'categories1_id';
276 $categories2_id = 'categories2_id';
277
278 wp_localize_script ( 'ywgc-categories', 'ywgc_data', array(
279 'loader' => apply_filters ( 'yith_gift_cards_loader', YITH_YWGC_ASSETS_URL . '/images/loading.gif' ),
280 'ajax_url' => admin_url ( 'admin-ajax.php' ),
281 'set_category_action' => esc_html__( "Set gift card category", 'yith-woocommerce-gift-cards' ),
282 'unset_category_action' => esc_html__( "Unset gift card category", 'yith-woocommerce-gift-cards' ),
283 'categories1' => $this->get_category_select ( $categories1_id ),
284 'categories1_id' => $categories1_id,
285 'categories2' => $this->get_category_select ( $categories2_id ),
286 'categories2_id' => $categories2_id,
287 ) );
288
289 wp_enqueue_script ( "ywgc-categories" );
290 }
291
292
293 if ( "edit-giftcard-category" == $screen->id ) {
294
295 wp_enqueue_media();
296 wp_register_script ( "ywgc-media-button",
297 YITH_YWGC_SCRIPT_URL . yit_load_js_file ( 'ywgc-media-button.js' ),
298 array(
299 'jquery',
300 ),
301 YITH_YWGC_VERSION,
302 true );
303
304 wp_localize_script( 'ywgc-media-button', 'ywgc_data', array(
305 'upload_file_frame_title' => esc_html__( 'Manage the Media library', 'yith-woocommerce-gift-cards' ),
306 'upload_file_frame_button' => esc_html__( 'Done', 'yith-woocommerce-gift-cards' )
307 ) );
308
309 wp_enqueue_script ( "ywgc-media-button" );
310 }
311
312
313 }
314
315 public function get_category_select( $select_id ) {
316 $media_terms = get_terms ( YWGC_CATEGORY_TAXONOMY, 'hide_empty=0' );
317
318 $select = '<select id="' . $select_id . '" name="' . $select_id . '">';
319 foreach ( $media_terms as $entry ) {
320 $select .= '<option value="' . $entry->term_id . '">' . $entry->name . '</option>';
321 }
322 $select .= '</select>';
323
324 return $select;
325
326 }
327
328 /**
329 * Add the "Gift card" type to product type list
330 *
331 * @param array $types current type array
332 *
333 * @return mixed
334 * @author Lorenzo Giuffrida
335 * @since 1.0.0
336 */
337 public function add_gift_card_product_type( $types ) {
338 if ( YITH_YWGC ()->current_user_can_create () ) {
339 $types[ YWGC_GIFT_CARD_PRODUCT_TYPE ] = esc_html__( "Gift card", 'yith-woocommerce-gift-cards' );
340 }
341
342 return $types;
343 }
344
345 /**
346 * Save gift card additional data
347 *
348 * @param $product_id
349 */
350 public function save_gift_card_data( $product_id ) {
351
352 $product = new WC_Product_Gift_Card( $product_id );
353
354 /**
355 * Save custom gift card header image, if exists
356 */
357 if ( isset( $_REQUEST['ywgc_product_image_id'] ) ) {
358 if ( intval ( $_REQUEST['ywgc_product_image_id'] ) ) {
359
360 $product->set_header_image ( $_REQUEST['ywgc_product_image_id'] );
361 } else {
362
363 $product->unset_header_image ();
364 }
365 }
366
367
368 /**
369 * Save gift card amounts
370 */
371 $amounts = isset( $_POST["gift-card-amounts"] ) ? $_POST["gift-card-amounts"] : array();
372 $amounts = !empty( $amounts ) ? $amounts : ( isset( $_POST["gift_card-amount"] ) && $_POST["gift_card-amount"] ? array( $_POST["gift_card-amount"] ) : array() );
373
374 $product->save_amounts ( $amounts );
375
376 /**
377 * Save gift card settings about template design
378 */
379 if ( isset( $_POST['template-design-mode'] ) ) {
380 $product->set_design_status ( $_POST['template-design-mode'] );
381 }
382 }
383
384
385 /**
386 * Save gift card amount when a product is saved
387 *
388 * @param $post_id int
389 * @param $post object
390 *
391 * @return mixed
392 */
393 function save_gift_card( $post_id, $post ) {
394
395 $product = wc_get_product ( $post_id );
396
397 if ( null == $product ) {
398 return;
399 }
400
401 if ( ! isset( $_POST["product-type"] ) || ( YWGC_GIFT_CARD_PRODUCT_TYPE != $_POST["product-type"] ) ) {
402
403 return;
404 }
405
406 // verify this is not an auto save routine.
407 if ( defined ( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
408 return;
409 }
410
411 /**
412 * Update gift card amounts
413 */
414 $this->save_gift_card_data ( $post_id );
415
416
417 do_action ( 'yith_gift_cards_after_product_save', $post_id, $post, $product );
418 }
419
420
421 /**
422 * Add a new amount to a gift card prdduct
423 *
424 * @since 1.0
425 * @author Lorenzo Giuffrida
426 */
427 public function add_gift_card_amount_callback() {
428
429 $amount = wc_format_decimal ( $_POST['amount'] );
430
431 if ( ! is_numeric( $amount ) )
432 return;
433
434 $product_id = intval ( $_POST['product_id'] );
435 $gift = new WC_Product_Gift_Card( $product_id );
436 $res = false;
437
438 if ( $gift->exists () ) {
439 $res = $gift->add_amount ( $amount );
440 }
441
442 wp_send_json (
443 array(
444 "code" => $res ? 1 : 0,
445 "value" => $this->gift_card_amount_list_html ( $product_id )
446 ) );
447 }
448
449 /**
450 * Remove amount to a gift card prdduct
451 *
452 * @since 1.0
453 * @author Lorenzo Giuffrida
454 */
455 public function remove_gift_card_amount_callback() {
456 $amount = wc_format_decimal ( $_POST['amount'] );
457 $product_id = intval ( $_POST['product_id'] );
458
459 $gift = new WC_Product_Gift_Card( $product_id );
460 if ( $gift->exists () ) {
461 $gift->remove_amount ( $amount );
462 }
463
464 wp_send_json ( array( "code" => '1' ) );
465 }
466
467 /**
468 * Retrieve the html content that shows the gift card amounts list
469 *
470 * @param $product_id int gift card product id
471 *
472 * @return string
473 */
474 private function gift_card_amount_list_html( $product_id ) {
475
476 ob_start ();
477 $this->show_gift_card_amount_list ( $product_id );
478 $html = ob_get_contents ();
479 ob_end_clean ();
480
481 return $html;
482 }
483
484
485 /**
486 * Hide some item meta from order edit page
487 */
488 public function hide_item_meta( $args ) {
489 $args[] = YWGC_META_GIFT_CARD_POST_ID;
490
491 return $args;
492 }
493
494 /**
495 * Show controls on backend product page to let create the gift card price
496 */
497 public function show_gift_card_product_settings() {
498
499 if ( ! YITH_YWGC ()->current_user_can_create () ) {
500 return;
501 }
502
503 global $post, $thepostid;
504 ?>
505 <div class="options_group show_if_gift-card">
506 <p class="form-field">
507 <label for="gift_card-amount"><?php _e ( "Gift card amount", 'yith-woocommerce-gift-cards' ); ?></label>
508 <span class="wrap add-new-amount-section">
509 <input type="text" id="gift_card-amount" name="gift_card-amount" class="short wc_input_price" style=""
510 placeholder="">
511 <a href="#" class="add-new-amount"><?php _e ( "Add", 'yith-woocommerce-gift-cards' ); ?></a>
512 </span>
513 </p>
514
515 <?php
516 $this->show_gift_card_amount_list ( $thepostid );
517 do_action ( 'yith_ywgc_product_settings_after_amount_list', $thepostid );
518
519 ?>
520 </div>
521 <?php
522 }
523
524 /**
525 * Show the gift card amounts list
526 *
527 * @param $product_id int gift card product id
528 */
529 private function show_gift_card_amount_list( $product_id ) {
530
531 $gift_card = new WC_Product_Gift_Card( $product_id );
532 if ( ! $gift_card->exists () ) {
533 return;
534 }
535 $amounts = $gift_card->get_product_amounts ();
536
537 ?>
538
539 <p class="form-field _gift_card_amount_field">
540 <?php if ( $amounts ): ?>
541 <?php foreach ( $amounts as $amount ) : ?>
542 <span class="variation-amount"><?php echo wc_price ( $amount ); ?>
543 <input type="hidden" name="gift-card-amounts[]" value="<?php _e ( $amount ); ?>">
544 <a href="#" class="remove-amount"></a></span>
545 <?php endforeach; ?>
546 <?php else: ?>
547 <span
548 class="no-amounts"><?php _e ( "You haven't configured any gift card yet", 'yith-woocommerce-gift-cards' ); ?></span>
549 <?php endif; ?>
550 </p>
551 <?php
552 }
553
554
555 /**
556 * When the order is completed, generate a card number for every gift card product
557 *
558 * @param int|WC_Order $order The order which status is changing
559 * @param string $old_status Current order status
560 * @param string $new_status New order status
561 *
562 */
563 public function order_status_changed( $order, $old_status, $new_status ) {
564
565 if ( is_numeric ( $order ) ) {
566 $order = wc_get_order ( $order );
567 }
568
569 $allowed_status = apply_filters ( 'yith_ywgc_generate_gift_card_on_order_status',
570 array( 'completed', 'processing' ) );
571
572 if ( in_array ( $new_status, $allowed_status ) ) {
573 $this->generate_gift_card_for_order ( $order );
574
575 $used_gift_cards = yit_get_prop($order, '_ywgc_applied_gift_cards', true);
576
577 if ( isset($used_gift_cards) && ! empty($used_gift_cards) ) {
578 $checkout_instance = YITH_YWGC_Cart_Checkout::get_instance();
579 foreach ($used_gift_cards as $gift_card_code => $value) {
580 $gift_card = YITH_YWGC()->get_gift_card_by_code( $gift_card_code );
581 }
582 }
583
584 } elseif ( 'refunded' == $new_status ) {
585 $this->change_gift_cards_status_on_order ( $order,'nothing' );
586 } elseif ( 'cancelled' == $new_status ) {
587 $this->change_gift_cards_status_on_order ( $order, 'nothing' );
588 }
589 }
590
591 /**
592 * Generate the gift card code, if not yet generated
593 *
594 * @param WC_Order $order
595 *
596 * @author Lorenzo Giuffrida
597 * @since 1.0.0
598 */
599 public function generate_gift_card_for_order( $order ) {
600 if ( is_numeric ( $order ) ) {
601 $order = new WC_Order( $order );
602 }
603
604 if ( apply_filters ( 'yith_gift_cards_generate_on_order_completed', true, $order ) ) {
605
606 $this->create_gift_cards_for_order ( $order );
607 }
608 }
609
610 /**
611 * Custom condition
612 *
613 * @param WC_Order $order
614 * @return boolean
615 *
616 * @author Daniel Sanchez <daniel.sanchez@yithemes.com>
617 * @since 2.0.6
618 */
619 public function ywgc_custom_condition_to_create_gift_card_call_back( $cond, $order ) {
620
621 $gateway = wc_get_payment_gateway_by_order( $order );
622 if ( $order->get_status() == 'processing' && is_object($gateway) && $gateway instanceof WC_Gateway_COD )
623 return false;
624
625 return true;
626
627 }
628
629 /**
630 * Create the gift cards for the order
631 *
632 * @param WC_Order $order
633 */
634 public function create_gift_cards_for_order( $order ) {
635
636 if ( ! apply_filters( 'ywgc_custom_condition_to_create_gift_card', true, $order ) )
637 return;
638
639
640 foreach ( $order->get_items ( 'line_item' ) as $order_item_id => $order_item_data ) {
641
642 $product_id = $order_item_data["product_id"];
643 $product = wc_get_product ( $product_id );
644
645 // skip all item that belong to product other than the gift card type
646 if ( ! $product instanceof WC_Product_Gift_Card ) {
647 continue;
648 }
649
650 // Check if current product, of type gift card, has a previous gift card
651 // code before creating another
652 if ( $gift_ids = ywgc_get_order_item_giftcards ( $order_item_id ) ) {
653 continue;
654 }
655
656 if ( ! apply_filters ( 'yith_ywgc_create_gift_card_for_order_item', true, $order, $order_item_id, $order_item_data ) ) {
657 continue;
658 }
659
660 $order_id = yit_get_order_id ( $order );
661
662 $line_subtotal = apply_filters ( 'yith_ywgc_line_subtotal', $order_item_data["line_subtotal"], $order_item_data, $order_id, $order_item_id );
663 $line_subtotal_tax = apply_filters ( 'yith_ywgc_line_subtotal_tax', $order_item_data["line_subtotal_tax"], $order_item_data, $order_id, $order_item_id );
664
665 // Generate as many gift card code as the quantity bought
666 $quantity = $order_item_data["qty"];
667 $single_amount = (float) ( $line_subtotal / $quantity );
668 $single_tax = (float) ( $line_subtotal_tax / $quantity );
669
670 $new_ids = array();
671
672 $order_currency = version_compare( WC()->version, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency();
673
674 $product_id = wc_get_order_item_meta ( $order_item_id, '_ywgc_product_id' );
675 $amount = wc_get_order_item_meta ( $order_item_id, '_ywgc_amount' );
676 $is_digital = wc_get_order_item_meta ( $order_item_id, '_ywgc_is_digital' );
677
678 $is_postdated = false;
679
680 if ( $is_digital ) {
681 $recipients = wc_get_order_item_meta ( $order_item_id, '_ywgc_recipients' );
682 $recipient_count = count ( (array)$recipients );
683 $sender = wc_get_order_item_meta ( $order_item_id, '_ywgc_sender_name' );
684 $recipient_name = wc_get_order_item_meta ( $order_item_id, '_ywgc_recipient_name' );
685 $message = wc_get_order_item_meta ( $order_item_id, '_ywgc_message' );
686 $has_custom_design = wc_get_order_item_meta ( $order_item_id, '_ywgc_has_custom_design' );
687 $design_type = wc_get_order_item_meta ( $order_item_id, '_ywgc_design_type' );
688 $postdated = apply_filters('ywgc_postdated_by_default', wc_get_order_item_meta ( $order_item_id, '_ywgc_postdated' ));
689
690 $is_postdated = true == apply_filters('ywgc_is_postdated_delivery_date_by_default', wc_get_order_item_meta ( $order_item_id, '_ywgc_postdated', true ));
691 if ( $is_postdated ) {
692 $delivery_date = wc_get_order_item_meta ( $order_item_id, '_ywgc_delivery_date', true );
693 }
694 }
695
696 for ( $i = 0; $i < $quantity; $i ++ ) {
697
698 // Generate a gift card post type and save it
699 $gift_card = new YITH_YWGC_Gift_Card();
700
701 $gift_card->product_id = $product_id;
702 $gift_card->order_id = $order_id;
703 $gift_card->is_digital = $is_digital;
704
705 if ( $gift_card->is_digital ) {
706 $gift_card->sender_name = $sender;
707 $gift_card->recipient_name = $recipient_name;
708 $gift_card->message = $message;
709 $gift_card->postdated_delivery = $is_postdated;
710 if ( $is_postdated ) {
711 $gift_card->delivery_date = $delivery_date;
712 }
713
714 $gift_card->has_custom_design = $has_custom_design;
715 $gift_card->design_type = $design_type;
716
717 if ( $has_custom_design ) {
718 $gift_card->design = wc_get_order_item_meta ( $order_item_id, '_ywgc_design' );
719 }
720
721 $gift_card->postdated_delivery = $postdated;
722 if ( $postdated ) {
723 $gift_card->delivery_date = $delivery_date;
724 }
725
726 /**
727 * If the user entered several recipient email addresses, one gift card
728 * for every recipient will be created and it will be the unique recipient for
729 * that email. If only one, or none if allowed, recipient email address was entered
730 * then create '$quantity' specular gift cards
731 */
732 if ( ( $recipient_count == 1 ) && ! empty( $recipients[0] ) ) {
733 $gift_card->recipient = $recipients[0];
734 } elseif ( ( $recipient_count > 1 ) && ! empty( $recipients[ $i ] ) ) {
735 $gift_card->recipient = $recipients[ $i ];
736 } else {
737 /**
738 * Set the customer as the recipient of the gift card
739 *
740 */
741 $gift_card->recipient = apply_filters ( 'yith_ywgc_set_default_gift_card_recipient', yit_get_prop($order, 'billing_email') );
742 }
743 }
744
745 $attempts = 100;
746 do {
747 $code = apply_filters( 'yith_wcgc_generated_code', YITH_YWGC ()->generate_gift_card_code(), $order, $gift_card );
748 $check_code = get_page_by_title ( $code, OBJECT, YWGC_CUSTOM_POST_TYPE_NAME );
749
750 if ( ! $check_code ) {
751 $gift_card->gift_card_number = $code;
752 break;
753 }
754 $attempts --;
755 } while ( $attempts > 0 );
756
757 if ( ! $attempts ) {
758 // Unable to find a unique code, the gift card need a manual code entered
759 $gift_card->set_as_code_not_valid ();
760 }
761
762
763 $gift_card->total_amount = $single_amount + $single_tax;
764
765 $on_sale = get_post_meta( $gift_card->product_id, '_ywgc_sale_discount_value', true );
766
767 if ( $on_sale ) {
768 $gift_card->total_amount = wc_get_order_item_meta( $order_item_id, '_ywgc_amount_without_discount', true);
769 }
770
771 $gift_card->update_balance ( $gift_card->total_amount );
772 $gift_card->version = YITH_YWGC_VERSION;
773 $gift_card->currency = $order_currency;
774
775 $gift_card->expiration = 0;
776
777 do_action( 'yith_ywgc_before_gift_card_generation_save', $gift_card );
778
779 $gift_card->save ();
780
781 do_action( 'yith_ywgc_after_gift_card_generation_save', $gift_card );
782
783 // Save the gift card id
784 $new_ids[] = $gift_card->ID;
785
786 // ...and send it now if it's not postdated
787 if ( (! $is_postdated && apply_filters( 'ywgc_send_gift_card_code_by_default', true, $gift_card) )|| apply_filters('yith_wcgc_send_now_gift_card_to_custom_recipient',false,$gift_card ) ) {
788
789 YITH_YWGC_Emails::get_instance ()->send_gift_card_email ( $gift_card );
790 }
791 }
792
793 // save gift card Post ids on order item
794 ywgc_set_order_item_giftcards ( $order_item_id, $new_ids );
795
796 }
797 if ( apply_filters( 'ywgc_apply_race_condition', false ) )
798 $this->end_race_condition( $order->get_id() );
799
800 }
801
802
803 /**
804 * The order is set to completed
805 *
806 * @param WC_Order $order
807 * @param string $action
808 *
809 * @author Lorenzo Giuffrida
810 * @since 1.0.0
811 */
812 public function change_gift_cards_status_on_order( $order, $action ) {
813
814 if ( 'nothing' == $action ) {
815 return;
816 }
817
818 foreach ( $order->get_items () as $item_id => $item ) {
819 $ids = ywgc_get_order_item_giftcards ( $item_id );
820
821 if ( $ids ) {
822 foreach ( $ids as $gift_id ) {
823
824 $gift_card = new YITH_YWGC_Gift_Card( array( 'ID' => $gift_id ) );
825
826 if ( ! $gift_card->exists () ) {
827 continue;
828 }
829
830 if ( 'dismiss' == $action ) {
831 $gift_card->set_dismissed_status ();
832 } elseif ( 'disable' == $action ) {
833
834 $gift_card->set_enabled_status ( false );
835 }
836 }
837 }
838 }
839 }
840
841 public function show_tax_class_for_gift_cards() {
842 echo '<script>
843 jQuery("select#_tax_status").closest(".options_group").addClass("show_if_gift-card");
844 </script>';
845 }
846
847
848 /**
849 * Show gift cards code and amount in order's totals section, in edit order page
850 *
851 * @param int $order_id
852 */
853 public function show_gift_cards_total_before_order_totals( $order_id ) {
854
855 $order = wc_get_order( $order_id );
856 $order_gift_cards = yit_get_prop( $order, YITH_YWGC_Cart_Checkout::ORDER_GIFT_CARDS, true );
857 $currency = version_compare( WC()->version, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency();
858
859 if ( $order_gift_cards ) :
860 foreach ( $order_gift_cards as $code => $amount ): ?>
861 <?php $amount = apply_filters('ywgc_gift_card_amount_order_total_item', $amount, YITH_YWGC()->get_gift_card_by_code( $code ) ); ?>
862 <tr>
863 <td class="label"><?php _e( 'Gift card: ' . $code, 'yith-woocommerce-gift-cards' ); ?>:</td>
864 <td width="1%"></td>
865 <td class="total">
866 <?php echo wc_price( $amount, array( 'currency' => $currency ) ); ?>
867 </td>
868 </tr>
869 <?php endforeach;
870 endif;
871 }
872
873 /**
874 * Show inventory section for gift card products
875 *
876 * @param array $tabs
877 *
878 * @return mixed
879 */
880 public function show_inventory_tab( $tabs ) {
881 if ( isset( $tabs['inventory'] ) ) {
882
883 array_push( $tabs['inventory']['class'], 'show_if_gift-card' );
884 }
885
886 return $tabs;
887
888 }
889
890 /**
891 * Save additional product attribute when a gift card product is saved
892 *
893 * @param int $post_id current product id
894 */
895 public function save_gift_card_product( $post_id ) {
896
897 if ( isset( $_POST["gift_card-sale-discount"] ) ) {
898 update_post_meta( $post_id, '_ywgc_sale_discount_value', $_POST["gift_card-sale-discount"] );
899 }
900
901 if ( isset( $_POST["gift_card-sale-discount-text"] ) ) {
902 update_post_meta( $post_id, '_ywgc_sale_discount_text', $_POST["gift_card-sale-discount-text"] );
903 }
904
905 if ( isset( $_POST["gift-card-expiration-date"] ) ) {
906
907 $date_format = apply_filters('yith_wcgc_date_format','Y-m-d');
908
909 $expiration_date = is_string($_POST["gift-card-expiration-date"]) ? strtotime($_POST["gift-card-expiration-date"]) : $_POST["gift-card-expiration-date"];
910
911 $expiration_date_formatted = !empty( $expiration_date ) ? date_i18n( $date_format, $expiration_date ) : '';
912
913 update_post_meta( $post_id, '_ywgc_expiration', $expiration_date );
914
915 update_post_meta( $post_id, '_ywgc_expiration_date', $expiration_date_formatted );
916 }
917
918
919 }
920
921
922 /**
923 * Fix the taxonomy count of items
924 *
925 * @param $term_id
926 * @param $taxonomy_name
927 *
928 * @author Lorenzo Giuffrida
929 * @since 1.0.0
930 */
931 public function update_taxonomy_count( $term_id, $taxonomy_name ) {
932 // Update the count of terms for attachment taxonomy
933 if ( YWGC_CATEGORY_TAXONOMY != $taxonomy_name ) {
934 return;
935 }
936
937 // update now
938 global $wpdb;
939 $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND (post_parent = 0 OR (post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term_id ) );
940
941 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term_id ) );
942 }
943
944
945 public function set_gift_card_category_to_media() {
946
947 // Skip all request without an action
948 if ( ! isset( $_REQUEST['action'] ) && ! isset( $_REQUEST['action2'] ) ) {
949 return;
950 }
951
952 // Skip all request without a valid action
953 if ( ( '-1' == $_REQUEST['action'] ) && ( '-1' == $_REQUEST['action2'] ) ) {
954 return;
955 }
956
957 $action = '-1' != $_REQUEST['action'] ? $_REQUEST['action'] : $_REQUEST['action2'];
958
959 // Skip all request that do not belong to gift card categories
960 if ( ( 'ywgc-set-category' != $action ) && ( 'ywgc-unset-category' != $action ) ) {
961 return;
962 }
963
964 // Skip all request without a media list
965 if ( ! isset( $_REQUEST['media'] ) ) {
966 return;
967 }
968
969 $media_ids = $_REQUEST['media'];
970
971 // Check if the request if for set or unset the selected category to the selected media
972 $action_set_category = ( 'ywgc-set-category' == $action ) ? true : false;
973
974 // Retrieve the category to be applied to the selected media
975 $category_id = '-1' != $_REQUEST['action'] ? intval( $_REQUEST['categories1_id'] ) : intval( $_REQUEST['categories2_id'] );
976
977 foreach ( $media_ids as $media_id ) {
978
979 // Check whether this user can edit this post
980 //if ( ! current_user_can ( 'edit_post', $media_id ) ) continue;
981
982 if ( $action_set_category ) {
983 $result = wp_set_object_terms( $media_id, $category_id, YWGC_CATEGORY_TAXONOMY, true );
984 } else {
985 $result = wp_remove_object_terms( $media_id, $category_id, YWGC_CATEGORY_TAXONOMY );
986 }
987
988 if ( is_wp_error( $result ) ) {
989 return $result;
990 }
991 }
992 }
993
994 /**
995 * manage CSS class for the gift cards table rows
996 *
997 * @param array $classes
998 * @param string $class
999 * @param int $post_id
1000 *
1001 * @return array|mixed|void
1002 * @author Lorenzo Giuffrida
1003 * @since 1.0.0
1004 */
1005 public function add_cpt_table_class( $classes, $class, $post_id ) {
1006
1007 if ( YWGC_CUSTOM_POST_TYPE_NAME != get_post_type( $post_id ) ) {
1008 return $classes;
1009 }
1010
1011 $gift_card = new YITH_YWGC_Gift_Card( array( 'ID' => $post_id ) );
1012
1013 if ( ! $gift_card->exists() ) {
1014 return $class;
1015 }
1016
1017 $classes[] = $gift_card->status;
1018
1019 return apply_filters( 'yith_gift_cards_table_class', $classes, $post_id );
1020 }
1021
1022
1023 /**
1024 * Make some redirect based on the current action being performed
1025 *
1026 * @author Lorenzo Giuffrida
1027 * @since 1.0.0
1028 */
1029 public function redirect_gift_cards_link() {
1030
1031 /**
1032 * Check if the user ask for retrying sending the gift card email that are not shipped yet
1033 */
1034 if ( isset( $_GET[ YWGC_ACTION_RETRY_SENDING ] ) ) {
1035
1036 $gift_card_id = $_GET['id'];
1037
1038 YITH_YWGC_Emails::get_instance()->send_gift_card_email( $gift_card_id, false );
1039 $redirect_url = remove_query_arg( array( YWGC_ACTION_RETRY_SENDING, 'id' ) );
1040
1041 wp_redirect( $redirect_url );
1042 exit;
1043 }
1044
1045 /**
1046 * Check if the user ask for enabling/disabling a specific gift cards
1047 */
1048 if ( isset( $_GET[ YWGC_ACTION_ENABLE_CARD ] ) || isset( $_GET[ YWGC_ACTION_DISABLE_CARD ] ) ) {
1049
1050 $gift_card_id = $_GET['id'];
1051 $enabled = isset( $_GET[ YWGC_ACTION_ENABLE_CARD ] );
1052
1053 $gift_card = new YITH_YWGC_Gift_Card( array( 'ID' => $gift_card_id ) );
1054
1055 if ( ! $gift_card->is_dismissed() ) {
1056
1057 $current_status = $gift_card->is_enabled();
1058
1059 if ( $current_status != $enabled ) {
1060
1061 $gift_card->set_enabled_status( $enabled );
1062 do_action( 'yith_gift_cards_status_changed', $gift_card, $enabled );
1063 }
1064
1065 wp_redirect( remove_query_arg( array(
1066 YWGC_ACTION_ENABLE_CARD,
1067 YWGC_ACTION_DISABLE_CARD,
1068 'id'
1069 ) ) );
1070 die();
1071 }
1072 }
1073
1074 if ( ! isset( $_GET["post_type"] ) || ! isset( $_GET["s"] ) ) {
1075 return;
1076 }
1077
1078
1079 if ( 'shop_coupon' != ( $_GET["post_type"] ) ) {
1080 return;
1081 }
1082
1083 if ( preg_match( "/(\w{4}-\w{4}-\w{4}-\w{4})(.*)/i", $_GET["s"], $matches ) ) {
1084 wp_redirect( admin_url( 'edit.php?s=' . $matches[1] . '&post_type=gift_card' ) );
1085 die();
1086 }
1087 }
1088
1089
1090 public function show_sold_individually_for_gift_cards() {
1091 ?>
1092 <script>
1093 jQuery("#_sold_individually").closest(".options_group").addClass("show_if_gift-card");
1094 jQuery("#_sold_individually").closest(".form-field").addClass("show_if_gift-card");
1095 </script>
1096 <?php
1097 }
1098
1099 /**
1100 * Show advanced product settings
1101 *
1102 * @param int $thepostid
1103 */
1104 public function show_advanced_product_settings( $thepostid ) {
1105
1106 }
1107
1108 /**
1109 * Add filters on the Gift Card Post Type page
1110 *
1111 * @param $views
1112 *
1113 * @return mixed
1114 */
1115
1116 public function add_gift_cards_filters( $views ) {
1117 global $wpdb;
1118 $args = array(
1119 'post_status' => 'published',
1120 'post_type' => 'gift_card',
1121 'balance' => 'active'
1122 );
1123
1124 $count_active = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT( post_id ) ) FROM {$wpdb->postmeta} AS pm LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id WHERE meta_key = %s AND meta_value <> 0 AND p.post_type= %s", '_ywgc_balance_total', 'gift_card' ) );
1125 $count_used = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT( post_id ) ) FROM {$wpdb->postmeta} AS pm LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id WHERE meta_key = %s AND ROUND(meta_value, %d) = 0 AND p.post_type= %s", '_ywgc_balance_total', wc_get_price_decimals(), 'gift_card' ) );
1126
1127 $views['active'] = sprintf( '<a href="%s">%s <span class="count">(%d)</span></a>', add_query_arg( $args, admin_url( 'edit.php' ) ), esc_html__( 'Active', 'yith-woocommerce-gift-cards' ), $count_active );
1128 $args['balance'] = 'used';
1129 $views['used'] = sprintf( '<a href="%s">%s <span class="count">(%d)</span></a>', add_query_arg( $args, admin_url( 'edit.php' ) ), esc_html__( 'Used', 'yith-woocommerce-gift-cards' ), $count_used );
1130
1131 return $views;
1132 }
1133
1134
1135 /**
1136 * Add filters on the Gift Card Post Type page
1137 *
1138 * @param $query
1139 */
1140
1141 public function filter_gift_card_page_query( $query ) {
1142 global $pagenow, $post_type;
1143
1144 if ( $pagenow == 'edit.php' && $post_type == 'gift_card' && isset( $_GET['balance'] ) && in_array( $_GET['balance'], array(
1145 'used',
1146 'active'
1147 ) ) ) {
1148 if ( 'active' == $_GET['balance'] ) {
1149 $meta_query = array(
1150 array(
1151 'key' => '_ywgc_balance_total',
1152 'value' => 0,
1153 'compare' => '>'
1154 )
1155 );
1156 } else {
1157 $meta_query = array(
1158 array(
1159 'key' => '_ywgc_balance_total',
1160 'value' => pow( 10, - wc_get_price_decimals() ),
1161 'compare' => '<'
1162 )
1163 );
1164 }
1165
1166 $query->set( 'meta_query', $meta_query );
1167 }
1168 }
1169
1170
1171 /**
1172 * Localize order item meta and show theme as strings
1173 *
1174 * @param $display_key
1175 * @param $meta
1176 * @param $order_item
1177 * @return string|void
1178 */
1179 public function show_as_string_order_item_meta_key($display_key){
1180 if( strpos($display_key,'ywgc') !== false){
1181 if( $display_key == '_ywgc_product_id' ){
1182 $display_key = esc_html__('Product ID','yith-woocommerce-gift-card');
1183 }
1184
1185 elseif( $display_key == '_ywgc_amount' ){
1186 $display_key = esc_html__('Amount','yith-woocommerce-gift-card');
1187 }
1188 elseif( $display_key == '_ywgc_is_digital' ){
1189 $display_key = esc_html__('Digital','yith-woocommerce-gift-card');
1190 }
1191 elseif( $display_key == '_ywgc_sender_name' ){
1192 $display_key = esc_html__('Sender\'s name','yith-woocommerce-gift-card');
1193 }
1194 elseif( $display_key == '_ywgc_recipient_name' ){
1195 $display_key = esc_html__('Recipient\'s name','yith-woocommerce-gift-card');
1196 }
1197 elseif( $display_key == '_ywgc_message' ){
1198 $display_key = esc_html__('Message','yith-woocommerce-gift-card');
1199 }
1200 elseif( $display_key == '_ywgc_design_type' ){
1201 $display_key = esc_html__('Design type','yith-woocommerce-gift-card');
1202 }
1203 elseif( $display_key == '_ywgc_design' ){
1204 $display_key = esc_html__('Design','yith-woocommerce-gift-card');
1205 }
1206 elseif( $display_key == '_ywgc_subtotal' ){
1207 $display_key = esc_html__('Subtotal','yith-woocommerce-gift-card');
1208 }
1209 elseif( $display_key == '_ywgc_subtotal_tax' ){
1210 $display_key = esc_html__('Subtotal tax','yith-woocommerce-gift-card');
1211 }
1212 elseif( $display_key == '_ywgc_version' ){
1213 $display_key = esc_html__('Version','yith-woocommerce-gift-card');
1214 }
1215 elseif( $display_key == '_ywgc_delivery_date' ){
1216 $display_key = esc_html__('Delivery date','yith-woocommerce-gift-card');
1217 }
1218 elseif( $display_key == '_ywgc_postdated' ){
1219 $display_key = esc_html__('Postdated','yith-woocommerce-gift-card');
1220 }
1221
1222
1223 }
1224 return $display_key;
1225 }
1226
1227 /**
1228 * Format date to show as meta value in order page
1229 * @param $meta_value
1230 * @param $meta
1231 * @return mixed
1232 */
1233 public function show_formatted_date( $meta_value, $meta ="", $item="" ){
1234
1235 if( '_ywgc_delivery_date' == $meta->key ){
1236 $date_format = apply_filters( 'yith_wcgc_date_format','Y-m-d' );
1237 $meta_value = date_i18n( $date_format,$meta_value ) . ' (' . $date_format . ')';
1238 }
1239
1240 return $meta_value;
1241
1242 }
1243
1244 /**
1245 * Update gift card amount in case the order is cancelled or refunded
1246 * @param $order_id
1247 * @param $from_status
1248 * @param $to_status
1249 * @param bool $order
1250 */
1251 public function update_gift_card_amount_on_order_status_change( $order_id, $from_status, $to_status, $order = false ){
1252 $is_gift_card_amount_refunded = yit_get_prop($order,'_ywgc_is_gift_card_amount_refunded');
1253 if( ($to_status == 'cancelled' || ( $to_status == 'refunded' ) || ( $to_status == 'failed' )) && $is_gift_card_amount_refunded != 'yes' ){
1254 $gift_card_applied = yit_get_prop( $order,'_ywgc_applied_gift_cards',true );
1255 if (empty($gift_card_applied)) {
1256 return;
1257 }
1258
1259 foreach ($gift_card_applied as $gift_card_code => $gift_card_value ){
1260 $args = array(
1261 'gift_card_number' => $gift_card_code
1262 );
1263 $gift_card = new YITH_YWGC_Gift_Card( $args );
1264 $new_amount = $gift_card->get_balance() + $gift_card_value;
1265 $gift_card->update_balance( $new_amount );
1266 }
1267
1268 yit_save_prop($order,'_ywgc_is_gift_card_amount_refunded','yes');
1269 }
1270 }
1271
1272
1273 public function update_totals_on_save_order_items( $order_id, $items ){
1274
1275 if( isset( $items['order_status'] ) && $items[ 'order_status' ] == 'wc-refunded' )
1276 return;
1277
1278 $order = wc_get_order( $order_id );
1279
1280 $used_gift_cards = get_post_meta( $order_id, '_ywgc_applied_gift_cards', true);
1281
1282 if (! $used_gift_cards )
1283 return;
1284
1285 $cart_subtotal = 0;
1286 $cart_total = 0;
1287 $fee_total = 0;
1288 $cart_subtotal_tax = 0;
1289 $cart_total_tax = 0;
1290
1291 $and_taxes = yit_get_prop( $order,'prices_include_tax' );
1292
1293 if ( $and_taxes && apply_filters('yith_ywgc_update_totals_calculate_taxes',true) ) {
1294 $order->calculate_taxes();
1295 }
1296
1297 // line items
1298 foreach ( $order->get_items() as $item ) {
1299 $cart_subtotal += $item->get_subtotal();
1300 $cart_total += $item->get_total();
1301 $cart_subtotal_tax += $item->get_subtotal_tax();
1302 $cart_total_tax += $item->get_total_tax();
1303 }
1304
1305 $applied_gift_card_amount = yit_get_prop( $order,'_ywgc_applied_gift_cards_totals' );
1306
1307 if ( !empty($applied_gift_card_amount) ){
1308 $cart_total -= $applied_gift_card_amount;
1309 }
1310
1311 $order->calculate_shipping();
1312
1313 foreach ( $order->get_fees() as $item ) {
1314 $fee_total += $item->get_total();
1315 }
1316
1317 $grand_total = round( $cart_total + $fee_total + $order->get_shipping_total() + $order->get_cart_tax() + $order->get_shipping_tax(), wc_get_price_decimals() );
1318
1319 $order->set_discount_total( $cart_subtotal - $cart_total );
1320 $order->set_discount_tax( $cart_subtotal_tax - $cart_total_tax );
1321 $order->set_total( $grand_total );
1322 $order->save();
1323
1324 }
1325
1326
1327 public function ywgc_edit_design_category_form() {
1328 global $current_screen;
1329
1330 if ( $current_screen->id == 'edit-giftcard-category' ){
1331 ?>
1332 <div>
1333 <h2><?php echo esc_html__('Manage the gift card images through the WordPress Media Library', 'yith-woocommerce-gift-cards');?></h2>
1334 <br>
1335 <button id="ywgc-media-upload-button" class="button" style="padding: 3px 15px;"><span class="dashicons dashicons-admin-media"></span><?php echo ' ' . esc_html__('Manage media', 'yith-woocommerce-gift-cards'); ?></button>
1336 <p><?php echo esc_html__('Upload/manage images in the WordPress Media Library and include them in the existing gift card categories.', 'yith-woocommerce-gift-cards'); ?></p>
1337 </div>
1338 <?php
1339 }
1340 }
1341
1342 function ywgc_remove_product_meta_boxes(){
1343
1344 $product = wc_get_product(get_the_ID());
1345
1346 if (is_object($product) && $product->get_type() == 'gift-card' ) {
1347 remove_meta_box('woocommerce-product-images', 'product', 'side');
1348 }
1349 if (is_object($product) && $product->get_type() != 'gift-card' ) {
1350 remove_meta_box('giftcard-categorydiv', 'product', 'side');
1351 }
1352 }
1353
1354
1355 public function set_gift_card_category_to_product( $post_id ) {
1356
1357 // Skip all request without an action
1358 if ( ! isset( $_REQUEST['action'] ) && ! isset( $_REQUEST['action2'] ) ) {
1359 return;
1360 }
1361
1362 // Skip all request without a valid action
1363 if ( ( '-1' == $_REQUEST['action'] ) && ( '-1' == $_REQUEST['action2'] ) ) {
1364 return;
1365 }
1366
1367 $selected_catergories = isset( $_REQUEST['tax_input']['giftcard-category'] ) ? $_REQUEST['tax_input']['giftcard-category'] : array();
1368
1369 $selected_catergories_serialized = serialize($selected_catergories);
1370
1371 update_post_meta( $post_id, 'selected_images_categories', $selected_catergories_serialized );
1372 }
1373
1374
1375
1376
1377 }
1378}
1379