Ecommerce Shopify WordPress Discussion

Woocommerce: Prevent user to buy same tag product if they have already purchased in past [duplicate]

This question already has answers here: Allow specific products to be only purchased once by user in Woocommerce (2 answers) Make a product to be purchased only once or not if any item from a specific product category has been purchased in Woocommerce (2 answers) Targeting product terms from a custom taxonomy in WooCommerce (1 answer) Closed 21 hours ago. in woocommerce i want to Prevent user to buy same tag(Tag: abc) product if they have already purchased this tag product in past (Tag: ABC) So for i.e. if user has purchased One product belongs to Product tag (ABC) then if they come back to site again and want to buy and product that is belong to tag (ABC) then i want to disable product purchase for that user. I am debugging in woocommerce.
You can use the hook "woocommerce_before_checkout_process" in the functions.php file of your theme. // Hook into the WooCommerce checkout process add_action('woocommerce_before_checkout_process', 'check_user_purchase_history'); function check_user_purchase_history() { // Get current user ID $user_id = get_current_user_id(); // Get the tags of the product being purchased $product_id = get_the_ID(); $product_tags = wp_get_post_terms($product_id, 'product_tag', array('fields' => 'names')); // Query user's order history $user_orders = wc_get_orders(array( 'customer_id' => $user_id, 'status' => 'completed', )); // Check if the user has already purchased a product with the same tag foreach ($user_orders as $order) { foreach ($order->get_items() as $item) { $item_product_id = $item->get_product_id(); $item_tags = wp_get_post_terms($item_product_id, 'product_tag', array('fields' => 'names')); // Check if there is a tag match if (array_intersect($product_tags, $item_tags)) { // Prevent the purchase wc_add_notice(__('You have already purchased a product with the same tag. Please review your cart.'), 'error'); // Optional: Redirect to cart page wp_redirect(wc_get_cart_url()); exit; } } } }

January 20, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.