| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\Formatter; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 5 |
use WC_Product; |
| 6 |
use WP_Term; |
| 7 |
|
| 8 |
class AppliedProducts { |
| 9 |
|
| 10 |
public function getName(): string { |
| 11 |
return __( 'Products', 'tier-pricing-table' ); |
| 12 |
} |
| 13 |
|
| 14 |
public function render( GlobalPricingRule $rule ) { |
| 15 |
|
| 16 |
$hasProducts = $this->showProducts( $rule->getIncludedProducts() ); |
| 17 |
$hasCategories = $this->showCategories( $rule->getIncludedProductCategories() ); |
| 18 |
|
| 19 |
if ( ! $hasProducts && ! $hasCategories ) { |
| 20 |
?> |
| 21 |
<mark class="order-status status-processing tips"> |
| 22 |
<span> |
| 23 |
<?php esc_html_e( 'Applied to every product', 'tier-pricing-table' ); ?> |
| 24 |
</span> |
| 25 |
</mark> |
| 26 |
<?php |
| 27 |
} else { |
| 28 |
$this->showProducts( $rule->getExcludedProducts(), false ); |
| 29 |
$this->showCategories( $rule->getExcludedProductCategories(), false ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public function showProducts( array $productsIds, $included = true ): bool { |
| 34 |
|
| 35 |
$moreThanCanBeShown = count( $productsIds ) > 10; |
| 36 |
|
| 37 |
$productsIds = array_slice( $productsIds, 0, 5 ); |
| 38 |
|
| 39 |
$products = array_filter( array_map( function ( $productId ) { |
| 40 |
return wc_get_product( $productId ); |
| 41 |
}, $productsIds ) ); |
| 42 |
|
| 43 |
if ( ! empty( $products ) ) { |
| 44 |
|
| 45 |
if ( $included ) { |
| 46 |
esc_html_e( 'Products: ', 'tier-pricing-table' ); |
| 47 |
} else { |
| 48 |
esc_html_e( 'Excluded products: ', 'tier-pricing-table' ); |
| 49 |
} |
| 50 |
|
| 51 |
$productsString = array_map( function ( WC_Product $product ) { |
| 52 |
return sprintf( '<a href="%s" target="_blank">%s</a>', |
| 53 |
get_edit_post_link( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ), |
| 54 |
$product->get_name() ); |
| 55 |
}, $products ); |
| 56 |
|
| 57 |
echo wp_kses_post( implode( ', ', $productsString ) . ( $moreThanCanBeShown ? '<span> ...</span>' : '' ) ); |
| 58 |
|
| 59 |
echo '<br><br>'; |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
public function showCategories( array $categoriesIds, $included = true ): bool { |
| 68 |
$moreThanCanBeShown = count( $categoriesIds ) > 10; |
| 69 |
$categoriesIds = array_slice( $categoriesIds, 0, 10 ); |
| 70 |
|
| 71 |
$categories = array_filter( array_map( function ( $categoryId ) { |
| 72 |
return get_term( $categoryId ); |
| 73 |
}, $categoriesIds ) ); |
| 74 |
|
| 75 |
$categories = array_filter( $categories, function ( $category ) { |
| 76 |
return $category instanceof WP_Term; |
| 77 |
} ); |
| 78 |
|
| 79 |
if ( ! empty( $categories ) ) { |
| 80 |
|
| 81 |
if ( $included ) { |
| 82 |
esc_html_e( 'Categories: ', 'tier-pricing-table' ); |
| 83 |
} else { |
| 84 |
esc_html_e( 'Excluded categories: ', 'tier-pricing-table' ); |
| 85 |
} |
| 86 |
|
| 87 |
$categoriesString = array_map( function ( WP_Term $category ) { |
| 88 |
return sprintf( '<a href="%s" target="_blank">%s</a>', get_edit_term_link( $category->term_id ), |
| 89 |
$category->name ); |
| 90 |
}, $categories ); |
| 91 |
|
| 92 |
echo wp_kses_post( implode( ', ', |
| 93 |
$categoriesString ) . ( $moreThanCanBeShown ? '<span> ...</span>' : '' ) ); |
| 94 |
|
| 95 |
echo '<br><br>'; |
| 96 |
|
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
} |
| 103 |
|