Wordpress 商店正在使用 WooCommerce,我有一个小的购买说明,我需要出现在 WooCommerce Checkout 上,但仅限于购买特定产品时。
我添加了一条自定义消息,该消息现在显示在“下订单”按钮下方。
然而,无论购物车中有什么,它都会出现。
这是我目前的代码:
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
echo '<div class="checkoutdisc">Custom message appears here fine.</div>';
}
我可以在这一行之前添加一个简单的代码吗?仅适用于特定类别产品在购物车中吗?
Thanks
在这里,我们检查购物车中是否有产品这个特殊类别。如果条件匹配(在购物车的一项中),则会显示该消息。
这是代码:
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
// set your special category name, slug or ID here:
$special_cat = 'special_category';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat, 'product_cat', $item->id ) )
$bool = true;
}
// If the special cat is detected in one items of the cart
// It displays the message
if ($bool)
echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}
您还可以使用一系列产品 ID而不是产品类别...
在这种情况下,代码会有点不同:
add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
// set your products IDs here:
$product_ids = array( 31, 68, 87, 124);
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( in_array( $item->id, $product_ids ) )
$bool = true;
}
// If the special cat is detected in one items of the cart
// It displays the message
if ($bool)
echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}
此代码位于活动子主题(或主题)的 function.php 文件中,或者也位于任何插件文件中.
这段代码已经过测试并且可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)