Code Duplication    Length = 18-18 lines in 3 locations

includes/class-wc-coupon.php 3 locations

@@ 439-456 (lines=18) @@
436
	 *
437
	 * @throws Exception
438
	 */
439
	private function validate_product_categories() {
440
		if ( sizeof( $this->product_categories ) > 0 ) {
441
			$valid_for_cart = false;
442
			if ( ! WC()->cart->is_empty() ) {
443
				foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
444
					$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
445
446
					// If we find an item with a cat in our allowed cat list, the coupon is valid
447
					if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) {
448
						$valid_for_cart = true;
449
					}
450
				}
451
			}
452
			if ( ! $valid_for_cart ) {
453
				throw new Exception( self::E_WC_COUPON_NOT_APPLICABLE );
454
			}
455
		}
456
	}
457
458
	/**
459
	 * Ensure coupon is valid for product categories in the cart is valid or throw exception.
@@ 463-480 (lines=18) @@
460
	 *
461
	 * @throws Exception
462
	 */
463
	private function validate_excluded_product_categories() {
464
		if ( sizeof( $this->exclude_product_categories ) > 0 ) {
465
			$valid_for_cart = false;
466
			if ( ! WC()->cart->is_empty() ) {
467
				foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
468
					$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
469
470
					// If we find an item with a cat NOT in our disallowed cat list, the coupon is valid
471
					if ( empty( $product_cats ) || sizeof( array_diff( $product_cats, $this->exclude_product_categories ) ) > 0 ) {
472
						$valid_for_cart = true;
473
					}
474
				}
475
			}
476
			if ( ! $valid_for_cart ) {
477
				throw new Exception( self::E_WC_COUPON_NOT_APPLICABLE );
478
			}
479
		}
480
	}
481
482
	/**
483
	 * Ensure coupon is valid for sale items in the cart is valid or throw exception.
@@ 547-564 (lines=18) @@
544
	 *
545
	 * @throws Exception
546
	 */
547
	private function validate_cart_excluded_product_categories() {
548
		if ( sizeof( $this->exclude_product_categories ) > 0 ) {
549
			$valid_for_cart = true;
550
			if ( ! WC()->cart->is_empty() ) {
551
				foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
552
553
					$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
554
555
					if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 ) {
556
						$valid_for_cart = false;
557
					}
558
				}
559
			}
560
			if ( ! $valid_for_cart ) {
561
				throw new Exception( self::E_WC_COUPON_EXCLUDED_CATEGORIES );
562
			}
563
		}
564
	}
565
566
	/**
567
	 * Exclude sale items from cart.