Home / Admin / more feater
Duplicate Snippet

Embed Snippet on Your Site

more feater

Code Preview
php
<?php
add_shortcode('product_features_boxes', function () {
    if (!function_exists('wc_get_product')) {
        return '';
    }
    global $product;
    if (!$product || !is_a($product, 'WC_Product')) {
        $product = wc_get_product(get_the_ID());
    }
    if (!$product) {
        return '';
    }
    $attributes = $product->get_attributes();
    if (empty($attributes)) {
        return '';
    }
    $items = array();
    foreach ($attributes as $attribute) {
        if (!$attribute->get_visible()) {
            continue;
        }
        $label = wc_attribute_label($attribute->get_name());
        if ($attribute->is_taxonomy()) {
            $values = wc_get_product_terms(
                $product->get_id(),
                $attribute->get_name(),
                array('fields' => 'names')
            );
        } else {
            $values = $attribute->get_options();
        }
        if (empty($values)) {
            continue;
        }
        $value = implode('، ', $values);
        if (!$label || !$value) {
            continue;
        }
        $items[] = array(
            'label' => $label,
            'value' => $value,
        );
        if (count($items) >= 6) {
            break;
        }
    }
    if (empty($items)) {
        return '';
    }
    ob_start();
    ?>
    <div class="mc-dk-features-wrap">
        <div class="mc-dk-features-title">ویژگی‌ها</div>
        <div class="mc-dk-features-grid">
            <?php foreach ($items as $item) : ?>
                <div class="mc-dk-feature-box">
                    <span class="mc-dk-feature-label">
                        <?php echo esc_html($item['label']); ?>
                    </span>
                    <span class="mc-dk-feature-value">
                        <?php echo esc_html($item['value']); ?>
                    </span>
                </div>
            <?php endforeach; ?>
        </div>
        <a class="mc-dk-features-more" href="#tab-additional_information">
            مشاهده همه ویژگی‌ها
        </a>
    </div>
    <?php
    return ob_get_clean();
});

Comments

Add a Comment