نمایش ویژگیهای محصول بهصورت باکس مانند دیجیکالا
با استفاده از این شورتکد، ویژگیهای محصول در صفحه محصول ووکامرس بهصورت باکسهای مرتب و خوانا نمایش داده میشوند. حداکثر ۴ ویژگی اول محصول نمایش داده شده و نام ویژگی و مقادیر آن در باکسهای جداگانه قرار میگیرند.
نحوه استفاده:
[product_features_boxes]
این شورتکد را میتوانید در صفحه محصول، مثلاً در المنتور قرار دهید.
<?php
function shortcode_product_features_boxes() {
if (!is_product()) return '';
global $product;
$max_features = 4;
$attributes = $product->get_attributes();
$output = '<div class="custom-feature-boxes">';
$count = 0;
foreach ($attributes as $attribute) {
if ($count >= $max_features) break;
$name = wc_attribute_label($attribute->get_name());
$value = $product->get_attribute($attribute->get_name());
if (!empty($value)) {
$values = array_map('trim', explode(',', $value));
$output .= '<div class="feature-group">';
$output .= '<span class="feature-title">'.esc_html($name).':</span>';
$output .= '<div class="feature-values">';
foreach ($values as $v) {
$output .= '<span class="feature-value">'.esc_html($v).'</span>';
}
$output .= '</div></div>';
$count++;
}
}
$output .= '</div>';
$output .= '
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20.custom-feature-boxes%7B%0A%20%20%20%20%20%20%20%20display%3Aflex%3B%0A%20%20%20%20%20%20%20%20flex-wrap%3Awrap%3B%0A%20%20%20%20%20%20%20%20gap%3A8px%3B%0A%20%20%20%20%20%20%20%20margin%3A%200px%3B%0A%20%20%20%20%7D%0A%20%20%20%20.feature-group%7B%0A%20%20%20%20%20%20%20%20display%3Ainline-flex%3B%0A%20%20%20%20%20%20%20%20align-items%3Acenter%3B%0A%20%20%20%20%20%20%20%20gap%3A8px%3B%0A%20%20%20%20%20%20%20%20background%3A%23f7f7f7%3B%0A%20%20%20%20%20%20%20%20border%3A1px%20solid%20%23eee%3B%0A%20%20%20%20%20%20%20%20border-radius%3A5px%3B%0A%20%20%20%20%20%20%20%20padding%3A5px%3B%0A%20%20%20%20%20%20%20%20width%3Aauto%3B%0A%20%20%20%20%20%20%20%20max-width%3A100%25%3B%0A%20%20%20%20%7D%0A%20%20%20%20.feature-title%7B%0A%20%20%20%20%20%20%20%20font-size%3A13px%3B%0A%20%20%20%20%20%20%20%20font-weight%3A600%3B%0A%20%20%20%20%20%20%20%20color%3A%23333%3B%0A%20%20%20%20%20%20%20%20white-space%3Anowrap%3B%0A%20%20%20%20%7D%0A%20%20%20%20.feature-values%7B%0A%20%20%20%20%20%20%20%20display%3Aflex%3B%0A%20%20%20%20%20%20%20%20flex-wrap%3Awrap%3B%0A%20%20%20%20%20%20%20%20gap%3A5px%3B%0A%20%20%20%20%7D%0A%20%20%20%20.feature-value%7B%0A%20%20%20%20%20%20%20%20background%3A%23fff%3B%0A%20%20%20%20%20%20%20%20border%3A1px%20solid%20%23e5e5e5%3B%0A%20%20%20%20%20%20%20%20border-radius%3A6px%3B%0A%20%20%20%20%20%20%20%20padding%3A5px%3B%0A%20%20%20%20%20%20%20%20font-size%3A13px%3B%0A%20%20%20%20%20%20%20%20color%3A%23555%3B%0A%20%20%20%20%20%20%20%20white-space%3Anowrap%3B%0A%20%20%20%20%20%20%20%20line-height%3A1.6%3B%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>" />';
return $output; }
add_shortcode('product_features_boxes','shortcode_product_features_boxes');
/*
// ######################### // نمایش ۶ ویژگی اول محصول در لوپ ووکامرس // [ostadno_product_attrs]
function ostadno_show_first_six_attributes() {
global $product;
if ( ! $product || ! is_a( $product, 'WC_Product' ) ) return;
$attributes = $product->get_attributes();
if ( empty( $attributes ) ) return;
echo '<ul class="ostadno-product-attrs">';
$count = 0;
foreach ( $attributes as $attribute ) {
if ( $count >= 6 ) break;
$name = wc_attribute_label( $attribute->get_name() );
$value = '';
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $attribute->get_name(), ['fields' => 'names'] );
$value = implode( ', ', $terms );
} else {
$value = $attribute->get_options()[0];
}
if ( $value ) {
echo '<li><strong>' . esc_html($name) . ':</strong> ' . esc_html($value) . '</li>';
$count++;
}
}
echo '</ul>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'ostadno_show_first_six_attributes', 15 );
function ostadno_product_attrs_shortcode() {
ob_start();
ostadno_show_first_six_attributes();
return ob_get_clean();
}
add_shortcode( 'ostadno_product_attrs', 'ostadno_product_attrs_shortcode' );
*/