| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Addons\ProductCatalogLoop; |
| 4 |
|
| 5 |
use TierPricingTable\Addons\AbstractAddon; |
| 6 |
use TierPricingTable\Addons\ProductCatalogLoop\Settings\ProductCatalogLoopSettingsSection; |
| 7 |
use WC_Product; |
| 8 |
class ProductCatalogLoop extends AbstractAddon { |
| 9 |
public function getName() : string { |
| 10 |
return __( 'Shop & Categories', 'tier-pricing-table' ); |
| 11 |
} |
| 12 |
|
| 13 |
public function getDescription() : string { |
| 14 |
return __( 'Display tiered pricing tables on the shop and category pages.', 'tier-pricing-table' ); |
| 15 |
} |
| 16 |
|
| 17 |
public function getIcon() : string { |
| 18 |
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 10h12v2H4zm0-4h16v2H4zm0 8h8v2H4zm10 0v6l5-3z"/></svg>'; |
| 19 |
} |
| 20 |
|
| 21 |
public function getSlug() : string { |
| 22 |
return 'shop-loop-display'; |
| 23 |
} |
| 24 |
|
| 25 |
public function getRenderAttributes() : array { |
| 26 |
if ( !ProductCatalogLoopSettingsSection::isCustomLayoutSettings() ) { |
| 27 |
return array( |
| 28 |
'display' => true, |
| 29 |
); |
| 30 |
} |
| 31 |
$args = array( |
| 32 |
'display_context' => 'shop-loop', |
| 33 |
'display' => true, |
| 34 |
'display_type' => ProductCatalogLoopSettingsSection::getLayoutType(), |
| 35 |
'title' => ProductCatalogLoopSettingsSection::getTitle(), |
| 36 |
'quantity_type' => ProductCatalogLoopSettingsSection::getQuantityType(), |
| 37 |
'getSelectedQuantityColor' => ProductCatalogLoopSettingsSection::getSelectedQuantityColor(), |
| 38 |
'quantity_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_quantity_text'], |
| 39 |
'price_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_price_text'], |
| 40 |
'discount_column_title' => ProductCatalogLoopSettingsSection::getTableColumnsTitles()['head_discount_text'], |
| 41 |
'show_discount_column' => ProductCatalogLoopSettingsSection::blocksShowDiscount(), |
| 42 |
'clickable_rows' => ProductCatalogLoopSettingsSection::isClickableTableRows(), |
| 43 |
'active_tier_color' => ProductCatalogLoopSettingsSection::getSelectedQuantityColor(), |
| 44 |
'options_show_original_product_price' => ProductCatalogLoopSettingsSection::isShowOriginalProductPriceInOptions(), |
| 45 |
'options_show_default_option' => ProductCatalogLoopSettingsSection::isShowDefaultOption(), |
| 46 |
'options_option_text' => ProductCatalogLoopSettingsSection::getOptionText(), |
| 47 |
'options_default_option_text' => ProductCatalogLoopSettingsSection::getDefaultOptionText(), |
| 48 |
'plain_text_show_default_option' => ProductCatalogLoopSettingsSection::isShowFirstPlainTextTier(), |
| 49 |
'plain_text_option_text' => ProductCatalogLoopSettingsSection::getPlainTextTemplate(), |
| 50 |
'plain_text_default_option_text' => ProductCatalogLoopSettingsSection::getFirstTierPlainTextTemplate(), |
| 51 |
); |
| 52 |
$default_quantity_measurement = array( |
| 53 |
'singular' => '', |
| 54 |
'plural' => '', |
| 55 |
); |
| 56 |
$quantity_measurement = $default_quantity_measurement; |
| 57 |
if ( in_array( $args['display_type'], array('table', 'horizontal-table', 'tooltip') ) ) { |
| 58 |
$quantity_measurement = ProductCatalogLoopSettingsSection::getTableQuantityMeasurement(); |
| 59 |
} |
| 60 |
if ( 'blocks' === $args['display_type'] ) { |
| 61 |
$quantity_measurement = ProductCatalogLoopSettingsSection::getBlocksQuantityMeasurement(); |
| 62 |
} |
| 63 |
$args['quantity_measurement_singular'] = $quantity_measurement['singular']; |
| 64 |
$args['quantity_measurement_plural'] = $quantity_measurement['plural']; |
| 65 |
return $args; |
| 66 |
} |
| 67 |
|
| 68 |
public function run() { |
| 69 |
add_filter( 'tiered_pricing_table/settings/sections', array($this, 'addSettingSection'), 5 ); |
| 70 |
if ( !ProductCatalogLoopSettingsSection::isEnabled() ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
add_filter( |
| 74 |
'tiered_pricing_table/frontend/variation_render_settings', |
| 75 |
function ( $settings, $productId, $context ) { |
| 76 |
if ( 'shop-loop' !== $context ) { |
| 77 |
return $settings; |
| 78 |
} |
| 79 |
return $this->getRenderAttributes(); |
| 80 |
}, |
| 81 |
10, |
| 82 |
3 |
| 83 |
); |
| 84 |
add_filter( |
| 85 |
'tiered_pricing_table/frontend/default_price_behaviour_type', |
| 86 |
function ( |
| 87 |
$type, |
| 88 |
$product, |
| 89 |
$priceHTML, |
| 90 |
$priceDisplayContext |
| 91 |
) { |
| 92 |
if ( 'shop-loop' === $priceDisplayContext ) { |
| 93 |
return ( ProductCatalogLoopSettingsSection::isDynamicPrice() ? 'dynamic' : 'static' ); |
| 94 |
} |
| 95 |
return $type; |
| 96 |
}, |
| 97 |
10, |
| 98 |
4 |
| 99 |
); |
| 100 |
add_action( 'init', function () { |
| 101 |
$position = ProductCatalogLoopSettingsSection::getPosition(); |
| 102 |
$closeLink = false; |
| 103 |
if ( 'woocommerce_shop_loop_item_title' === $position['hook'] ) { |
| 104 |
$closeLink = true; |
| 105 |
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 ); |
| 106 |
} |
| 107 |
add_action( $position['hook'], function () use($closeLink) { |
| 108 |
global $product, $post; |
| 109 |
if ( !$product instanceof WC_Product ) { |
| 110 |
$productID = ( isset( $post ) ? $post->ID : null ); |
| 111 |
$product = wc_get_product( $productID ); |
| 112 |
} |
| 113 |
if ( !$product ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
if ( !$product->is_purchasable() ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
if ( $closeLink ) { |
| 120 |
echo '</a>'; |
| 121 |
} |
| 122 |
$this->render( $product, ProductCatalogLoopSettingsSection::useReducedStyles() ); |
| 123 |
}, $position['priority'] ); |
| 124 |
} ); |
| 125 |
} |
| 126 |
|
| 127 |
public function render( WC_product $product, $useReducedStyles = false ) { |
| 128 |
$classes = 'tiered-pricing-shop-loop'; |
| 129 |
if ( $useReducedStyles ) { |
| 130 |
$classes .= ' tiered-pricing-shop-loop--reduced'; |
| 131 |
} |
| 132 |
$this->getContainer()->getFileManager()->includeTemplate( 'frontend/shop-loop.php', array( |
| 133 |
'classes' => $classes, |
| 134 |
'product' => $product, |
| 135 |
'settings' => $this->getRenderAttributes(), |
| 136 |
) ); |
| 137 |
} |
| 138 |
|
| 139 |
public function addSettingSection( $sections ) : array { |
| 140 |
$_sections = array(); |
| 141 |
foreach ( $sections as $section ) { |
| 142 |
$_sections[] = $section; |
| 143 |
if ( $section->getSlug() === 'general' ) { |
| 144 |
$_sections[] = new ProductCatalogLoopSettingsSection(); |
| 145 |
} |
| 146 |
} |
| 147 |
return $_sections; |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|