Files
wordpress-theme-aksrt/templates/pages/page-product.php
T
kate522 c102c9d16d feat(shop): wire product/cart widget zones and extra sidebars
Render shop_product top/sidebar/bottom and cart fluid areas; register
shop home/order sidebar slots for zibll shop layout parity.
2026-07-19 01:36:14 +08:00

472 lines
30 KiB
PHP

<?php
/**
* Template Name: AKSRT - Product Detail
* Template Post Type: page
*
* Enhanced: options selector, video gallery, params/services modal,
* review tab, favorites, related products, mobile tabbar (§6.10).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
get_header();
$product_id = (int) ($_GET['product_id'] ?? 0);
if ($product_id <= 0) {
$product_id = (int) get_query_var('product_id', 0);
}
$shop = class_exists('\AKSRT\Shop\Shop') ? \AKSRT\Shop\Shop::get_instance() : null;
$product = function_exists('aksrt_get_product_page_view')
? aksrt_get_product_page_view($product_id)
: ($shop ? $shop->get_product_detail_view($product_id) : []);
$user_id = get_current_user_id();
$is_fav = $shop && $user_id > 0 ? $shop->is_favorite($user_id, $product_id) : false;
$options = $product['options'] ?? [];
$has_options = !empty($options);
$price_range = $product['price_range'] ?? [];
$params = $product['params'] ?? [];
$services = $product['services'] ?? [];
$pay_modo = $product['pay_modo'] ?? '0';
$scores = class_exists('\AKSRT\Shop\ProductReview') ? \AKSRT\Shop\ProductReview::get_instance()->get_product_scores($product_id) : [];
$reviews = class_exists('\AKSRT\Shop\ProductReview') ? \AKSRT\Shop\ProductReview::get_instance()->get_reviews($product_id, ['per_page' => 5]) : ['items' => [], 'total' => 0];
$author_id = (int) get_post_field('post_author', $product_id);
$author = $author_id > 0 ? get_userdata($author_id) : null;
?>
<main id="main-content" class="site-main content-shell content-shell--stream content-shell--product" tabindex="-1">
<?php if (is_active_sidebar('shop_product_top_fluid')) : ?>
<div class="aksrt-fluid-widget aksrt-fluid-widget--shop"><?php dynamic_sidebar('shop_product_top_fluid'); ?></div>
<?php endif; ?>
<?php if (function_exists('aksrt_breadcrumbs')) { aksrt_breadcrumbs(); } ?>
<?php if (!empty($product)) : ?>
<div class="content-wrap container">
<?php $has_shop_sidebar = is_active_sidebar('shop_product_sidebar'); ?>
<div class="content-layout <?php echo $has_shop_sidebar ? 'content-layout--with-sidebar' : 'content-layout--no-sidebar'; ?>">
<div class="content-layout__primary">
<div
class="aksrt-product-page"
data-aksrt-product-page
data-product-id="<?php echo esc_attr((string) $product_id); ?>"
data-options='<?php echo esc_attr(wp_json_encode($options, JSON_UNESCAPED_UNICODE)); ?>'
data-base-price="<?php echo esc_attr((string) (($product['start_price'] ?? 0) > 0 ? $product['start_price'] : ($product['price'] ?? 0))); ?>"
data-base-stock="<?php echo esc_attr((string) ($product['stock'] ?? 0)); ?>"
data-pay-modo="<?php echo esc_attr((string) $pay_modo); ?>"
data-price-range="<?php echo !empty($price_range) && (($price_range['min'] ?? 0) !== ($price_range['max'] ?? 0)) ? '1' : '0'; ?>"
>
<div class="aksrt-product-page__layout">
<!-- Gallery -->
<section class="aksrt-product-page__gallery aksrt-product-page__panel">
<?php if (!empty($product['image'])) : ?>
<div class="aksrt-product-page__primary-image" data-aksrt-main-image>
<img src="<?php echo esc_url((string) $product['image']); ?>" alt="<?php echo esc_attr((string) ($product['title'] ?? '')); ?>" id="product-main-img">
</div>
<?php else : ?>
<div class="aksrt-product-page__placeholder">
<i class="fas fa-image" aria-hidden="true"></i>
</div>
<?php endif; ?>
<?php
$gallery_items = (array) ($product['gallery'] ?? []);
$cover_videos = (array) (get_post_meta($product_id, '_aksrt_cover_videos', true) ?: []);
?>
<?php if (!empty($gallery_items) || !empty($cover_videos)) : ?>
<div class="aksrt-product-page__thumbs">
<?php foreach ($cover_videos as $video_url) : ?>
<button type="button" class="aksrt-product-page__thumb aksrt-product-page__thumb--video" data-aksrt-product-video="<?php echo esc_url($video_url); ?>">
<i class="fas fa-play-circle"></i>
</button>
<?php endforeach; ?>
<?php foreach ($gallery_items as $gallery_image) : ?>
<button type="button" class="aksrt-product-page__thumb" data-aksrt-product-thumb="<?php echo esc_url($gallery_image); ?>">
<img src="<?php echo esc_url($gallery_image); ?>" alt="">
</button>
<?php endforeach; ?>
</div>
<?php endif; ?>
</section>
<!-- Summary -->
<section class="aksrt-product-page__summary aksrt-product-page__panel">
<div class="aksrt-product-page__badges">
<?php if ($pay_modo === 'points') : ?>
<span class="aksrt-product-page__badge aksrt-product-page__badge--points"><?php esc_html_e('积分商品', 'aksrt'); ?></span>
<?php endif; ?>
<?php if (!empty($product['marketing']['headline'])) : ?>
<span class="aksrt-product-page__badge aksrt-product-page__badge--promo"><?php echo esc_html((string) $product['marketing']['headline']); ?></span>
<?php endif; ?>
<?php if (!empty($product['after_sale']['support_refund'])) : ?>
<span class="aksrt-product-page__badge"><?php esc_html_e('支持售后', 'aksrt'); ?></span>
<?php endif; ?>
</div>
<h1 class="aksrt-product-page__title"><?php echo esc_html((string) ($product['title'] ?? '')); ?></h1>
<!-- Price row with range support -->
<div class="aksrt-product-page__price-row">
<?php if ($has_options && !empty($price_range) && $price_range['min'] !== $price_range['max']) : ?>
<strong class="aksrt-product-page__price" data-aksrt-dynamic-price>
<?php echo $pay_modo === 'points' ? '' : '¥'; ?><?php echo esc_html(number_format($price_range['min'], 2)); ?> - <?php echo $pay_modo === 'points' ? '' : '¥'; ?><?php echo esc_html(number_format($price_range['max'], 2)); ?>
<?php if ($pay_modo === 'points') : ?><small><?php esc_html_e('积分', 'aksrt'); ?></small><?php endif; ?>
</strong>
<?php else : ?>
<strong class="aksrt-product-page__price" data-aksrt-dynamic-price>
<?php echo $pay_modo === 'points' ? '' : '¥'; ?><?php echo esc_html(number_format((float) ($product['price'] ?? 0), 2)); ?>
<?php if ($pay_modo === 'points') : ?><small><?php esc_html_e('积分', 'aksrt'); ?></small><?php endif; ?>
</strong>
<?php endif; ?>
<span class="aksrt-product-page__sales"><?php printf(esc_html__('已售 %d', 'aksrt'), (int) ($product['sales'] ?? 0)); ?></span>
<?php if (!empty($scores['average'])) : ?>
<span class="aksrt-product-page__rating"><i class="fas fa-star"></i> <?php echo esc_html((string) $scores['average']); ?></span>
<?php endif; ?>
</div>
<?php if (!empty($product['excerpt'])) : ?>
<p class="aksrt-product-page__excerpt"><?php echo esc_html((string) $product['excerpt']); ?></p>
<?php endif; ?>
<div class="aksrt-product-page__service-strip">
<span><i class="fas fa-shield-alt"></i> <?php esc_html_e('主站统一交易保障', 'aksrt'); ?></span>
<span><i class="fas fa-box-open"></i> <?php esc_html_e('与内容主线共用卡片与购买节奏', 'aksrt'); ?></span>
</div>
<!-- Product options selector -->
<?php if ($has_options) : ?>
<div class="aksrt-product-page__options" data-aksrt-options-selector>
<?php foreach ($options as $group_idx => $group) : ?>
<div class="aksrt-product-page__option-group" data-group-index="<?php echo esc_attr((string) $group_idx); ?>">
<span class="aksrt-product-page__option-label"><?php echo esc_html((string) ($group['name'] ?? '')); ?></span>
<div class="aksrt-product-page__option-values aksrt-product-page__option-values--<?php echo esc_attr($group['view_mode'] ?? 'list'); ?>">
<?php foreach ((array) ($group['values'] ?? []) as $val) : ?>
<button type="button"
class="aksrt-product-page__option-btn"
data-option-group="<?php echo esc_attr((string) $group_idx); ?>"
data-option-label="<?php echo esc_attr((string) ($val['label'] ?? '')); ?>"
data-price-change="<?php echo esc_attr((string) ($val['price_change'] ?? 0)); ?>"
<?php if (!empty($val['image'])) : ?>data-option-image="<?php echo esc_url($val['image']); ?>"<?php endif; ?>
>
<?php if (($group['view_mode'] ?? '') === 'img' && !empty($val['image'])) : ?>
<img src="<?php echo esc_url($val['image']); ?>" alt="<?php echo esc_attr($val['label']); ?>">
<?php endif; ?>
<span><?php echo esc_html((string) ($val['label'] ?? '')); ?></span>
<?php if ((float) ($val['price_change'] ?? 0) !== 0.0) : ?>
<em><?php echo (float) $val['price_change'] > 0 ? '+' : ''; ?><?php echo esc_html(number_format((float) $val['price_change'], 2)); ?></em>
<?php endif; ?>
</button>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<div class="aksrt-product-page__option-result" data-aksrt-option-result hidden>
<span data-aksrt-selected-labels></span>
<strong data-aksrt-resolved-price></strong>
<span data-aksrt-resolved-stock></span>
</div>
</div>
<?php endif; ?>
<!-- Meta info -->
<dl class="aksrt-product-page__meta">
<div>
<dt><?php esc_html_e('库存', 'aksrt'); ?></dt>
<dd data-aksrt-stock-display><?php
$stock_val = (int) ($product['stock'] ?? 0);
echo $stock_val <= -1 ? esc_html__('充足', 'aksrt') : esc_html((string) $stock_val);
?></dd>
</div>
<div>
<dt><?php esc_html_e('配送', 'aksrt'); ?></dt>
<dd><?php echo esc_html((string) (($product['shipping']['delivery_note'] ?? '') ?: __('标准配送', 'aksrt'))); ?></dd>
</div>
<div>
<dt><?php esc_html_e('运费', 'aksrt'); ?></dt>
<dd><?php
$ship_fee = (float) ($product['shipping']['fee'] ?? 0);
echo $ship_fee > 0 ? '¥' . esc_html(number_format($ship_fee, 2)) : esc_html__('包邮', 'aksrt');
?></dd>
</div>
</dl>
<!-- Params & Services quick links -->
<?php if (!empty($params)) : ?>
<button type="button" class="aksrt-product-page__info-trigger" data-aksrt-show-params>
<i class="fas fa-list-ul"></i> <?php esc_html_e('商品参数', 'aksrt'); ?> <i class="fas fa-chevron-right"></i>
</button>
<?php endif; ?>
<?php if (!empty($services)) : ?>
<button type="button" class="aksrt-product-page__info-trigger" data-aksrt-show-services>
<i class="fas fa-shield-alt"></i> <?php esc_html_e('服务保障', 'aksrt'); ?> <i class="fas fa-chevron-right"></i>
</button>
<?php endif; ?>
<!-- Purchase box -->
<div class="aksrt-product-page__purchase-box">
<label class="aksrt-product-page__field">
<span><?php esc_html_e('购买数量', 'aksrt'); ?></span>
<div class="aksrt-product-page__quantity">
<button type="button" class="aksrt-product-page__qty-btn" data-aksrt-qty-action="dec">-</button>
<input type="number" min="1" max="<?php echo esc_attr((string) max(1, $stock_val > 0 ? $stock_val : 999)); ?>" value="1" class="aksrt-product-page__qty-input" data-aksrt-quantity>
<button type="button" class="aksrt-product-page__qty-btn" data-aksrt-qty-action="inc">+</button>
</div>
</label>
<div class="aksrt-product-page__actions">
<?php if (is_user_logged_in() && !empty($product['purchase_enabled'])) : ?>
<button type="button" class="btn-outline btn-lg" data-aksrt-add-cart><?php esc_html_e('加入购物车', 'aksrt'); ?></button>
<button type="button" class="btn-primary btn-lg" data-aksrt-buy-now>
<?php echo $pay_modo === 'points' ? esc_html__('积分兑换', 'aksrt') : esc_html__('立即下单', 'aksrt'); ?>
</button>
<?php elseif (!is_user_logged_in()) : ?>
<a href="<?php echo esc_url(function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url(get_permalink())); ?>" class="btn-primary btn-lg">
<?php esc_html_e('登录后购买', 'aksrt'); ?>
</a>
<?php else : ?>
<button type="button" class="btn-primary btn-lg" disabled><?php esc_html_e('暂不可购买', 'aksrt'); ?></button>
<?php endif; ?>
</div>
<!-- Auxiliary actions -->
<div class="aksrt-product-page__aux-actions">
<button type="button" class="aksrt-product-page__aux-btn<?php echo $is_fav ? ' is-active' : ''; ?>" data-aksrt-toggle-fav="<?php echo esc_attr((string) $product_id); ?>">
<i class="<?php echo $is_fav ? 'fas' : 'far'; ?> fa-heart"></i>
<?php esc_html_e('收藏', 'aksrt'); ?>
</button>
<button type="button" class="aksrt-product-page__aux-btn" data-aksrt-share>
<i class="fas fa-share-alt"></i> <?php esc_html_e('分享', 'aksrt'); ?>
</button>
</div>
</div>
<!-- Merchant info -->
<?php if ($author) :
$store_url = function_exists('aksrt_get_user_home_url')
? add_query_arg('tab', 'product', aksrt_get_user_home_url($author_id))
: get_author_posts_url($author_id);
?>
<div class="aksrt-product-page__merchant">
<img src="<?php echo esc_url(function_exists('aksrt_get_avatar_url') ? aksrt_get_avatar_url($author_id) : get_avatar_url($author_id)); ?>" alt="" class="aksrt-product-page__merchant-avatar">
<div>
<strong><?php echo esc_html($author->display_name); ?></strong>
<a href="<?php echo esc_url($store_url); ?>" class="aksrt-product-page__merchant-link"><?php esc_html_e('进入店铺', 'aksrt'); ?></a>
</div>
</div>
<?php endif; ?>
</section>
</div>
<!-- Tabs: detail / reviews / custom -->
<div class="aksrt-product-page__tabs">
<nav class="aksrt-product-page__tab-nav">
<button class="aksrt-product-page__tab-btn is-active" data-aksrt-tab="detail"><?php esc_html_e('商品详情', 'aksrt'); ?></button>
<button class="aksrt-product-page__tab-btn" data-aksrt-tab="reviews">
<?php esc_html_e('评价', 'aksrt'); ?>
<?php if (($scores['total'] ?? 0) > 0) : ?>
<em>(<?php echo esc_html((string) $scores['total']); ?>)</em>
<?php endif; ?>
</button>
</nav>
<div class="aksrt-product-page__tab-panel is-active" data-aksrt-tab-panel="detail">
<div class="aksrt-product-page__content">
<?php echo wp_kses_post((string) ($product['content'] ?? '')); ?>
</div>
</div>
<div class="aksrt-product-page__tab-panel" data-aksrt-tab-panel="reviews">
<?php if (!empty($scores['total'])) : ?>
<div class="aksrt-product-page__review-summary shop-comment-header" win-ajax-replace="shop-comment-header">
<div class="comment-score">
<strong class="aksrt-product-page__review-avg"><?php echo esc_html((string) ($scores['average'] ?? '0')); ?></strong>
<span class="aksrt-product-page__review-level"><?php echo esc_html((string) ($scores['level'] ?? '')); ?></span>
</div>
<div class="aksrt-product-page__review-counts comment-filter">
<a href="javascript:;" class="comment-orderby c-theme badg" data-review-filter=""><?php esc_html_e('全部', 'aksrt'); ?></a>
<a href="javascript:;" class="comment-orderby badg" data-review-filter="has_image"><?php printf(esc_html__('有图 %d', 'aksrt'), (int) ($scores['has_image'] ?? 0)); ?></a>
<a href="javascript:;" class="comment-orderby badg" data-review-filter="good"><?php printf(esc_html__('好评 %d', 'aksrt'), (int) ($scores['good'] ?? 0)); ?></a>
<a href="javascript:;" class="comment-orderby badg" data-review-filter="bad"><?php printf(esc_html__('中/差评 %d', 'aksrt'), (int) ($scores['bad'] ?? 0)); ?></a>
</div>
</div>
<?php endif; ?>
<div class="aksrt-product-page__review-list shop-commentlist" data-aksrt-review-list data-product-id="<?php echo esc_attr((string) $product_id); ?>">
<?php foreach (($reviews['items'] ?? []) as $review) : ?>
<div class="aksrt-product-page__review-item comment" data-review-id="<?php echo esc_attr((string) ($review['id'] ?? '')); ?>">
<div class="aksrt-product-page__review-header comment-header">
<img src="<?php echo esc_url((string) ($review['user_avatar'] ?? '')); ?>" alt="" class="aksrt-product-page__review-avatar avatar-img comt-avatar">
<span class="aksrt-product-page__review-name"><?php echo esc_html((string) ($review['user_name'] ?? '')); ?></span>
<span class="aksrt-product-page__review-date"><?php echo esc_html((string) ($review['date'] ?? '')); ?></span>
<span class="aksrt-product-page__review-stars comment-score-badge">
<?php for ($s = 0; $s < 5; $s++) : ?>
<i class="<?php echo $s < (int) round((float) ($review['rating_average'] ?? 0)) ? 'fas' : 'far'; ?> fa-star"></i>
<?php endfor; ?>
</span>
</div>
<div class="aksrt-product-page__review-text comment-content">
<?php
$review_body = (string) ($review['content'] ?? '');
if ($review_body === '') {
echo esc_html__('用户未填写评价内容', 'aksrt');
} elseif (function_exists('aksrt_render_comment_content')) {
echo aksrt_render_comment_content($review_body); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
echo esc_html($review_body);
}
?>
</div>
<?php if (!empty($review['images'])) : ?>
<div class="aksrt-product-page__review-images comment-score-imgs imgbox-container">
<?php foreach ((array) $review['images'] as $rimg) : ?>
<img src="<?php echo esc_url($rimg); ?>" alt="" class="aksrt-product-page__review-img box-img">
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php if (empty($reviews['items'])) : ?>
<p class="aksrt-product-page__review-empty"><?php esc_html_e('暂无评价', 'aksrt'); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<!-- Related products -->
<?php
$related_items = [];
if ($shop && method_exists($shop, 'build_product_card_view')) {
$related_q = new WP_Query([
'post_type' => 'aksrt_product',
'posts_per_page' => 6,
'post_status' => 'publish',
'post__not_in' => [$product_id],
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => !empty($product['categories']) ? [[
'taxonomy' => 'aksrt_product_cat',
'field' => 'term_id',
'terms' => wp_list_pluck($product['categories'], 'term_id'),
]] : [],
'orderby' => 'rand',
]);
while ($related_q->have_posts()) {
$related_q->the_post();
$card = $shop->build_product_card_view((int) get_the_ID());
if (!empty($card['id'])) {
$related_items[] = $card;
}
}
wp_reset_postdata();
}
?>
<?php if (!empty($related_items)) : ?>
<section class="aksrt-product-page__related">
<h2 class="aksrt-product-page__related-title"><?php esc_html_e('相关推荐', 'aksrt'); ?></h2>
<div class="aksrt-product-archive__grid aksrt-product-archive__grid--default aksrt-product-page__related-grid">
<?php foreach ($related_items as $related_product) :
get_template_part('templates/parts/shop/product', 'card', [
'product' => $related_product,
'show_sales' => ((int) ($related_product['sales'] ?? 0) > 0),
]);
endforeach; ?>
</div>
</section>
<?php endif; ?>
</div>
<!-- Params modal -->
<?php if (!empty($params)) : ?>
<div class="aksrt-modal" data-aksrt-params-modal hidden>
<div class="aksrt-modal__overlay"></div>
<div class="aksrt-modal__dialog">
<div class="aksrt-modal__header">
<h3><?php esc_html_e('商品参数', 'aksrt'); ?></h3>
<button type="button" class="aksrt-modal__close" data-aksrt-modal-close>&times;</button>
</div>
<div class="aksrt-modal__body">
<table class="aksrt-product-page__params-table">
<?php foreach ($params as $param) : ?>
<tr>
<th><?php echo esc_html((string) ($param['name'] ?? '')); ?></th>
<td><?php echo esc_html((string) ($param['value'] ?? '')); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
</div>
<?php endif; ?>
<!-- Services modal -->
<?php if (!empty($services)) : ?>
<div class="aksrt-modal" data-aksrt-services-modal hidden>
<div class="aksrt-modal__overlay"></div>
<div class="aksrt-modal__dialog">
<div class="aksrt-modal__header">
<h3><?php esc_html_e('服务保障', 'aksrt'); ?></h3>
<button type="button" class="aksrt-modal__close" data-aksrt-modal-close>&times;</button>
</div>
<div class="aksrt-modal__body">
<?php foreach ($services as $service) : ?>
<div class="aksrt-product-page__service-item">
<?php if (!empty($service['icon'])) : ?>
<img src="<?php echo esc_url($service['icon']); ?>" alt="" class="aksrt-product-page__service-icon">
<?php else : ?>
<i class="fas fa-check-circle"></i>
<?php endif; ?>
<div>
<strong><?php echo esc_html((string) ($service['name'] ?? '')); ?></strong>
<?php if (!empty($service['desc'])) : ?>
<p><?php echo esc_html((string) $service['desc']); ?></p>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
</div><!-- .aksrt-product-page -->
</div><!-- .content-layout__primary -->
<?php if (!empty($has_shop_sidebar)) : ?>
<aside class="content-layout__sidebar aksrt-shop-widget-zone">
<?php dynamic_sidebar('shop_product_sidebar'); ?>
</aside>
<?php endif; ?>
</div><!-- .content-layout -->
</div><!-- .content-wrap -->
<?php if (is_active_sidebar('shop_product_bottom_fluid')) : ?>
<div class="aksrt-fluid-widget aksrt-fluid-widget--shop"><?php dynamic_sidebar('shop_product_bottom_fluid'); ?></div>
<?php endif; ?>
<!-- Mobile purchase tabbar -->
<div class="aksrt-product-page__mobile-bar" data-aksrt-mobile-purchase>
<button type="button" class="aksrt-product-page__mobile-btn" data-aksrt-toggle-fav="<?php echo esc_attr((string) $product_id); ?>">
<i class="<?php echo $is_fav ? 'fas' : 'far'; ?> fa-heart"></i>
</button>
<button type="button" class="aksrt-product-page__mobile-btn" data-aksrt-add-cart>
<i class="fas fa-shopping-cart"></i>
</button>
<button type="button" class="aksrt-product-page__mobile-buy btn-primary" data-aksrt-buy-now>
<?php echo $pay_modo === 'points' ? esc_html__('积分兑换', 'aksrt') : esc_html__('立即购买', 'aksrt'); ?>
</button>
</div>
<?php else : ?>
<div class="aksrt-product-page container">
<div class="aksrt-product-page__empty">
<i class="fas fa-box-open" aria-hidden="true"></i>
<p><?php esc_html_e('商品不存在或已下架。', 'aksrt'); ?></p>
</div>
</div>
<?php endif; ?>
</main>
<?php get_footer(); ?>