| 1 |
<?php namespace TierPricingTable\Addons\CustomColumns\Columns; |
| 2 |
|
| 3 |
use TierPricingTable\PricingRule; |
| 4 |
|
| 5 |
class SavingPerItemColumn extends AbstractCustomColumn { |
| 6 |
|
| 7 |
const TYPE = 'saving_per_item'; |
| 8 |
|
| 9 |
public function getType(): string { |
| 10 |
return self::TYPE; |
| 11 |
} |
| 12 |
|
| 13 |
public function getDataType(): string { |
| 14 |
return 'price'; |
| 15 |
} |
| 16 |
|
| 17 |
protected function _getSingleRowValue( PricingRule $pricingRule, $currentTierQuantity = null ): string { |
| 18 |
$product = wc_get_product( $pricingRule->getProductId() ); |
| 19 |
|
| 20 |
if ( $currentTierQuantity && $product ) { |
| 21 |
$tierPrice = $pricingRule->getTierPrice( $currentTierQuantity, false ); |
| 22 |
$regularPrice = (float) $product->get_price(); |
| 23 |
|
| 24 |
if ( $tierPrice && $regularPrice && $regularPrice > $tierPrice ) { |
| 25 |
$saving = $regularPrice - $tierPrice; |
| 26 |
$priceHtml = wc_price( wc_get_price_to_display( $product, array( |
| 27 |
'price' => $saving, |
| 28 |
) ) ); |
| 29 |
|
| 30 |
$unitName = get_post_meta( $product->get_id(), '_tiered_pricing_base_unit_name', true ); |
| 31 |
$unitLabel = ''; |
| 32 |
|
| 33 |
if ( ! empty( $unitName['singular'] ) ) { |
| 34 |
$unitLabel = $unitName['singular']; |
| 35 |
} else { |
| 36 |
$quantity_measurement = \TierPricingTable\Core\ServiceContainer::getInstance()->getSettings()->get( 'table_quantity_measurement', array( |
| 37 |
'singular' => '', |
| 38 |
'plural' => '', |
| 39 |
) ); |
| 40 |
if ( ! empty( $quantity_measurement['singular'] ) ) { |
| 41 |
$unitLabel = $quantity_measurement['singular']; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ( $unitLabel ) { |
| 46 |
$priceHtml .= '<span> / ' . $unitLabel . '</span>'; |
| 47 |
} |
| 48 |
|
| 49 |
return $priceHtml; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return '-'; |
| 54 |
} |
| 55 |
} |
| 56 |
|