با استفاده از این کد میتوان محصولات متغیر ووکامرس را بهصورت یک جدول در صفحه محصول نمایش داد. در این جدول وزن، قیمت و وضعیت موجودی هر variation نمایش داده میشود و کاربر میتواند با انتخاب هر ردیف، همان محصول را برای خرید انتخاب کند. همچنین ارزانترین variation موجود بهصورت خودکار انتخاب میشود.
برای نمایش جدول کافی است شورتکد زیر را در صفحه محصول قرار دهید:
[variation_table]
<?php
add_filter('woocommerce_price_format', 'custom_price_format', 10, 2);
function custom_price_format($format, $currency_pos) {
return '%1$s%2$s'; }
add_filter('wc_price_args', 'custom_wc_price_args');
function custom_wc_price_args($args) {
$args['decimals'] = 0;
$args['decimal_separator'] = '.';
$args['thousand_separator'] = '.';
return $args; }
add_shortcode('variation_table', 'custom_variation_table');
function custom_variation_table() {
if (!is_product()) return '';
global $product;
if (!$product || !$product->is_type('variable')) {
return ''; }
$weight_attribute = 'pa_weight';
$variations = $product->get_available_variations();
if (!$variations) {
return ''; }
ob_start();
?>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20.custom-variation-table-wrapper%7B%20margin%3A0%3B%20%7D%0A%20%20%20%20.custom-variation-table%7B%20width%3A100%25%3B%20border-collapse%3Acollapse%3B%20font-size%3A14px%3B%20margin%3A0%3B%20%7D%0A%20%20%20%20.custom-variation-table%20th%2C%20.custom-variation-table%20td%7B%20border%3A1px%20solid%20%23e5e5e5%3B%20padding%3A5px%3B%20text-align%3Aright%3B%20vertical-align%3Amiddle%3B%20line-height%3A1.4%3B%20%7D%0A%20%20%20%20.custom-variation-table%20th%7B%20background%3A%23f7f7f7%3B%20font-weight%3A700%3B%20%7D%0A%20%20%20%20.custom-variation-table%20tr%7B%20cursor%3Apointer%3B%20transition%3Aall%20.2s%20ease%3B%20%7D%0A%20%20%20%20.custom-variation-table%20tr%3Ahover%7B%20background%3A%23fafafa%3B%20%7D%0A%20%20%20%20.custom-variation-table%20tr.selected%7B%20background%3A%23eef6ff%3B%20%7D%0A%20%20%20%20.custom-variation-table%20tr.is-out-of-stock%7B%20opacity%3A.6%3B%20cursor%3Anot-allowed%3B%20%7D%0A%20%20%20%20.custom-variation-weight%20label%7B%20display%3Aflex%3B%20align-items%3Acenter%3B%20gap%3A6px%3B%20margin%3A0%3B%20cursor%3Apointer%3B%20%7D%0A%20%20%20%20.custom-variation-weight%20input%5Btype%3D%22radio%22%5D%7B%20margin%3A0%3B%20transform%3Ascale(1.03)%3B%20flex%3A0%200%20auto%3B%20%7D%0A%20%20%20%20.custom-variation-price%7B%20white-space%3Anowrap%3B%20%7D%0A%20%20%20%20.custom-variation-price%20del%7B%20color%3A%23999%3B%20margin-left%3A6px%3B%20%7D%0A%20%20%20%20.custom-variation-price%20ins%7B%20color%3A%23c62828%3B%20text-decoration%3Anone%3B%20font-weight%3A700%3B%20%7D%0A%20%20%20%20.custom-out-of-stock%7B%20color%3A%23d32f2f%3B%20font-weight%3A700%3B%20%7D%0A%20%20%20%20%40media%20(max-width%3A767px)%7B%0A%20%20%20%20%20%20%20%20.custom-variation-table%7B%20font-size%3A13px%3B%20%7D%0A%20%20%20%20%20%20%20%20.custom-variation-table%20th%2C%20.custom-variation-table%20td%7B%20padding%3A5px%207px%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-style" width="20" height="20" alt="<style>" />
<div class="custom-variation-table-wrapper">
<table class="custom-variation-table">
<thead>
<tr>
<th>انتخاب وزن <span style="font-size:10px;color:#8c8c8c;">(گرم)</span></th>
<th>قیمت <span style="font-size:10px;color:#8c8c8c;">(تومان)</span></th>
</tr>
</thead>
<tbody>
<?php foreach ($variations as $variation_data): ?>
<?php
$id = $variation_data['variation_id'];
$variation = wc_get_product($id);
if (!$variation) continue;
$attributes = $variation->get_attributes();
$weight = '-';
if (isset($attributes[$weight_attribute]) && !empty($attributes[$weight_attribute])) {
$slug = $attributes[$weight_attribute];
$term = get_term_by('slug', $slug, $weight_attribute);
$weight = $term ? $term->name : $slug;
}
$is_in_stock = !empty($variation_data['is_in_stock']);
$price_html = '';
if ($is_in_stock) {
$regular_price = (float) $variation->get_regular_price();
$sale_price = (float) $variation->get_sale_price();
$current_price = (float) $variation->get_price();
$format_price = function($price) { return number_format($price, 0, '.', '.') . ' تومان'; };
if ($sale_price && $sale_price < $regular_price) {
$price_html = '<span class="custom-price-wrapper"><del>' . $format_price($regular_price) . '</del><ins>' . $format_price($sale_price) . '</ins></span>';
} else {
$price_html = '<span class="custom-price-wrapper">' . $format_price($current_price) . '</span>';
}
} else {
$price_html = '<span class="custom-out-of-stock">ناموجود</span>';
}
$row_classes = array('custom-variation-row');
if (!$is_in_stock) $row_classes[] = 'is-out-of-stock';
?>
<tr class="<?php echo esc_attr(implode(' ', $row_classes)); ?>"
data-variation-id="<?php echo esc_attr($id); ?>"
<?php foreach ($attributes as $tax => $val): ?>
data-<?php echo esc_attr('attribute_' . $tax); ?>="<?php echo esc_attr($val); ?>"
<?php endforeach; ?>
>
<td class="custom-variation-weight">
<?php if ($is_in_stock): ?>
<label>
<input type="radio" name="custom_variation_radio" value="<?php echo esc_attr($id); ?>">
<span><?php echo esc_html($weight); ?></span>
</label>
<?php else: ?>
<span><?php echo esc_html($weight); ?></span>
<?php endif; ?>
</td>
<td class="custom-variation-price"><?php echo wp_kses_post($price_html); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20jQuery(function(%24)%7B%0A%20%20%20%20%20%20%20%20function%20initVariationTable()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24('.custom-variation-row').off('click.customVariation').on('click.customVariation'%2C%20function(e)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(%24(this).hasClass('is-out-of-stock'))%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(%24(e.target).is('input'))%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20radio%20%3D%20%24(this).find('input%5Btype%3D%22radio%22%5D')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(radio.length)%20%7B%20radio.prop('checked'%2C%20true).trigger('change')%3B%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24('input%5Bname%3D%22custom_variation_radio%22%5D').off('change.customVariation').on('change.customVariation'%2C%20function()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20row%20%3D%20%24(this).closest('tr')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20form%20%3D%20%24('form.variations_form').first()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(!form.length)%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24('.custom-variation-row').removeClass('selected')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20row.addClass('selected')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.find('select').each(function()%7B%20%24(this).val('')%3B%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24.each(row.get(0).attributes%2C%20function(i%2C%20attr)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(attr%20%26%26%20attr.name.indexOf('data-attribute_')%20%3D%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20name%20%3D%20attr.name.replace('data-'%2C%20'')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20value%20%3D%20attr.value%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.find('select%5Bname%3D%22'%20%2B%20name%20%2B%20'%22%5D').val(value).trigger('change')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20variationId%20%3D%20row.data('variation-id')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.find('input%5Bname%3D%22variation_id%22%5D').val(variationId).trigger('change')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.trigger('woocommerce_variation_select_change')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.trigger('check_variations')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20initVariationTable()%3B%0A%20%20%20%20%20%20%20%20%24(document.body).on('woocommerce_update_variation_values%20found_variation%20reset_data'%2C%20function()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20initVariationTable()%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="<script>" />
<?php
return ob_get_clean();
}
add_action('wp_footer', function () {
if ( ! is_product() ) { return; }
?>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20jQuery(document).ready(function(%24)%7B%0A%20%20%20%20%20%20%20%20const%20priceBox%20%3D%20%24('.custom-currency-price')%3B%0A%20%20%20%20%20%20%20%20const%20form%20%20%20%20%20%3D%20%24('form.variations_form')%3B%0A%20%20%20%20%20%20%20%20if%20(!priceBox.length%20%7C%7C%20!form.length)%20%7B%20return%3B%20%7D%0A%20%20%20%20%20%20%20%20function%20formatPrice(price)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20price%20%3D%20Math.floor(price)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20formatted%20%3D%20price.toString().replace(%2F%5CB(%3F%3D(%5Cd%7B3%7D)%2B(%3F!%5Cd))%2Fg%2C%20'.')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22woocommerce-Price-amount%20amount%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%7Bformatted%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22woocommerce-Price-currencySymbol%22%3E%D8%AA%D9%88%D9%85%D8%A7%D9%86%3C%2Fspan%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fspan%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%60%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20function%20getCheapestVariation()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20variations%20%3D%20form.data('product_variations')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!variations%20%7C%7C%20!variations.length)%20return%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20let%20cheapest%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20variations.forEach(function(v)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(typeof%20v.display_price%20!%3D%3D%20'undefined'%20%26%26%20v.is_in_stock)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(cheapest%20%3D%3D%3D%20null%20%7C%7C%20parseFloat(v.display_price)%20%3C%20parseFloat(cheapest.display_price))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cheapest%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20cheapest%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20function%20selectCheapestVariation()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20cheapest%20%3D%20getCheapestVariation()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!cheapest)%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20currentPriceText%20%3D%20priceBox.text().replace('%D8%AA%D9%88%D9%85%D8%A7%D9%86'%2C%20'').replace(%2F%5C.%2Fg%2C%20'').trim()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20const%20newPrice%20%3D%20Math.floor(cheapest.display_price)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(parseInt(currentPriceText)%20%3D%3D%3D%20newPrice)%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20priceBox.html(formatPrice(cheapest.display_price))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20priceBox.attr('data-default-price'%2C%20cheapest.display_price)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24('.custom-variation-row').removeClass('selected')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24('input%5Bname%3D%22custom_variation_radio%22%5D%5Bvalue%3D%22'%20%2B%20cheapest.variation_id%20%2B%20'%22%5D')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.prop('checked'%2C%20true)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.closest('tr')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.addClass('selected')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(cheapest.attributes)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24.each(cheapest.attributes%2C%20function(name%2C%20value)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20form.find('select%5Bname%3D%22'%20%2B%20name%20%2B%20'%22%5D').val(value).trigger('change')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20form.find('input%5Bname%3D%22variation_id%22%5D').val(cheapest.variation_id)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20setTimeout(function()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20selectCheapestVariation()%3B%0A%20%20%20%20%20%20%20%20%7D%2C%20300)%3B%0A%20%20%20%20%20%20%20%20%24(document).on('found_variation'%2C%20'form.variations_form'%2C%20function(event%2C%20variation)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(variation%20%26%26%20typeof%20variation.display_price%20!%3D%3D%20'undefined')%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20priceBox.html(formatPrice(variation.display_price))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%24(document).on('reset_data'%2C%20'form.variations_form'%2C%20function()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20setTimeout(function()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20selectCheapestVariation()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%20100)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="<script>" />
<?php
});
یک پاسخ
123