subscription/templates/bundle/bundle-options.php

267 lines
7.8 KiB
PHP

<?php
/**
* 混装产品选项模板
*
* 在产品页面显示混装选项
*/
if (!defined('ABSPATH')) {
exit;
}
global $product;
// 获取混装数据
$bundle_helper = new Yoone_Bundle();
$bundle = $bundle_helper->get_bundle_by_product_id($product->get_id());
if (!$bundle || $bundle->get_status() !== 'active') {
return;
}
$bundle_items = $bundle->get_bundle_items();
if (empty($bundle_items)) {
return;
}
?>
<div class="yoone-bundle-options" data-product-id="<?php echo esc_attr($product->get_id()); ?>">
<h3><?php _e('混装选项', 'yoone-subscriptions'); ?></h3>
<div class="yoone-bundle-info">
<p class="bundle-description"><?php echo esc_html($bundle->get_description()); ?></p>
<?php if ($bundle->get_discount_value() > 0): ?>
<div class="bundle-discount">
<?php if ($bundle->get_discount_type() === 'percentage'): ?>
<span class="discount-badge"><?php printf(__('混装优惠 %s%%', 'yoone-subscriptions'), $bundle->get_discount_value()); ?></span>
<?php else: ?>
<span class="discount-badge"><?php printf(__('混装优惠 %s', 'yoone-subscriptions'), wc_price($bundle->get_discount_value())); ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="yoone-bundle-items">
<?php foreach ($bundle_items as $item): ?>
<?php
$item_product = wc_get_product($item['product_id']);
if (!$item_product) continue;
?>
<div class="bundle-item" data-product-id="<?php echo esc_attr($item['product_id']); ?>">
<div class="item-image">
<?php echo $item_product->get_image('thumbnail'); ?>
</div>
<div class="item-details">
<h4 class="item-name"><?php echo esc_html($item_product->get_name()); ?></h4>
<p class="item-price"><?php echo $item_product->get_price_html(); ?></p>
<?php if (!empty($item['description'])): ?>
<p class="item-description"><?php echo esc_html($item['description']); ?></p>
<?php endif; ?>
</div>
<div class="item-quantity">
<label for="bundle_quantity_<?php echo esc_attr($item['product_id']); ?>">
<?php _e('数量', 'yoone-subscriptions'); ?>
</label>
<input
type="number"
id="bundle_quantity_<?php echo esc_attr($item['product_id']); ?>"
name="bundle_quantities[<?php echo esc_attr($item['product_id']); ?>]"
value="<?php echo esc_attr($item['default_quantity']); ?>"
min="<?php echo esc_attr($item['min_quantity']); ?>"
max="<?php echo esc_attr($item['max_quantity']); ?>"
step="1"
class="bundle-quantity-input"
/>
</div>
<div class="item-subtotal">
<span class="subtotal-label"><?php _e('小计:', 'yoone-subscriptions'); ?></span>
<span class="subtotal-amount" data-base-price="<?php echo esc_attr($item_product->get_price()); ?>">
<?php echo wc_price($item_product->get_price() * $item['default_quantity']); ?>
</span>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="yoone-bundle-summary">
<div class="bundle-total">
<div class="total-row original-total">
<span class="total-label"><?php _e('原价:', 'yoone-subscriptions'); ?></span>
<span class="total-amount" id="bundle-original-total">-</span>
</div>
<?php if ($bundle->get_discount_value() > 0): ?>
<div class="total-row discount-total">
<span class="total-label"><?php _e('优惠:', 'yoone-subscriptions'); ?></span>
<span class="total-amount discount" id="bundle-discount-amount">-</span>
</div>
<?php endif; ?>
<div class="total-row final-total">
<span class="total-label"><?php _e('混装价格:', 'yoone-subscriptions'); ?></span>
<span class="total-amount" id="bundle-final-total">-</span>
</div>
</div>
<div class="bundle-actions">
<button type="button" class="button yoone-calculate-bundle" id="calculate-bundle-price">
<?php _e('计算价格', 'yoone-subscriptions'); ?>
</button>
<button type="button" class="button alt yoone-add-bundle-to-cart" id="add-bundle-to-cart" disabled>
<?php _e('添加混装到购物车', 'yoone-subscriptions'); ?>
</button>
</div>
</div>
<?php if ($bundle->get_min_quantity() || $bundle->get_max_quantity()): ?>
<div class="bundle-quantity-info">
<?php if ($bundle->get_min_quantity()): ?>
<p class="min-quantity-info">
<?php printf(__('最小购买数量: %d', 'yoone-subscriptions'), $bundle->get_min_quantity()); ?>
</p>
<?php endif; ?>
<?php if ($bundle->get_max_quantity()): ?>
<p class="max-quantity-info">
<?php printf(__('最大购买数量: %d', 'yoone-subscriptions'), $bundle->get_max_quantity()); ?>
</p>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<style>
.yoone-bundle-options {
margin: 20px 0;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background: #f9f9f9;
}
.yoone-bundle-options h3 {
margin-top: 0;
color: #333;
}
.bundle-discount .discount-badge {
background: #e74c3c;
color: white;
padding: 5px 10px;
border-radius: 3px;
font-size: 12px;
font-weight: bold;
}
.yoone-bundle-items {
margin: 20px 0;
}
.bundle-item {
display: flex;
align-items: center;
padding: 15px;
margin-bottom: 15px;
background: white;
border: 1px solid #eee;
border-radius: 5px;
}
.bundle-item .item-image {
flex: 0 0 80px;
margin-right: 15px;
}
.bundle-item .item-details {
flex: 1;
margin-right: 15px;
}
.bundle-item .item-name {
margin: 0 0 5px 0;
font-size: 16px;
}
.bundle-item .item-price {
margin: 0 0 5px 0;
font-weight: bold;
color: #e74c3c;
}
.bundle-item .item-quantity {
flex: 0 0 100px;
margin-right: 15px;
}
.bundle-item .bundle-quantity-input {
width: 60px;
text-align: center;
}
.bundle-item .item-subtotal {
flex: 0 0 120px;
text-align: right;
}
.yoone-bundle-summary {
background: white;
padding: 20px;
border-radius: 5px;
border: 1px solid #eee;
}
.bundle-total .total-row {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
padding: 5px 0;
}
.bundle-total .final-total {
border-top: 1px solid #eee;
padding-top: 10px;
font-weight: bold;
font-size: 18px;
}
.bundle-total .discount {
color: #e74c3c;
}
.bundle-actions {
margin-top: 20px;
text-align: center;
}
.bundle-actions .button {
margin: 0 10px;
}
.bundle-quantity-info {
margin-top: 15px;
font-size: 12px;
color: #666;
}
@media (max-width: 768px) {
.bundle-item {
flex-direction: column;
text-align: center;
}
.bundle-item .item-image,
.bundle-item .item-details,
.bundle-item .item-quantity,
.bundle-item .item-subtotal {
margin: 10px 0;
}
}
</style>