php – 基于特定产品类别的WooCommerce结帐消息

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 基于特定产品类别的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>';
}

是否一个简单代码可以在此行之前添加,这使得它仅适用于购物车中某个类别的产品?

谢谢

解决方法

这是代码

add_action( 'woocommerce_after_checkout_form',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>';
}

在这种情况下,代码将有点不同:

add_action( 'woocommerce_after_checkout_form',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文件中,或者也可以放在任何插件文件中.

代码经过测试和运行.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 基于特定产品类别的WooCommerce结帐消息全部内容,希望文章能够帮你解决php – 基于特定产品类别的WooCommerce结帐消息所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。