我想为特定用户(如:Andy 等)设置 woocommerce 产品,而不是为特定角色(如:客户、编辑等)设置 woocommerce 产品,无需密码

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

我想使用 woocommerce 平台为特定用户/客户(如:Andy 等)上传产品,而不是为特定角色(如:客户、编辑等)上传产品。这样当客户登录他们的帐户时就能够看到该产品。那么任何人都可以帮助我吗?

注意:我已经知道我们可以设置受密码保护的产品,并且还知道我们可以将产品显示或隐藏为用户角色(例如:客户、编辑等),但我不希望这样。

我尝试了一些插件,但对我的要求没有任何帮助。

wordpress woocommerce product data-protection access-protection
1个回答
0
投票

试试这个代码:

add_action('profile_personal_options', 'sss_profile_fields', 11);
add_action('edit_user_profile', 'sss_profile_fields', 11);

function sss_profile_fields($user)
{

    $product_ids = get_user_meta($user->ID, 'user_base_product_ids', true);
    $args = array(
        'numberposts' => '-1',
        'post_type' => 'product'
    );
    $products = get_posts($args);

?>
    <h3>User Based Product</h3>
    <table class="form-table">
        <tr class="user-based-product-wrap">
            <th>
                <label for="user_products">Products</label>
            </th>
            <td>
                <?php if($products): ?>
                <select name="user_products[]" id="user_products" class="user_products" multiple="multiple">
                    <option value="">Select Products</option>
                    <?php
                    foreach($products as $product){
                        ?>
                        <option value="<?= $product->ID ?>" <?= !empty($product_ids) && in_array($product->ID,$product_ids)? "Selected":"" ?>> 
                            <?= $product->post_title ?> 
                        </option>
                        <?php
                    }
                    ?>
                </select>
                <?php endif; ?>
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        jQuery('.user_products').select2({
            placeholder: 'Select Product'
        });
    </script>
<?php
}

add_action( 'personal_options_update', 'sss_save_profile_fields' );
add_action( 'edit_user_profile_update', 'sss_save_profile_fields' );
 
function sss_save_profile_fields( $user_id ) {
    
    if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
        return;
    }
    
    if( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }
    
    if(isset($_POST['user_products'])){
        update_user_meta( $user_id, 'user_base_product_ids', $_POST['user_products'] );
    }else{
        delete_user_meta( $user_id, 'user_base_product_ids' );
    }
    
 
}

add_filter( 'woocommerce_product_is_visible', 'af_product_visible_by_user_role',10, 2 );
function af_product_visible_by_user_role($visible, $product_id)
{
    $products_to_hide = is_user_logged_in() ? get_user_meta(get_current_user_id(), 'user_base_product_ids', true)  : '';
    if (is_array($products_to_hide) && in_array($product_id, $products_to_hide)) {
        $visible = true;
    }else{
        $visible = false;
    }
    return $visible;
}

您将在每个用户上看到此选择产品的下拉列表。所选产品将仅向特定用户显示。

我希望它对你有用。

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