plugin development - How do I show how many products are remaining from the bought package

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 a

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.

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.

Share Improve this question asked Dec 19, 2019 at 5:23 Bhaumik BhattBhaumik Bhatt 1071 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744890657a4599391.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信