| 1 |
<?php namespace TierPricingTable\Services; |
| 2 |
|
| 3 |
/* |
| 4 |
* Class DebugService |
| 5 |
* |
| 6 |
* @package TierPricingTable/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
use TierPricingTable\Core\ServiceContainer; |
| 10 |
use TierPricingTable\PriceManager; |
| 11 |
use TierPricingTable\PricingRule; |
| 12 |
|
| 13 |
class DebugService { |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
|
| 17 |
if ( ServiceContainer::getInstance()->getSettings()->get( 'debug_enabled', 'no' ) !== 'yes' ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
add_action( 'woocommerce_after_cart_item_name', function ( $cartItem ) { |
| 22 |
|
| 23 |
if ( ! ( $cartItem['data'] instanceof \WC_Product ) ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$pricingRule = PriceManager::getPricingRule( $cartItem['data']->get_id() ); |
| 28 |
|
| 29 |
$this->formatPricingRule( $pricingRule, $cartItem ); |
| 30 |
} ); |
| 31 |
|
| 32 |
add_action( 'tiered_pricing_table/before_rendering_tiered_pricing/inner', |
| 33 |
function ( PricingRule $pricingRule ) { |
| 34 |
$this->formatPricingRule( $pricingRule ); |
| 35 |
} ); |
| 36 |
} |
| 37 |
|
| 38 |
protected function getPricingRuleData( PricingRule $pricingRule, ?array $cartItem = null ): array { |
| 39 |
|
| 40 |
switch ( $pricingRule->provider ) { |
| 41 |
case 'global-rules': |
| 42 |
$globalRuleId = $pricingRule->providerData['rule_id'] ?? 'N\A'; |
| 43 |
$data['provider'] = sprintf( '<a href="%s">%s</a>', get_edit_post_link( $globalRuleId ), |
| 44 |
"Global rule ($globalRuleId)" ); |
| 45 |
break; |
| 46 |
case 'category-rules': |
| 47 |
$categoryId = $pricingRule->providerData['category_id'] ?? 'N\A'; |
| 48 |
$data['provider'] = sprintf( '<a href="%s">%s</a>', get_edit_term_link( $categoryId ), |
| 49 |
"DEPRECATED Category rule ($categoryId)" ); |
| 50 |
break; |
| 51 |
case 'role-based': |
| 52 |
$role = $pricingRule->providerData['role'] ?? 'N\A'; |
| 53 |
$data['provider'] = "Role-based rule ($role)"; |
| 54 |
break; |
| 55 |
case 'product': |
| 56 |
$data['provider'] = 'Product level rule'; |
| 57 |
break; |
| 58 |
default: |
| 59 |
$provider = $pricingRule->provider ?? 'N\A'; |
| 60 |
$data['provider'] = "Undefined provider ($provider)"; |
| 61 |
} |
| 62 |
|
| 63 |
$data['minimum'] = $pricingRule->getMinimum() ? $pricingRule->getMinimum() : 'N/A'; |
| 64 |
$data['minimum_mix_and_match'] = $pricingRule->isMixAndMatchMinQuantity() ? 'Yes' : 'No'; |
| 65 |
$data['maximum'] = ! empty( $pricingRule->data['maximum_quantity'] ) ? $pricingRule->data['maximum_quantity'] : 'N/A'; |
| 66 |
$data['quantity_step'] = ! empty( $pricingRule->data['group_of_quantity'] ) ? $pricingRule->data['group_of_quantity'] : 'N/A'; |
| 67 |
|
| 68 |
$data['pricing_type'] = $pricingRule->getType(); |
| 69 |
$data['rules'] = '-'; |
| 70 |
|
| 71 |
if ( ! empty( $pricingRule->getRules() ) ) { |
| 72 |
$data['rules'] = ''; |
| 73 |
|
| 74 |
foreach ( $pricingRule->getRules() as $quantity => $price ) { |
| 75 |
$data['rules'] .= $quantity . ':' . $price . ','; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ( $cartItem ) { |
| 80 |
$data['total_in_cart'] = ! empty( $cartItem['tiered_pricing_data']['total_item_quantity'] ) ? $cartItem['tiered_pricing_data']['total_item_quantity'] : 'undefined'; |
| 81 |
} else { |
| 82 |
$data['total_in_cart'] = 'N\A'; |
| 83 |
} |
| 84 |
|
| 85 |
$data['custom_regular_price'] = ! empty( $pricingRule->pricingData['regular_price'] ) ? $pricingRule->pricingData['regular_price'] : 'N\A'; |
| 86 |
$data['custom_sale_price'] = ! empty( $pricingRule->pricingData['sale_price'] ) ? $pricingRule->pricingData['sale_price'] : 'N\A'; |
| 87 |
$data['discount'] = ! empty( $pricingRule->pricingData['discount'] ) ? $pricingRule->pricingData['discount'] : 'N\A'; |
| 88 |
$data['custom_pricing_type'] = ! empty( $pricingRule->pricingData['pricing_type'] ) ? $pricingRule->pricingData['pricing_type'] : 'N\A'; |
| 89 |
|
| 90 |
return $data; |
| 91 |
} |
| 92 |
|
| 93 |
protected function formatPricingRule( PricingRule $pricingRule, ?array $cartItem = null ) { |
| 94 |
|
| 95 |
$data = $this->getPricingRuleData( $pricingRule, $cartItem ); |
| 96 |
?> |
| 97 |
|
| 98 |
<div> |
| 99 |
<table style="font-size: .7em"> |
| 100 |
<thead> |
| 101 |
<tr> |
| 102 |
<th colspan="2" style="padding: 10px">Debug info</th> |
| 103 |
</tr> |
| 104 |
</thead> |
| 105 |
<tbody> |
| 106 |
<?php foreach ( $data as $key => $value ) : ?> |
| 107 |
<tr> |
| 108 |
<td style="padding: 0"> |
| 109 |
<?php echo esc_html( $key . ':' ); ?> |
| 110 |
</td> |
| 111 |
<td style="padding: 0"> |
| 112 |
<?php echo wp_kses_post( $value ); ?> |
| 113 |
</td> |
| 114 |
</tr> |
| 115 |
<?php endforeach; ?> |
| 116 |
</tbody> |
| 117 |
</table> |
| 118 |
</div> |
| 119 |
|
| 120 |
<?php if ( ! empty( $pricingRule->getPricingLog() ) ) : ?> |
| 121 |
<small> |
| 122 |
Pricing Log |
| 123 |
<ol> |
| 124 |
<?php foreach ( $pricingRule->getPricingLog() as $log ) : ?> |
| 125 |
<li><?php echo $log; ?></> |
| 126 |
<?php endforeach; ?> |
| 127 |
</ol> |
| 128 |
</small> |
| 129 |
<?php endif; ?> |
| 130 |
|
| 131 |
<?php |
| 132 |
} |
| 133 |
} |