subscription/templates/frontend/bundle-product.php

472 lines
13 KiB
PHP

<?php
/**
* 混装产品前端展示模板
*
* @package Yoone_Subscriptions
* @version 1.0.0
*/
if (!defined('ABSPATH')) {
exit; // 防止直接访问
}
global $product;
// 获取混装产品数据
$bundle_id = $product->get_meta('_yoone_bundle_id');
if (!$bundle_id) {
return;
}
global $wpdb;
// 获取混装详情
$bundle = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}yoone_bundles WHERE id = %d AND status = 'active'",
$bundle_id
));
if (!$bundle) {
return;
}
// 获取混装商品
$bundle_items = $wpdb->get_results($wpdb->prepare(
"SELECT bi.*, p.post_title as product_name, p.post_excerpt as product_excerpt
FROM {$wpdb->prefix}yoone_bundle_items bi
LEFT JOIN {$wpdb->posts} p ON bi.product_id = p.ID
WHERE bi.bundle_id = %d AND p.post_status = 'publish'
ORDER BY bi.sort_order ASC",
$bundle->id
));
if (empty($bundle_items)) {
return;
}
// 计算总价和折扣
$original_total = 0;
$bundle_total = 0;
foreach ($bundle_items as $item) {
$item_product = wc_get_product($item->product_id);
if ($item_product) {
$item_price = $item_product->get_price();
$original_total += $item_price * $item->quantity;
}
}
// 计算折扣后价格
if ($bundle->discount_type === 'percentage') {
$bundle_total = $original_total * (1 - $bundle->discount_value / 100);
} else {
$bundle_total = max(0, $original_total - $bundle->discount_value);
}
$savings = $original_total - $bundle_total;
$savings_percentage = $original_total > 0 ? ($savings / $original_total) * 100 : 0;
?>
<div class="yoone-bundle-product" data-bundle-id="<?php echo esc_attr($bundle->id); ?>">
<!-- 混装标识 -->
<div class="bundle-badge">
<span class="bundle-label"><?php _e('混装优惠', 'yoone-subscriptions'); ?></span>
<?php if ($savings_percentage > 0): ?>
<span class="bundle-savings"><?php printf(__('省 %.0f%%', 'yoone-subscriptions'), $savings_percentage); ?></span>
<?php endif; ?>
</div>
<!-- 混装描述 -->
<?php if ($bundle->description): ?>
<div class="bundle-description">
<h4><?php _e('套装说明', 'yoone-subscriptions'); ?></h4>
<p><?php echo wp_kses_post($bundle->description); ?></p>
</div>
<?php endif; ?>
<!-- 混装商品列表 -->
<div class="bundle-items">
<h4><?php _e('套装包含', 'yoone-subscriptions'); ?></h4>
<div class="bundle-items-list">
<?php foreach ($bundle_items as $item): ?>
<?php
$item_product = wc_get_product($item->product_id);
if (!$item_product) continue;
$item_price = $item_product->get_price();
$item_total = $item_price * $item->quantity;
?>
<div class="bundle-item" data-product-id="<?php echo esc_attr($item->product_id); ?>">
<div class="bundle-item-image">
<?php echo $item_product->get_image('thumbnail'); ?>
</div>
<div class="bundle-item-details">
<h5 class="bundle-item-title">
<a href="<?php echo get_permalink($item->product_id); ?>">
<?php echo esc_html($item->product_name); ?>
</a>
</h5>
<?php if ($item->product_excerpt): ?>
<p class="bundle-item-excerpt"><?php echo esc_html($item->product_excerpt); ?></p>
<?php endif; ?>
<div class="bundle-item-meta">
<span class="bundle-item-quantity">
<?php printf(__('数量: %d', 'yoone-subscriptions'), $item->quantity); ?>
</span>
<span class="bundle-item-price">
<?php echo Yoone_Helper::format_price($item_price); ?>
<?php if ($item->quantity > 1): ?>
<small>(<?php echo Yoone_Helper::format_price($item_total); ?>)</small>
<?php endif; ?>
</span>
</div>
</div>
<div class="bundle-item-actions">
<a href="<?php echo get_permalink($item->product_id); ?>" class="view-product" target="_blank">
<?php _e('查看详情', 'yoone-subscriptions'); ?>
</a>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- 价格对比 -->
<div class="bundle-pricing">
<div class="pricing-comparison">
<div class="original-price">
<span class="label"><?php _e('单独购买:', 'yoone-subscriptions'); ?></span>
<span class="price"><?php echo Yoone_Helper::format_price($original_total); ?></span>
</div>
<div class="bundle-price">
<span class="label"><?php _e('套装价格:', 'yoone-subscriptions'); ?></span>
<span class="price"><?php echo Yoone_Helper::format_price($bundle_total); ?></span>
</div>
<?php if ($savings > 0): ?>
<div class="savings-amount">
<span class="label"><?php _e('您节省:', 'yoone-subscriptions'); ?></span>
<span class="savings"><?php echo Yoone_Helper::format_price($savings); ?></span>
<span class="percentage">(<?php printf('%.0f%%', $savings_percentage); ?>)</span>
</div>
<?php endif; ?>
</div>
</div>
<!-- 混装选项 -->
<div class="bundle-options">
<div class="bundle-option-field">
<label>
<input type="checkbox" name="yoone_bundle_purchase" value="1" checked>
<?php _e('购买整个套装', 'yoone-subscriptions'); ?>
<span class="bundle-option-price"><?php echo Yoone_Helper::format_price($bundle_total); ?></span>
</label>
<p class="bundle-option-description">
<?php _e('选择此选项将以优惠价格购买整个套装', 'yoone-subscriptions'); ?>
</p>
</div>
</div>
</div>
<style>
.yoone-bundle-product {
border: 2px solid #e1e1e1;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
background: #f9f9f9;
}
.yoone-bundle-product .bundle-badge {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
}
.yoone-bundle-product .bundle-label {
background: #46b450;
color: white;
padding: 4px 12px;
border-radius: 15px;
font-size: 12px;
font-weight: bold;
text-transform: uppercase;
}
.yoone-bundle-product .bundle-savings {
background: #dc3232;
color: white;
padding: 4px 12px;
border-radius: 15px;
font-size: 12px;
font-weight: bold;
}
.yoone-bundle-product .bundle-description {
margin-bottom: 20px;
padding: 15px;
background: white;
border-radius: 5px;
}
.yoone-bundle-product .bundle-description h4 {
margin: 0 0 10px 0;
color: #333;
}
.yoone-bundle-product .bundle-items h4 {
margin: 0 0 15px 0;
color: #333;
border-bottom: 2px solid #46b450;
padding-bottom: 5px;
}
.yoone-bundle-product .bundle-items-list {
display: grid;
gap: 15px;
}
.yoone-bundle-product .bundle-item {
display: flex;
align-items: center;
gap: 15px;
padding: 15px;
background: white;
border-radius: 5px;
border: 1px solid #e1e1e1;
}
.yoone-bundle-product .bundle-item-image {
flex-shrink: 0;
width: 80px;
}
.yoone-bundle-product .bundle-item-image img {
width: 100%;
height: auto;
border-radius: 3px;
}
.yoone-bundle-product .bundle-item-details {
flex: 1;
}
.yoone-bundle-product .bundle-item-title {
margin: 0 0 5px 0;
font-size: 16px;
}
.yoone-bundle-product .bundle-item-title a {
color: #333;
text-decoration: none;
}
.yoone-bundle-product .bundle-item-title a:hover {
color: #46b450;
}
.yoone-bundle-product .bundle-item-excerpt {
margin: 0 0 10px 0;
color: #666;
font-size: 14px;
}
.yoone-bundle-product .bundle-item-meta {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 14px;
}
.yoone-bundle-product .bundle-item-quantity {
color: #666;
}
.yoone-bundle-product .bundle-item-price {
font-weight: bold;
color: #46b450;
}
.yoone-bundle-product .bundle-item-actions {
flex-shrink: 0;
}
.yoone-bundle-product .view-product {
display: inline-block;
padding: 8px 15px;
background: #46b450;
color: white;
text-decoration: none;
border-radius: 3px;
font-size: 12px;
transition: background-color 0.3s;
}
.yoone-bundle-product .view-product:hover {
background: #3a9b3f;
color: white;
}
.yoone-bundle-product .bundle-pricing {
margin: 20px 0;
padding: 20px;
background: white;
border-radius: 5px;
border: 2px solid #46b450;
}
.yoone-bundle-product .pricing-comparison {
display: grid;
gap: 10px;
}
.yoone-bundle-product .pricing-comparison > div {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 0;
}
.yoone-bundle-product .original-price {
border-bottom: 1px solid #e1e1e1;
}
.yoone-bundle-product .original-price .price {
text-decoration: line-through;
color: #999;
}
.yoone-bundle-product .bundle-price .price {
font-size: 18px;
font-weight: bold;
color: #46b450;
}
.yoone-bundle-product .savings-amount {
background: #f0f8f0;
padding: 10px;
border-radius: 3px;
margin-top: 10px;
}
.yoone-bundle-product .savings-amount .savings {
font-weight: bold;
color: #dc3232;
}
.yoone-bundle-product .savings-amount .percentage {
color: #dc3232;
font-weight: bold;
}
.yoone-bundle-product .bundle-options {
margin-top: 20px;
padding: 15px;
background: white;
border-radius: 5px;
}
.yoone-bundle-product .bundle-option-field label {
display: flex;
align-items: center;
gap: 10px;
font-weight: bold;
cursor: pointer;
}
.yoone-bundle-product .bundle-option-price {
color: #46b450;
font-size: 16px;
}
.yoone-bundle-product .bundle-option-description {
margin: 10px 0 0 30px;
color: #666;
font-size: 14px;
}
@media (max-width: 768px) {
.yoone-bundle-product .bundle-item {
flex-direction: column;
text-align: center;
}
.yoone-bundle-product .bundle-item-meta {
flex-direction: column;
gap: 5px;
}
.yoone-bundle-product .pricing-comparison > div {
flex-direction: column;
gap: 5px;
text-align: center;
}
.yoone-bundle-product .bundle-option-field label {
flex-direction: column;
text-align: center;
gap: 5px;
}
.yoone-bundle-product .bundle-option-description {
margin-left: 0;
text-align: center;
}
}
</style>
<script>
jQuery(document).ready(function($) {
// 混装选项切换
$('.yoone-bundle-product input[name="yoone_bundle_purchase"]').on('change', function() {
var $bundleProduct = $(this).closest('.yoone-bundle-product');
var bundleId = $bundleProduct.data('bundle-id');
if ($(this).is(':checked')) {
// 选择混装购买
$bundleProduct.addClass('bundle-selected');
// 更新产品价格显示
var bundlePrice = '<?php echo $bundle_total; ?>';
$('.price .woocommerce-Price-amount').text('<?php echo Yoone_Helper::format_price($bundle_total); ?>');
// 添加隐藏字段
if ($('input[name="yoone_bundle_id"]').length === 0) {
$('<input>').attr({
type: 'hidden',
name: 'yoone_bundle_id',
value: bundleId
}).appendTo('.cart');
}
} else {
// 取消混装购买
$bundleProduct.removeClass('bundle-selected');
// 恢复原价格显示
var originalPrice = '<?php echo $product->get_price(); ?>';
$('.price .woocommerce-Price-amount').text('<?php echo Yoone_Helper::format_price($product->get_price()); ?>');
// 移除隐藏字段
$('input[name="yoone_bundle_id"]').remove();
}
});
// 初始化状态
if ($('.yoone-bundle-product input[name="yoone_bundle_purchase"]').is(':checked')) {
$('.yoone-bundle-product input[name="yoone_bundle_purchase"]').trigger('change');
}
});
</script>