将产品变体添加到产品页面
问题描述:
我正在研究http://fitboxmeals.com/order/,我想在我的woocommerce主题的function.php中使用此代码段,以便我的产品变体显示在膳食产品页面上。将产品变体添加到产品页面
if (! function_exists(‘woocommerce_template_loop_add_to_cart’)) { function woocommerce_template_loop_add_to_cart() { global $product; if ($product->product_type == "variable") { woocommerce_variable_add_to_cart(); } else { woocommerce_get_template(
'loop/add-to-cart.php'); } } }
代码工作,但现在当我将产品添加到购物车,我想再次对相同的产品或不同的一个复位的变化,使用户可以选择那些变化在产品添加到购物车后。
if (! defined('ABSPATH')) exit; // Exit if accessed directly global $woocommerce, $product, $post; ?>
<?php do_action('woocommerce_before_add_to_cart_form'); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr(json_encode($available_variations)) ?>">
<?php if (! empty($available_variations)) : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php $loop=0 ; foreach ($attributes as $name=>$options) : $loop++; ?>
<tr>
<td class="label">
<label for="<?php echo sanitize_title($name); ?>">
<?php echo wc_attribute_label($name); ?>
</label>
</td>
<td class="value">
<select id="<?php echo esc_attr(sanitize_title($name)); ?>" name="attribute_<?php echo sanitize_title($name); ?>">
<option value="">
<?php echo __('Choose an option', ETHEME_DOMAIN) ?>…</option>
<?php if (is_array($options)) { if (isset($_REQUEST[ 'attribute_' . sanitize_title($name) ])) { $selected_value=$ _REQUEST[ 'attribute_' . sanitize_title($name) ]; } elseif (isset($selected_attributes[ sanitize_title($name) ])) { $selected_value=$
selected_attributes[ sanitize_title($name) ]; } else { $selected_value='' ; } // Get terms if this is a taxonomy - ordered if (taxonomy_exists($name)) { $orderby=w c_attribute_orderby($name); switch ($orderby) { case 'name' : $args=a
rray('orderby'=>'name', 'hide_empty' => false, 'menu_order' => false); break; case 'id' : $args = array('orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false); break; case 'menu_order' : $args = array('menu_order' => 'ASC',
'hide_empty' => false); break; } $terms = get_terms($name, $args); foreach ($terms as $term) { if (! in_array($term->slug, $options)) continue; echo '
<option value="' . esc_attr($term->slug) . '"
' . selected(sanitize_title($selected_value), sanitize_title($term->slug), false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>'; } } else { foreach ($options as $option) { echo '
<option value="' . esc_attr(sanitize_title($option)) . '"
' . selected(sanitize_title($selected_value), sanitize_title($option), false) . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>'; } } } ?>
</select>
<?php if (sizeof($attributes)==$ loop) echo '<a class="reset_variations" href="#reset">' . __('Clear selection', ETHEME_DOMAIN) . '</a>'; ?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action('woocommerce_before_add_to_cart_button'); ?>
<div class="single_variation_wrap" style="display:none;">
<?php do_action('woocommerce_before_single_variation'); ?>
<div class="single_variation"></div>
<div class="variations_button">
<?php woocommerce_quantity_input(); ?>
<button type="submit" class="single_add_to_cart_button button big active alt"><span><?php echo $product->single_add_to_cart_text(); ?></span>
</button>
</div>
<input type="hidden" name="add-to-cart" value="<?php echo $product->id; ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr($post->ID); ?>" />
<input type="hidden" name="variation_id" value="" />
<?php do_action('woocommerce_after_single_variation'); ?>
</div>
<?php do_action('woocommerce_after_add_to_cart_button'); ?>
<?php else : ?>
<p class="stock out-of-stock">
<?php _e('This product is currently out of stock and unavailable.', ETHEME_DOMAIN); ?>
</p>
<?php endif; ?>
</form>
<?php do_action('woocommerce_after_add_to_cart_form'); ?>
我希望这可以帮助,任何建议将不胜感激。
答
事件左撇子真的应该在JavaScript/jQuery的附加,而不是HTML属性,但因为你的setTimeout为你工作那么应该是这样的:
<form onsubmit="
$.ajax({
url: location.href,
type: 'post',
headers: {
'enctype': 'multipart/form-data'
},
data: $(this).serialize()
}).then(function() {
//tidy the page
$('select.whatever').each(function() {
this.selectedIndex = 0;
}).eq(0).trigger('change');
});
return false;" class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr(json_encode($available_variations)) ?>" >
我从来没有尝试定义相当如此广泛事件处理程序,但是,AFAIK,它可以工作(调试时)。
您将需要更改应用于产品变体选择菜单的类别名称。目前它们似乎具有相同的ID,因此您应该可以简单地将id=
更改为class=
中的<select>
标签。
+0
太棒了,我会给你一个旋转,看看它在哪里。我要感谢你Roamer-1888。 – 2014-11-10 18:55:39
第二对是难以遵循,部分原因是由于缺少标点。 – 2014-11-07 02:42:15
嗨,Roamer-1888,我刚刚更新了帖子。希望它比以前多一点。感谢您看看它。任何建议将不胜感激。 – 2014-11-07 03:52:52
RoccRocc,不幸的是我不是正确的人回答你的问题。我只是对措辞做了一些质量控制。虽然我仍然觉得难以形象化,但在编辑之后好得多。我认为进一步的改善将是更多的肉“重新设置变化” - 这实际上意味着什么? – 2014-11-07 04:03:27