Ali Raza
Ali Raza

Reputation: 13

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. Purchasing the item through WooCommerce plugin.

e.g. 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.

Upvotes: 1

Views: 270

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253921

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.

Below you will find 2 functions.
1) first one will get all bought product IDs for the current customer (with an optional argument, the $user_id).
2) Second one will display a menu (or a list) for that products with titles and links…

Here is this code (goes in function.php file of your active child theme ore theme):

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:

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…

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
}

Upvotes: 1

Related Questions