Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yokirahmada/696d3ca0eb0a26fa64561497c22d7b2f to your computer and use it in GitHub Desktop.
Save yokirahmada/696d3ca0eb0a26fa64561497c22d7b2f to your computer and use it in GitHub Desktop.
Debugging & get price by size with Plugin Republic Plugin
<!-- Add on your function theme or code snipets plugin -->
// 1. SHORTCODE FORM FILTER
function pewc_size_filter_shortcode() {
if (!is_shop() && !is_product_category()) return '';
ob_start();
$width = isset($_GET['width']) ? intval($_GET['width']) : '';
$height = isset($_GET['height']) ? intval($_GET['height']) : '';
?>
<div class="pewc-size-filter-form">
<form method="get" class="pewc-filter-form">
<input type="hidden" name="post_type" value="product">
<div class="filter-inputs">
<div class="input-group">
<label for="pewc-width">Width (mm)</label>
<input type="number" id="pewc-width" name="width" value="<?php echo esc_attr($width); ?>" min="0" required>
</div>
<div class="input-group">
<label for="pewc-height">Height (mm)</label>
<input type="number" id="pewc-height" name="height" value="<?php echo esc_attr($height); ?>" min="0" required>
</div>
</div>
<div class="filter-buttons">
<button type="submit" class="filter-submit">Apply Filters</button>
<a href="<?php echo esc_url(remove_query_arg(['width', 'height'])); ?>" class="filter-reset">Reset</a>
</div>
</form>
</div>
<style>
.pewc-size-filter-form {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background: #f8f9fa;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.pewc-filter-form .filter-inputs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: 600;
color: #333;
}
.input-group input {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
background: #fff;
}
.filter-buttons {
display: flex;
gap: 10px;
}
.filter-submit, .filter-reset {
flex: 1;
padding: 12px;
border: none;
border-radius: 4px;
cursor: pointer;
text-align: center;
text-decoration: none;
font-weight: 600;
transition: all 0.3s;
}
.filter-submit {
background: #27ae60;
color: white;
}
.filter-submit:hover {
background: #219955;
}
.filter-reset {
background: #e74c3c;
color: white;
}
.filter-reset:hover {
background: #c0392b;
}
</style>
<?php
return ob_get_clean();
}
add_shortcode('pewc_size_filter', 'pewc_size_filter_shortcode');
// 2. FUNCTION FILTER PRODUCTS
add_action('woocommerce_product_query', 'filter_products_by_pewc_size', 20);
function filter_products_by_pewc_size($q) {
if (is_admin() || !$q->is_main_query()) return;
$width = isset($_GET['width']) ? (int)$_GET['width'] : null;
$height = isset($_GET['height']) ? (int)$_GET['height'] : null;
// Only filter with parameter width/height
if (!$width && !$height) return;
$all_ids = wc_get_products([
'limit' => -1,
'return' => 'ids',
'status' => 'publish'
]);
$matched_ids = [];
foreach ($all_ids as $product_id) {
$groups = pewc_get_extra_fields($product_id);
$has_match = false;
// Skip If product DOESNT HAVE PEWC GROUPS FROM PLUGIN
if (empty($groups)) continue;
foreach ($groups as $group) {
if (empty($group['items'])) continue;
foreach ($group['items'] as $field) {
if (isset($field['field_type']) &&
$field['field_type'] === 'calculation' &&
isset($field['formula']) &&
$field['formula'] === '{look_up_table}' &&
!empty($field['look_up_table'])) {
$tables = apply_filters('pewc_calculation_look_up_tables', []);
$table = $tables[$field['look_up_table']] ?? [];
// Check width & height
if ($width && $height) {
if (isset($table[$width][$height])) {
$has_match = true;
break 2;
}
} elseif ($width) {
if (isset($table[$width])) {
$has_match = true;
break 2;
}
} elseif ($height) {
foreach ($table as $w => $heights) {
if (isset($heights[$height])) {
$has_match = true;
break 2;
}
}
}
}
}
}
if ($has_match) {
$matched_ids[] = $product_id;
}
}
// If product matches
if (!empty($matched_ids)) {
$q->set('post__in', $matched_ids);
} else {
// If doesnt product matches
add_action('woocommerce_before_shop_loop', function() {
echo '<div class="woocommerce-info">No products match the selected size.</div>';
}, 30);
// empty query
$q->set('post__in', [0]);
}
}
// 3. Show active filters
add_action('woocommerce_before_shop_loop', 'display_active_size_filter', 5);
function display_active_size_filter() {
if (!is_shop() && !is_product_category()) return;
$width = isset($_GET['width']) ? (int)$_GET['width'] : null;
$height = isset($_GET['height']) ? (int)$_GET['height'] : null;
if ($width || $height) {
echo '<div class="pewc-active-filter">';
echo '<strong>Active Filter:</strong> ';
echo $width ? 'Width: ' . $width . 'mm ' : '';
echo $height ? 'Height: ' . $height . 'mm ' : '';
echo '</div>';
echo '<style>
.pewc-active-filter {
padding: 15px;
margin: 0 0 20px;
background: #f8f9fa;
border-left: 4px solid #27ae60;
color: #333;
}
</style>';
}
}
function get_pewc_table_price($product_id, $width, $height) {
$groups = pewc_get_extra_fields($product_id);
foreach ($groups as $group) {
if (empty($group['items'])) continue;
foreach ($group['items'] as $field) {
if (isset($field['field_type']) &&
$field['field_type'] === 'calculation' &&
isset($field['formula']) &&
$field['formula'] === '{look_up_table}' &&
!empty($field['look_up_table'])) {
$tables = apply_filters('pewc_calculation_look_up_tables', []);
$table = $tables[$field['look_up_table']] ?? [];
if (isset($table[$width][$height])) {
return $table[$width][$height];
}
}
}
}
return false; // Return false if doesnt found
}
add_filter('woocommerce_get_price_html', 'update_price_based_on_size', 10, 2);
function update_price_based_on_size($price_html, $product) {
if (!is_shop() && !is_product_category()) return $price_html;
$width = isset($_GET['width']) ? (int)$_GET['width'] : null;
$height = isset($_GET['height']) ? (int)$_GET['height'] : null;
if ($width && $height) {
$table_price = get_pewc_table_price($product->get_id(), $width, $height);
if ($table_price !== false) {
$formatted_price = wc_price($table_price);
return '<span class="price"><span class="woocommerce-Price-amount amount"><bdi>' .
$formatted_price . '</bdi></span></span>';
}
}
return $price_html;
}
// DEBUGING CODE
add_action('wp', function() {
if (!is_shop() || !current_user_can('manage_options')) return;
if (!isset($_GET['debug_size_filter'])) return;
// Get parameters
$width = isset($_GET['width']) ? (int)$_GET['width'] : 0;
$height = isset($_GET['height']) ? (int)$_GET['height'] : 0;
echo '<div style="padding: 20px; background: #fff; margin: 20px; border: 1px solid #ddd;">';
echo '<h1>PEWC Size Filter Debugger</h1>';
echo "<h2>Filter Parameters: Width={$width}mm, Height={$height}mm</h2>";
$products = wc_get_products([
'limit' => -1,
'return' => 'ids',
'status' => 'publish'
]);
foreach ($products as $product_id) {
$product = wc_get_product($product_id);
echo '<hr>';
echo "<h3>Product #{$product_id}: {$product->get_name()}</h3>";
$groups = function_exists('pewc_get_extra_fields') ? pewc_get_extra_fields($product_id) : [];
if (empty($groups)) {
echo "<p>No PEWC groups found (will be INCLUDED in results)</p>";
continue;
}
$has_lookup_table = false;
foreach ($groups as $group_id => $group) {
if (empty($group['items'])) continue;
foreach ($group['items'] as $field_id => $field) {
if (isset($field['field_type']) &&
$field['field_type'] === 'calculation' &&
isset($field['formula']) &&
$field['formula'] === '{look_up_table}' &&
!empty($field['look_up_table'])) {
$has_lookup_table = true;
$table_key = $field['look_up_table'];
echo "<h4>Lookup Table Found: {$field['field_label']} (Key: {$table_key})</h4>";
$all_tables = apply_filters('pewc_calculation_look_up_tables', []);
if (!isset($all_tables[$table_key])) {
echo "<p style='color:red'>Table with key '{$table_key}' not found!</p>";
continue;
}
$table = $all_tables[$table_key];
// Extract all unique widths and heights
$widths = array_keys($table);
$heights = [];
foreach ($table as $w => $height_prices) {
$heights = array_unique(array_merge($heights, array_keys($height_prices)));
}
sort($widths);
sort($heights);
// Display table
echo "<table border='1' cellpadding='5' style='border-collapse: collapse;'>";
// Header row (widths)
echo "<tr><th>{$table_key}</th>";
foreach ($widths as $w) {
$style = ($width == $w) ? 'background: #FFEB3B;' : '';
echo "<th style='{$style}'>" . esc_html($w) . "</th>";
}
echo "</tr>";
// Data rows
foreach ($heights as $h) {
echo "<tr>";
$style = ($height == $h) ? 'background: #FFEB3B;' : '';
echo "<td style='{$style}'>" . esc_html($h) . "</td>";
foreach ($widths as $w) {
$value = $table[$w][$h] ?? '';
$cell_style = ($width == $w && $height == $h) ? 'background: #4CAF50; color: white;' : '';
echo "<td style='{$cell_style}'>" . esc_html($value) . "</td>";
}
echo "</tr>";
}
echo "</table>";
// Check for match
if ($width > 0 && $height > 0) {
$match_found = isset($table[$width][$height]);
if ($match_found) {
echo "<p style='color:green; font-weight:bold'>MATCH FOUND for {$width}mm × {$height}mm (Price: {$table[$width][$height]})</p>";
} else {
echo "<p style='color:red'>No match for {$width}mm × {$height}mm</p>";
}
}
}
}
}
if (!$has_lookup_table) {
echo "<p>No lookup tables found (will be INCLUDED in results)</p>";
}
}
echo '</div>';
exit;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment