نمایش محصولات مکمل و ویژه

این کد محصولات Cross-sell و Up-sell ووکامرس را با ظاهر اختصاصی، تصویر، قیمت و درصد تخفیف در صفحه محصول نمایش می‌دهد.

نمایش محصولات مکمل و ویژه در ووکامرس

این کد امکان نمایش محصولات مرتبط با محصول فعلی را با دو شورت‌کد فراهم می‌کند:

[custom_cross_sells]
محصولاتی که در بخش Cross-sell ووکامرس برای محصول انتخاب شده‌اند را با عنوان «محصولات مشابه» نمایش می‌دهد.

[custom_up_sells]
محصولاتی که در بخش Up-sell ووکامرس تعیین شده‌اند را با عنوان «محصولات مکمل» نمایش می‌دهد.

برای هر محصول، تصویر، نام، قیمت و درصد تخفیف نمایش داده می‌شود. محصولات در دسکتاپ به‌صورت چهارستونه و در موبایل به‌صورت تک‌ستونه نمایش داده می‌شوند و با کلیک روی هر محصول، صفحه آن در تب جدید باز خواهد شد.

همچنین اگر محصولی برای Cross-sell یا Up-sell تعریف نشده باشد، شورت‌کد چیزی نمایش نمی‌دهد.


<?php
function vira_cross_get_sale_percent($product) {
    if (!$product->is_on_sale()) {
        return 0; }
    $regular_price = (float) $product->get_regular_price();
    $sale_price    = (float) $product->get_sale_price();
    if ($regular_price <= 0 || $sale_price <= 0) {
        return 0; }
    return (1 - ($sale_price / $regular_price)) * 100;
}
function vira_cross_format_percent($percent) {
    if ($percent <= 0) {
        return ''; }
    $formatted = number_format($percent, 2);
    if (strpos($formatted, '.') !== false) {
        $formatted = rtrim(rtrim($formatted, '0'), '.');
    }
    return $formatted; }

// نمایش محصولات مشابه // [custom_cross_sells]
function custom_cross_sells_shortcode() {
    if ( ! is_product() ) return '';
    global $product;
    $cross_sells = $product->get_cross_sell_ids();
    if ( empty($cross_sells) ) {
        return '';
    }
    ob_start();
    ?>
    <style>
        .custom-sells-title { margin: 0px; }
        .custom-sells-wrapper { margin-top: 3px; display: flex; flex-wrap: wrap; gap: 5px; }
        .custom-sells-box { flex: 0 0 calc(25% - 3.75px); box-sizing: border-box; border: 1px solid #ddd; border-radius: 10px; padding: 0px; min-width: 0; background: #fcfcfc; }
        .custom-sells-box-image { display: flex; align-items: center; gap: 5px; min-width: 0; }
        .custom-sells-image { display: block; width: 70px; height: 70px; overflow: hidden; flex-shrink: 0; }
        .custom-sells-image img { border-radius: 0 10px 10px 0 !important; background: #ddd; } 
        .custom-sells-box-image > div { min-width: 0; overflow: hidden; width: 100%; }
        .custom-sells-products { color: #333 !important; text-decoration: none !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; max-width: 100%; }
        .custom-sells-price { color: #777; font-size: 13px; display: flex; justify-content: space-between; align-items: center; width: 100%; margin-top: 4px; }
        .custom-sells-sale-text { color: #e74c3c; font-size: 13px; font-weight: bold; margin-left: 10px; }
		@media (max-width: 767px) { .custom-sells-box { flex: 0 0 100%; } }
    </style> 
    <h4 class="custom-sells-title">محصولات مشابه</h4>
    <div class="custom-sells-wrapper">
        <?php foreach ( $cross_sells as $cross_id ) :
            $cross_product = wc_get_product( $cross_id );
            if ( ! $cross_product ) continue;
            $product_permalink = get_permalink( $cross_id );
            $percent = vira_cross_get_sale_percent($cross_product);
        ?>
        <div class="custom-sells-box">
            <div class="custom-sells-box-image">
                <a class="custom-sells-image" href="<?php echo esc_url($product_permalink); ?>" target="_blank" rel="noopener">
                    <?php echo $cross_product->get_image( 'woocommerce_thumbnail', ['style'=>'width:100%;height:100%;object-fit:contain;' ] ); ?>
                </a>
                <div>
                    <a class="custom-sells-products" href="<?php echo esc_url($product_permalink); ?>" target="_blank" rel="noopener" title="<?php echo esc_attr($cross_product->get_name()); ?>">
                        <?php echo $cross_product->get_name(); ?>
                    </a>
                    <div class="custom-sells-price">
                        <span class="custom-sells-price-value"><?php echo wc_price( $cross_product->get_price() ); ?></span>
                        <?php if ( $percent > 0 ) : ?>
                            <span class="custom-sells-sale-text">٪<?php echo vira_cross_format_percent($percent); ?>-</span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php
    return ob_get_clean(); 
}
add_shortcode( 'custom_cross_sells', 'custom_cross_sells_shortcode' );

// نمایش محصولات مکمل // [custom_up_sells]
function custom_up_sells_shortcode() {
    if ( ! is_product() ) return '';
    global $product;
    $upsells = $product->get_upsell_ids();
    if ( empty($upsells) ) {
        return ''; } 
    ob_start();
    ?>
    <h4 class="custom-sells-title">محصولات مکمل</h4>
    <div class="custom-sells-wrapper">
        <?php foreach ( $upsells as $upsell_id ) :
            $upsell_product = wc_get_product( $upsell_id );
            if ( ! $upsell_product ) continue;
            $product_permalink = get_permalink( $upsell_id );
            $percent = vira_cross_get_sale_percent($upsell_product);
        ?>
        <div class="custom-sells-box">
            <div class="custom-sells-box-image">
                <a class="custom-sells-image" href="<?php echo esc_url($product_permalink); ?>" target="_blank" rel="noopener">
                    <?php echo $upsell_product->get_image( 'woocommerce_thumbnail', ['style'=>'width:100%;height:100%;object-fit:contain;' ] ); ?>
                </a>
                <div>
                    <a class="custom-sells-products" href="<?php echo esc_url($product_permalink); ?>" target="_blank" rel="noopener" title="<?php echo esc_attr($upsell_product->get_name()); ?>">
                        <?php echo $upsell_product->get_name(); ?>
                    </a>
                    <div class="custom-sells-price">
                        <span class="custom-sells-price-value"><?php echo wc_price( $upsell_product->get_price() ); ?></span>
                        <?php if ( $percent > 0 ) : ?>
                            <span class="custom-sells-sale-text">٪<?php echo vira_cross_format_percent($percent); ?>-</span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php
    return ob_get_clean(); 
}
add_shortcode( 'custom_up_sells', 'custom_up_sells_shortcode' );

روش اول: فایل functions.php

اگر از قالب فرزند (Child Theme) استفاده می‌کنید، می‌توانید کد را در فایل: functions.php قالب فرزند قرار دهید.

توصیه می‌شود کد را مستقیماً در فایل functions.php قالب اصلی قرار ندهید؛ زیرا با به‌روزرسانی قالب ممکن است تغییرات شما از بین برود.

روش دوم: استفاده از افزونه Code Snippets

روش ساده‌تر این است که از افزونه‌های مدیریت Snippet استفاده کنید.

در این روش، کد را به‌عنوان یک Snippet جدید اضافه کرده و آن را فعال می‌کنید.

مزیت این روش این است که برای اضافه کردن کدهای PHP نیازی به ویرایش مستقیم فایل‌های قالب ندارید.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *