0

I am new to this. What I want to do is create a package that gives the customer liberty to buy 10 of any of my product at anytime. Now when a user buys this package I want my site to show him that there are 10 products he can buy and when he buys any of the product the number of the products remaining will be deducted by the amount of the product he buys. for E.g If he has 10 products remaining and he buys two then I want to show him that there are still 8 products remaining. Now how do I distinguish normal customer from the one that has already bought the package and how do I not show the payment page to the one that has already bought the package. Here I want to sell both the packages and singles products to customer. I have a little idea about wordpress so is there any plugin for this and if not then where do I code this. I am using twentyseventeen theme. Sorry for the inconvenience if this is not the right question for this site.

0

May this code will helpful for you. Ihave use a code to get the current logged in users completed (or you may use your status) order data. Then get their product count, now we can obtain the product which will purchased before. Just gone through this code.

$customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
        //use status such as "wc-processing"/"wc-on-hold"etc.
    ) );
    $previous_count = 0;

    // Going through each current customer orders
    foreach ( $customer_orders as $customer_order ) {
        $order    = wc_get_order( $customer_order );
        $items    = $order->get_items();


        // Going through each current customer products bought in the order
        foreach ( $items as $item ) {
            $id = $item['product_id'];

            // If product not in array, add it
            if ( ! array_key_exists( $item['product_id'], $products ) ) {
                $products[ $id ] = array(
                    'name' => $item['name'],
                    'count' => 0,
                );
            }
            // Increment Product <code>count</code> from cart quantity
            //$products[ $id ]['count'] += $item->get_quantity();// get the total product
            $previous_count += $item->get_quantity();
        }
    }
    $cart_count = WC()->cart->get_cart_contents_count();
    $remaining_product_count = $previous_count+$cart_count;
    if($remaining_product_count < 10){
        $remaining_product_count = 10-$remaining_product_count;
        echo $remaining_product_count;
    }
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.