简体   繁体   English

在项目购买时隐藏 wordpress 菜单

[英]hide wordpress menu on item purchase

I'm trying to build a wordpress website where I want to show/hide the menu items on item purchase.我正在尝试建立一个 wordpress 网站,我想在其中显示/隐藏购买项目时的菜单项。 Purchasing the item through WooCommerce plugin.通过 WooCommerce 插件购买商品。

eg If I buy an item, the link related to product should come to the menu as menu item.例如,如果我购买了一个项目,与产品相关的链接应该作为菜单项出现在菜单中。 If someone could hint me how can I do it.如果有人可以提示我我该怎么做。 Doesn't matter If I have to code or edit the code , I'll.没关系如果我必须编码或编辑代码,我会。

As your question is not very clear, I suppose you want to get all bought items by a customer (user ID) and to display them as a kind of list or menu.由于您的问题不是很清楚,我想您想获取客户(用户 ID)购买的所有商品并将它们显示为一种列表或菜单。

Below you will find 2 functions.您将在下面找到 2 个函数。
1) first one will get all bought product IDs for the current customer (with an optional argument, the $user_id ). 1) 第一个将获得当前客户购买的所有产品 ID(带有可选参数$user_id )。
2) Second one will display a menu (or a list) for that products with titles and links… 2)第二个将显示带有标题和链接的该产品的菜单(或列表)......

Here is this code (goes in function.php file of your active child theme ore theme) :这是此代码(位于活动子主题矿石主题的 function.php 文件中)

function get_customer_products( $user_id = null ){

    if( empty($user_id) && is_user_logged_in() )
        $user_id = get_current_user_id();

    if( ! empty($user_id) && ! is_admin() ){
        $customer_orders = get_posts( array(
            'meta_key' => '_customer_user',
            'meta_value' => $user_id,
            'post_type'   => 'shop_order',
            'numberposts' => -1,
            'post_status' => 'wc-completed', // 'completed' order status
        ) );

        $product_ids = array();

        foreach($customer_orders as $customer_order){
            $_order = wc_get_order( $customer_order->ID );
            foreach($_order->get_items() as $item){
                // Avoiding duplicates
                if(!in_array($item['product_id'], $product_ids))
                    $product_ids[] = $item['product_id'];
            }
        }
        return $product_ids;
    }
}

function display_customer_product_list(){
    // Getting current customer bought products IDs
    $product_ids = get_customer_products();
    if(!empty($product_ids)){
        $output_html = '<div class="custom-product"><ul class="custom-menu">';
        foreach( $product_ids as $product_id ){
            $product = new WC_Product($product_id);
            $output_html .= '<li><a href="'.$product->get_permalink().'">'.$product->get_title().'</a></li>';
        }
        $output_html .= '</ul></div>';

        echo $output_html;
    }
}

USAGE用法

Then you can use everywhere, in your theme php templates/files, this way:然后你可以在任何地方使用,在你的主题 php 模板/文件中,这样:

display_customer_product_list();

This will output something like:这将输出如下内容:

<div class="custom-product">
    <ul class="custom-menu">
    <li><a href="http://www.example.com/product/slug1/">Product Title 1</a></li>
    <li><a href="http://www.example.com/product/slug2/">Product Title 2</a></li>
    <li><a href="http://www.example.com/product/slug3/">Product Title 3</a></li>
    </ul>
</div>

With that material you will be able, to achieve what you are looking at, rearranging the second function, or just using the first one in your header.php template of your active theme…使用该材料,您将能够实现您正在查看的内容,重新排列第二个功能,或者仅使用活动主题的 header.php 模板中的第一个功能......

As conditional to show hide some existing menu, you can use something like:作为显示隐藏一些现有菜单的条件,您可以使用以下内容:

if(count(get_customer_products()) > 0){
    // Displaying customer bought product items
} else {
    // Displaying normal menu items
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM