xjr*_*owx 3 php wordpress product checkout woocommerce
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>';
}
Run Code Online (Sandbox Code Playgroud)
是否有一个简单的代码可以在此行之前添加,这使得它仅适用于购物车中某个类别的产品?
谢谢
在这里,我们检查我们在购物车中有这个特殊类别的产品项目.如果条件匹配(在购物车的其中一个项目中),则显示消息.
这是代码:
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>';
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用一系列产品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>';
}
Run Code Online (Sandbox Code Playgroud)
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
此代码经过测试和运行.
| 归档时间: |
|
| 查看次数: |
2843 次 |
| 最近记录: |