admin管理员组文章数量:1405335
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 badges1 Answer
Reset to default 0May 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;
}
本文标签: plugin developmentHow do I show how many products are remaining from the bought package
版权声明:本文标题:plugin development - How do I show how many products are remaining from the bought package 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744890656a2630759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论