| 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 |
$hasProducts = $this->showProducts( $rule->getIncludedProducts() ); |
| 16 |
$hasCategories = $this->showTerms( $rule->getIncludedProductCategories(), __( 'Categories', 'tier-pricing-table' ) ); |
| 17 |
$hasTags = $this->showTerms( $rule->getIncludedProductTags(), __( 'Tags', 'tier-pricing-table' ) ); |
| 18 |
$hasBrands = $this->showTerms( $rule->getIncludedProductBrands(), __( 'Brands', 'tier-pricing-table' ) ); |
| 19 |
|
| 20 |
$excludedProducts = $rule->getExcludedProducts(); |
| 21 |
$excludedCategories = $rule->getExcludedProductCategories(); |
| 22 |
$excludedTags = $rule->getExcludedProductTags(); |
| 23 |
$excludedBrands = $rule->getExcludedProductBrands(); |
| 24 |
$hasExceptions = ! empty( $excludedProducts ) || ! empty( $excludedCategories ) || ! empty( $excludedTags ) || ! empty( $excludedBrands ); |
| 25 |
|
| 26 |
if ( ! $hasProducts && ! $hasCategories && ! $hasTags && ! $hasBrands ) { |
| 27 |
$badgeText = $hasExceptions ? __( 'Applied to every product', 'tier-pricing-table' ) : __( 'Applied to every product', 'tier-pricing-table' ); |
| 28 |
?> |
| 29 |
<span style="display: inline-block; background: #e0f0fa; color: #0070bc; border: 1px solid #bae0ff; padding: 4px 10px; border-radius: 4px; font-size: 13px; font-weight: 500; margin-bottom: 12px; line-height: 1.4;"> |
| 30 |
<?php echo esc_html( $badgeText ); ?> |
| 31 |
</span> |
| 32 |
<?php |
| 33 |
} |
| 34 |
|
| 35 |
$this->showProducts( $excludedProducts, false ); |
| 36 |
$this->showTerms( $excludedCategories, __( 'Excluded Categories', 'tier-pricing-table' ) ); |
| 37 |
$this->showTerms( $excludedTags, __( 'Excluded Tags', 'tier-pricing-table' ) ); |
| 38 |
$this->showTerms( $excludedBrands, __( 'Excluded Brands', 'tier-pricing-table' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
public function showProducts( array $productsIds, $included = true ): bool { |
| 42 |
|
| 43 |
$moreThanCanBeShown = count( $productsIds ) > 10; |
| 44 |
|
| 45 |
$productsIds = array_slice( $productsIds, 0, 5 ); |
| 46 |
|
| 47 |
$products = array_filter( array_map( function ( $productId ) { |
| 48 |
return wc_get_product( $productId ); |
| 49 |
}, $productsIds ) ); |
| 50 |
|
| 51 |
if ( ! empty( $products ) ) { |
| 52 |
$title = $included ? __( 'Products', 'tier-pricing-table' ) : __( 'Excluded Products', 'tier-pricing-table' ); |
| 53 |
|
| 54 |
echo '<div style="margin-bottom: 12px;">'; |
| 55 |
echo sprintf('<strong style="display: block; margin-bottom: 4px;">%s:</strong>', esc_html($title)); |
| 56 |
echo '<div style="display: flex; flex-wrap: wrap; gap: 4px;">'; |
| 57 |
|
| 58 |
foreach ($products as $product) { |
| 59 |
$link = get_edit_post_link( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ); |
| 60 |
echo sprintf( |
| 61 |
'<a href="%s" target="_blank" style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; text-decoration: none; font-size: 12px; color: #3c434a; border: 1px solid #dcdcdc; line-height: 1.4;">%s</a>', |
| 62 |
esc_url( $link ), |
| 63 |
esc_html( $product->get_name() ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
if ( $moreThanCanBeShown ) { |
| 68 |
echo '<span style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; font-size: 12px; color: #8c8f94; border: 1px solid #dcdcdc; line-height: 1.4;">...</span>'; |
| 69 |
} |
| 70 |
|
| 71 |
echo '</div></div>'; |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
public function showTerms( array $termsIds, string $title ): bool { |
| 80 |
$moreThanCanBeShown = count( $termsIds ) > 10; |
| 81 |
$termsIds = array_slice( $termsIds, 0, 10 ); |
| 82 |
|
| 83 |
$terms = array_filter( array_map( function ( $termId ) { |
| 84 |
return get_term( $termId ); |
| 85 |
}, $termsIds ) ); |
| 86 |
|
| 87 |
$terms = array_filter( $terms, function ( $term ) { |
| 88 |
return $term instanceof WP_Term; |
| 89 |
} ); |
| 90 |
|
| 91 |
if ( ! empty( $terms ) ) { |
| 92 |
|
| 93 |
echo '<div style="margin-bottom: 12px;">'; |
| 94 |
echo sprintf('<strong style="display: block; margin-bottom: 4px;">%s:</strong>', esc_html($title)); |
| 95 |
echo '<div style="display: flex; flex-wrap: wrap; gap: 4px;">'; |
| 96 |
|
| 97 |
foreach ($terms as $term) { |
| 98 |
$link = get_edit_term_link( $term->term_id ); |
| 99 |
$linkHtml = $link ? esc_url( $link ) : '#'; |
| 100 |
echo sprintf( |
| 101 |
'<a href="%s" target="_blank" style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; text-decoration: none; font-size: 12px; color: #3c434a; border: 1px solid #dcdcdc; line-height: 1.4;">%s</a>', |
| 102 |
$linkHtml, |
| 103 |
esc_html( $term->name ) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
if ( $moreThanCanBeShown ) { |
| 108 |
echo '<span style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; font-size: 12px; color: #8c8f94; border: 1px solid #dcdcdc; line-height: 1.4;">...</span>'; |
| 109 |
} |
| 110 |
|
| 111 |
echo '</div></div>'; |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|