![](/img/trans.png)
[英]Display the terms or product attributes for a specific product category(woocommerce)
[英]Custom terms and conditions checkbox for a specific product category in WooCommerce
在 Woocommerce 网站上,根据购物车中的产品类别,我需要提供不同的条款和条件。 这部分工作没有问题。
但是,当触发特定于产品的术语时,我需要将它们作为必填字段。 这是有效的,除了当指定的类别不在购物车中时它也会阻止结账。
这是我的代码:
add_action('woocommerce_review_order_before_submit', 'add_pipletz_terms', 9);
function add_pipletz_terms() {
// Show Terms 1
$special_cat = 'insurance'; // HERE set your special category name, slug or ID
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( 'insurance', 'product_cat', $item->id ) )
$bool = true;
}
if ( $bool ) {
?>
<p class="form-row terms wc-terms-and-conditions">
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox pipletz-terms" name="pipletz-terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms-1'] ) ), true ); ?> id="pipletz-terms"> <span><a href="https://businessbloomer.com/woocommerce-display-product-specific-tc-checkout/" target="_blank">I’ve read and accept the Pipletz product terms & conditions</a></span> <span class="required">*</span>
</label>
</p>
<?php
}
}
// If customer does not agree to terms
add_action('woocommerce_checkout_process', 'not_approved_pipletz_terms');
function not_approved_pipletz_terms() {
if ( empty( $_POST['pipletz-terms'] ) ) {
wc_add_notice( __( 'Please agree to the Pipletz product terms & conditions' ), 'error' ); }
}
任何有关如何仅在类别存在时才需要的条款的帮助将不胜感激。
更新- 首先,此代码对于 woocommerce 版本 3+ 来说有点过时了……所以我添加了 WC 3+ 兼容性代码。
为了避免这个问题,您还需要检查购物车项目中特殊产品类别的第二个函数(因此您也需要添加 foreach 循环)......
同样在 foreach 循环中,一旦找到产品,您就可以打破循环……最后,在您的代码中, isset( $_POST['terms-1'] )
应该改为isset( $_POST['pipletz-terms'] )
工作…
所以你兼容的 WC3+ 代码应该是:
add_action('woocommerce_review_order_before_submit', 'add_pipletz_terms', 9 );
function add_pipletz_terms() {
$special_cat = 'insurance'; // HERE set your special category name, slug or ID
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
// compatibility with WC +3
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
if ( has_term( $special_cat, 'product_cat', $product_id ) ){
$bool = true;
break; // added this too
}
}
if ( $bool ) {
$link = 'https://businessbloomer.com/woocommerce-display-product-specific-tc-checkout/';
?>
<p class="form-row terms wc-terms-and-conditions">
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox pipletz-terms" name="pipletz-terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['pipletz-terms'] ) ), true ); ?> id="pipletz-terms">
<span>
<a href="<?php echo $link; ?>" target="_blank">I’ve read and accept the Pipletz product terms & conditions</a>
</span> <span class="required">*</span>
</label>
</p>
<?php
}
}
// If customer does not agree to terms
add_action('woocommerce_checkout_process', 'not_approved_pipletz_terms');
function not_approved_pipletz_terms() {
$special_cat = 'insurance'; // HERE set your special category name, slug or ID
$bool = false;
// Checking again if the category is in one cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
// compatibility with WC +3
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
if ( has_term( $special_cat, 'product_cat', $product_id ) ){
$bool = true;
break; // added this too
}
}
if ( empty( $_POST['pipletz-terms'] ) && $bool )
wc_add_notice( __( 'Please agree to the Pipletz product terms & conditions' ), 'error' );
}
我已经在 WC3+ 上成功测试了这段代码,没有问题,所以它现在适用于所有情况......
您认为在需要基于VENDOR (而不是产品或产品目录)的T&C服务时,可以使用这种方法吗? 说明:如果是市场,供应商应能够展示自己的条款和条件。 因此,在结帐页面上,应出现一个或多个客户必须检查的框(如果订单中有多个供应商,则将其复选)。 那有意义吗 ? 我已经检查了很多话题,但这是唯一一个实际对待这个话题的话题。 再见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.