加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:56:11 所属栏目:PHP教程 来源:网络整理
导读:WordPress商店正在使用WooCommerce,我有一个小的购买说明,我需要在WooCommerce Checkout上显示,但仅限于购买某个产品时. 我添加了一条自定义消息,该消息现在显示在“下订单”按钮下方. 然而,无论购物车中是什么,它都会出现. 这是我目前的代码: add_action(
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>';
}

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

谢谢

解决方法

Here we check that we have a product item in cart with this special category. If the condition is matched (in one of the items of the cart),the message is displayed.

这是代码:

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>';
}

You can also use an array of products Ids instead of a product category…

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

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

此代码经过测试和运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读