125 lines
4.6 KiB
PHP
125 lines
4.6 KiB
PHP
<?php
|
||
/**
|
||
* 模板:单个混装产品的 add-to-cart 表单
|
||
* 路径:templates/single-product/add-to-cart/yoone-bundle.php
|
||
*/
|
||
defined('ABSPATH') || exit;
|
||
|
||
global $product;
|
||
if (! $product || $product->get_type() !== Yoone_Product_Bundles::TYPE) {
|
||
return;
|
||
}
|
||
|
||
$config = Yoone_Product_Bundles::get_bundle_config($product);
|
||
$allowed = $config['allowed_products'];
|
||
$min_qty = max(0, absint($config['min_qty']));
|
||
$cat_ids = $config['categories'];
|
||
|
||
// 收集允许的商品对象,且必须是 simple product
|
||
$allowed_products = array();
|
||
foreach ($allowed as $pid) {
|
||
$p = wc_get_product($pid);
|
||
if ($p && $p->is_type('simple')) {
|
||
$allowed_products[$pid] = $p;
|
||
}
|
||
}
|
||
|
||
// 按分类分组(如果未配置分类,则全部在一个组内)
|
||
$groups = array();
|
||
if (! empty($cat_ids)) {
|
||
foreach ($allowed_products as $pid => $p) {
|
||
$terms = get_the_terms($pid, 'product_cat');
|
||
$matched = false;
|
||
if (is_array($terms)) {
|
||
foreach ($terms as $t) {
|
||
if (in_array($t->term_id, $cat_ids, true)) {
|
||
$groups[$t->term_id]['term'] = $t;
|
||
$groups[$t->term_id]['items'][] = $p;
|
||
$matched = true;
|
||
}
|
||
}
|
||
}
|
||
// 若商品不属于所选分类,则不显示
|
||
if (! $matched) {
|
||
// 跳过
|
||
}
|
||
}
|
||
} else {
|
||
$groups[0] = array('term' => null, 'items' => array_values($allowed_products));
|
||
}
|
||
|
||
// 表单开始
|
||
?>
|
||
<form class="cart yoone-bundle-form" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype="multipart/form-data">
|
||
<div class="yoone-bundle-meta">
|
||
<p class="yoone-bundle-min"><?php printf( esc_html__('至少选择 %d 件混装组件', 'yoone-product-bundles'), $min_qty ); ?></p>
|
||
<p class="yoone-bundle-selected"><?php esc_html_e('当前选择数量:', 'yoone-product-bundles'); ?><span class="yoone-bundle-selected-count">0</span></p>
|
||
</div>
|
||
|
||
<?php if (empty($groups)) : ?>
|
||
<p><?php esc_html_e('暂无可混装商品,请在后台为该混装产品配置。', 'yoone-product-bundles'); ?></p>
|
||
<?php else : ?>
|
||
<?php foreach ($groups as $gid => $group) : ?>
|
||
<div class="yoone-bundle-group">
|
||
<?php if (! empty($group['term'])) : ?>
|
||
<h3 class="yoone-bundle-group-title"><?php echo esc_html($group['term']->name); ?></h3>
|
||
<?php else : ?>
|
||
<h3 class="yoone-bundle-group-title"><?php esc_html_e('可混装商品', 'yoone-product-bundles'); ?></h3>
|
||
<?php endif; ?>
|
||
|
||
<table class="yoone-bundle-table">
|
||
<thead>
|
||
<tr>
|
||
<th><?php esc_html_e('商品', 'yoone-product-bundles'); ?></th>
|
||
<th><?php esc_html_e('价格', 'yoone-product-bundles'); ?></th>
|
||
<th style="width:140px;"><?php esc_html_e('数量', 'yoone-product-bundles'); ?></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($group['items'] as $item) : ?>
|
||
<?php $pid = $item->get_id(); ?>
|
||
<tr>
|
||
<td>
|
||
<a href="<?php echo esc_url(get_permalink($pid)); ?>" target="_blank"><?php echo esc_html($item->get_name()); ?></a>
|
||
</td>
|
||
<td>
|
||
<?php echo wp_kses_post($item->get_price_html()); ?>
|
||
</td>
|
||
<td>
|
||
<input type="number" min="0" step="1" class="yoone-bundle-qty" name="yoone_bundle_components[<?php echo esc_attr($pid); ?>]" value="0" />
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
|
||
<div class="yoone-bundle-actions">
|
||
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr($product->get_id()); ?>" />
|
||
<button type="submit" class="single_add_to_cart_button button alt" disabled><?php esc_html_e('加入购物车', 'yoone-product-bundles'); ?></button>
|
||
</div>
|
||
</form>
|
||
|
||
<script>
|
||
(function($){
|
||
function updateState(){
|
||
var total = 0;
|
||
$('.yoone-bundle-qty').each(function(){
|
||
var v = parseInt($(this).val(), 10);
|
||
if (!isNaN(v) && v > 0) total += v;
|
||
});
|
||
$('.yoone-bundle-selected-count').text(total);
|
||
var min = <?php echo (int) $min_qty; ?>;
|
||
var btn = $('.yoone-bundle-actions .single_add_to_cart_button');
|
||
if (total >= min) {
|
||
btn.prop('disabled', false);
|
||
} else {
|
||
btn.prop('disabled', true);
|
||
}
|
||
}
|
||
$(document).on('change keyup', '.yoone-bundle-qty', updateState);
|
||
$(document).ready(updateState);
|
||
})(jQuery);
|
||
</script>
|