| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns; |
| 2 |
|
| 3 |
use ArrayIterator; |
| 4 |
use Exception; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 6 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 7 |
|
| 8 |
class Pricing { |
| 9 |
|
| 10 |
use ServiceContainerTrait; |
| 11 |
|
| 12 |
public function getName(): string { |
| 13 |
return __( 'Pricing', 'tier-pricing-table' ); |
| 14 |
} |
| 15 |
|
| 16 |
public function render( GlobalPricingRule $rule ) { |
| 17 |
try { |
| 18 |
$rule->validatePricing(); |
| 19 |
|
| 20 |
$pricingType = $rule->getTieredPricingType(); |
| 21 |
$rules = 'percentage' === $pricingType ? $rule->getPercentageTieredPricingRules() : $rule->getFixedTieredPricingRules(); |
| 22 |
$minimum = $rule->getMinimum() ? intval( $rule->getMinimum() ) : 1; |
| 23 |
|
| 24 |
$regularProductPriceString = __( 'Regular price', 'tier-pricing-table' ); |
| 25 |
|
| 26 |
if ( $rule->getPricingType() === 'flat' ) { |
| 27 |
if ( $rule->getSalePrice() ) { |
| 28 |
$regularProductPriceString = wc_price( $rule->getSalePrice() ); |
| 29 |
} elseif ( $rule->getRegularPrice() ) { |
| 30 |
$regularProductPriceString = wc_price( $rule->getRegularPrice() ); |
| 31 |
} |
| 32 |
} elseif ( $rule->getDiscount() ) { |
| 33 |
$regularProductPriceString = $rule->getDiscount() . '% off'; |
| 34 |
} |
| 35 |
|
| 36 |
?> |
| 37 |
|
| 38 |
<?php if ( $rule->getPricingType() === 'flat' ) : ?> |
| 39 |
<?php if ( $rule->getRegularPrice() ) : ?> |
| 40 |
<p> |
| 41 |
<?php |
| 42 |
echo wp_kses_post( __( 'Regular Price', |
| 43 |
'tier-pricing-table' ) . ': <b>' . wc_price( $rule->getRegularPrice() ) . '</b>' ); |
| 44 |
?> |
| 45 |
</p> |
| 46 |
<?php endif; ?> |
| 47 |
|
| 48 |
<?php if ( $rule->getSalePrice() ) : ?> |
| 49 |
<p> |
| 50 |
<?php |
| 51 |
echo wp_kses_post( __( 'Sale Price', |
| 52 |
'tier-pricing-table' ) . ': <b>' . wc_price( $rule->getSalePrice() ) . '</b>' ); |
| 53 |
?> |
| 54 |
</p> |
| 55 |
<?php endif; ?> |
| 56 |
<?php else : ?> |
| 57 |
<?php if ( $rule->getDiscount() ) : ?> |
| 58 |
<p> |
| 59 |
<?php esc_html_e( 'Discount', 'tier-pricing-table' ); ?> |
| 60 |
: |
| 61 |
<b><?php echo esc_html( $rule->getDiscount() ); ?>%</b> |
| 62 |
<small>( |
| 63 |
<?php |
| 64 |
$rule->getDiscountType() === 'sale_price' ? esc_html_e( 'Sale price', |
| 65 |
'tier-pricing-table' ) : esc_html_e( 'Regular price', 'tier-pricing-table' ); |
| 66 |
?> |
| 67 |
)</small> |
| 68 |
</p> |
| 69 |
<?php endif; ?> |
| 70 |
<?php endif; ?> |
| 71 |
|
| 72 |
<?php |
| 73 |
if ( empty( $rules ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
?> |
| 77 |
<table class="wp-list-table widefat fixed striped table-view-list tpt-global-rule-pricing-table"> |
| 78 |
<thead> |
| 79 |
<tr> |
| 80 |
<th> |
| 81 |
<b><?php esc_html_e( 'Quantity', 'tier-pricing-table' ); ?></b></th> |
| 82 |
<th> |
| 83 |
<b> |
| 84 |
<?php if ( 'percentage' === $pricingType ) : ?> |
| 85 |
<?php esc_html_e( 'Discount', 'tier-pricing-table' ); ?> |
| 86 |
<?php else : ?> |
| 87 |
<?php esc_html_e( 'Price', 'tier-pricing-table' ); ?> |
| 88 |
<?php endif; ?> |
| 89 |
</b> |
| 90 |
</th> |
| 91 |
</tr> |
| 92 |
</thead> |
| 93 |
<tbody> |
| 94 |
<tr> |
| 95 |
<td> |
| 96 |
<?php if ( 1 >= array_keys( $rules )[0] - $minimum ) : ?> |
| 97 |
<span><?php echo esc_attr( number_format_i18n( $minimum ) ); ?></span> |
| 98 |
<?php else : ?> |
| 99 |
<span><?php echo esc_attr( number_format_i18n( $minimum ) ); ?> - <?php echo esc_attr( number_format_i18n( array_keys( $rules )[0] - 1 ) ); ?></span> |
| 100 |
<?php endif; ?> |
| 101 |
</td> |
| 102 |
|
| 103 |
<td> |
| 104 |
<?php if ( 'percentage' === $pricingType ) : ?> |
| 105 |
<?php echo wp_kses_post( $regularProductPriceString ); ?> |
| 106 |
<?php else : ?> |
| 107 |
<?php echo wp_kses_post( $regularProductPriceString ); ?> |
| 108 |
<?php endif; ?> |
| 109 |
</td> |
| 110 |
</tr> |
| 111 |
|
| 112 |
<?php $iterator = new ArrayIterator( $rules ); ?> |
| 113 |
|
| 114 |
<?php while ( $iterator->valid() ) : ?> |
| 115 |
<?php |
| 116 |
$currentPrice = $iterator->current(); |
| 117 |
$currentQuantity = $iterator->key(); |
| 118 |
|
| 119 |
$iterator->next(); |
| 120 |
|
| 121 |
if ( $iterator->valid() ) { |
| 122 |
$quantity = $currentQuantity; |
| 123 |
|
| 124 |
if ( intval( $iterator->key() - 1 != $currentQuantity ) ) { |
| 125 |
|
| 126 |
$quantity = number_format_i18n( $quantity ); |
| 127 |
|
| 128 |
if ( $this->getContainer()->getSettings()->get( 'quantity_type', 'range' ) === 'range' ) { |
| 129 |
$quantity .= ' - ' . number_format_i18n( intval( $iterator->key() - 1 ) ); |
| 130 |
} |
| 131 |
} |
| 132 |
} else { |
| 133 |
$quantity = number_format_i18n( $currentQuantity ) . '+'; |
| 134 |
} |
| 135 |
?> |
| 136 |
<tr> |
| 137 |
<td> |
| 138 |
<?php echo esc_attr( $quantity ); ?> |
| 139 |
</td> |
| 140 |
|
| 141 |
<td> |
| 142 |
|
| 143 |
<?php if ( 'percentage' === $pricingType ) : ?> |
| 144 |
<?php echo esc_html( $currentPrice . '%' ); ?> |
| 145 |
|
| 146 |
<?php else : ?> |
| 147 |
<?php echo wp_kses_post( wc_price( $currentPrice ) ); ?> |
| 148 |
|
| 149 |
<?php endif; ?> |
| 150 |
|
| 151 |
</td> |
| 152 |
</tr> |
| 153 |
<?php endwhile; ?> |
| 154 |
|
| 155 |
|
| 156 |
</tbody> |
| 157 |
</table> |
| 158 |
<h4 style="margin-top: 10px;"> |
| 159 |
<?php |
| 160 |
$applyingType = $rule->getApplyingType() === 'individual' ? __( 'Applied individually per product', |
| 161 |
'tier-pricing-table' ) : __( 'Applied as Mix and Match', 'tier-pricing-table' ); |
| 162 |
echo esc_html( $applyingType ); |
| 163 |
?> |
| 164 |
</h4> |
| 165 |
<?php |
| 166 |
|
| 167 |
} catch ( Exception $e ) { |
| 168 |
echo wp_kses_post( '<div class="help_tip tpt-rule-status tpt-rule-status--invalid" data-tip="' . $e->getMessage() . '">!</div>' ); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|