在Woocommerce中,我想添加一个具有特定数量的额外加入购物车按钮。
答案“在Woocommerce单个产品页面上添加具有固定数量的附加加入购物车按钮”大多数情况下都不适用,我只需要为任何产品类型的特定产品启用该代码。
这是可能的吗?我需要改变什么...
答案“在Woocommerce单个产品页面上添加具有固定数量的附加加入购物车按钮”大多数情况下都不适用,我只需要为任何产品类型的特定产品启用该代码。
这是可能的吗?我需要改变什么...
处理产品变体(对于可变产品)需要添加一些JavaScript/jQuery代码,这更加复杂。
我将产品设置限制从主要功能中分离出来,并在自定义条件函数中。您可以选择以下两种方式之一:
1)您可以使用产品ID限制:
// PRODUCT IDs based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
// HERE define your Product Ids in the array
$product_ids = array(37, 40, 53);
return in_array( $product_ids, $product_id ) ? true : false;
}
2) 或者您可以使用产品类别限制:
// PRODUCT CATEGORY based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
// HERE define your Product Category terms in the array (can be term Ids, names or slugs)
$terms = array('t-shirts', 'socks', 'glasses');
return has_term( $terms, 'product_cat', $product_id ) ? true : false;
}
// Additional add to cart button with fixed bulk quantity
add_action( 'woocommerce_after_add_to_cart_button', 'additional_add_to_cart_button', 20 );
function additional_add_to_cart_button() {
global $product;
if( ! enable_additional_add_to_cart_button( $product->get_id() ) ) return;
$qty = 12;
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity='.$qty;
$class = 'single_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$btn_txt = __( "Add a case of 12", "woocommerce" );
## ---------------------- For non variable products ----------------------- ##
if( ! $product->is_type('variable') ) :
// Output
echo '<br><a href"'.$href.'"= rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';
## ---------- For variable products (handling product variations) --------- ##
else :
// Output
echo '<br><a rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';
$data = array();
// Loop through available variations data
foreach( $product->get_available_variations() as $variation_data ){
if( $variation_data['is_in_stock'] && $variation_data['is_purchasable'] ) {
$variation_id = $variation_data['variation_id'];
$attributes_str = '';
// Loop through variation attributes
foreach ( $variation_data['attributes'] as $attribute_taxonomy => $term_slug ) {
$attributes_str .= '&'.$attribute_taxonomy.'='.$term_slug;
}
// Set product attributes pairs for variations
$data[$variation_id] = $href . '&variation_id=' . $variation_id . $attributes_str;
}
}
?>
<script type="text/javascript">
jQuery(function($){
var a = <?php echo json_encode($data); ?>, b = '.single_add_to_cart_button-12',
c = 'wc-variation-selection-needed', d = 'disabled',
f = 'form.variations_form', h = $(c).attr('href'),
s = '.variations select', i = 'input.variation_id';
// On show and hide variation event
$(f).on('show_variation', function() {
var found = false;
$.each(a, function(j,k){
if( $(i).val() == j ) {
found = true;
if( $(b).hasClass(c) && $(b).hasClass(d) )
$(b).removeClass(c).removeClass(d).attr('href',k);
}
});
if( ! found && ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
$(b).addClass(c).addClass(d).removeAttr("href");
}).on('hide_variation', function() {
if( ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
$(b).addClass(c).addClass(d).removeAttr("href");
});
});
</script>
<?php
endif;
}
感谢您提供的所有澄清信息。 我只会有单一产品类型,但是我会有一些产品需要添加到购物车中的6瓶,另外一些产品需要添加到购物车中的12瓶。 我在想是否可以在产品页面上添加一个选项,例如自定义字段,例如wine-box,我可以填写6或12,并显示为额外的添加到购物车按钮(添加6箱文本或添加12箱文本)。 谢谢
您可以使用 WC_prroduct->get_id() 函数来检查产品 ID
// 仅适用于 ID 为16的产品 if( $product->get_id() != 16 ) return;
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=12';
$class = 'ingle_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "Add a case of 12", "woocommerce" );
// Output
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
(!)
,所以我试图找出代码中是否还有其他错误,并告诉你。但是,我理解一段php
代码的速度无法与那些只需看一眼就知道其含义的人相比。正如我在两个评论中所说,我进行了一些测试,当然我会接受你的问题,我甚至可以对得到的答案提供奖励。但是你很快就删除了答案,这是你的权利。如果这一切曾经冒犯或打扰到你,我只是感到抱歉。 - undefined