如果满足用户角色和产品条件,则隐藏 HTML 元素

问题描述 投票:0回答:1

我正在寻找以下条件。如果用户的用户角色为“客户”,并且购物车中的产品 ID 为 1、2 或 3,则不会为该用户显示特定的送货选项。我已经开始了:

<?php 
 $user = wp_get_current_user();
 if ('vendor', $user->roles ) {
 ?>

我不知道如何从这里继续。

php wordpress woocommerce user-roles
1个回答
0
投票

你可以简单地这样做。

function matched_cart_items( $search_products ) {
    $count = 0; // Initializing

    if ( ! WC()->cart->is_empty() ) {
        // Loop though cart items
        foreach(WC()->cart->get_cart() as $cart_item ) {
            // Handling also variable products and their products variations
            $cart_item_ids = array($cart_item['product_id'], $cart_item['variation_id']);

            // Handle a simple product Id (int or string) or an array of product Ids 
            if( ( is_array($search_products) && array_intersect($search_products, cart_item_ids) ) 
            || ( !is_array($search_products) && in_array($search_products, $cart_item_ids)
                $count++; // incrementing items count
        }
    }
    return $count; // returning matched items count 
}

// to Search Cart
if ( in_array( 'customer', (array) wp_get_current_user()->roles ) && (0 < matched_cart_items( 1, 2,3 )) ) {
 // Do your thing.
}

这个答案的一部分是从这里获取的:https://stackoverflow.com/a/41266005/8298248

© www.soinside.com 2019 - 2024. All rights reserved.