Guest User

Untitled

a guest
Jan 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php//IDENTIFICA SE JA COMPROU O PRODUTO E MUDA O BOTAO (COLOCA ISSO NO SEU FUNCTION)
  2. function has_bought_items($meuCursoID) {
  3. $bought = false;
  4.  
  5. // Set HERE ine the array your specific target product IDs
  6. $prod_arr = array( $meuCursoID );
  7.  
  8. // Get all customer orders
  9. $customer_orders = get_posts( array(
  10. 'numberposts' => -1,
  11. 'meta_key' => '_customer_user',
  12. 'meta_value' => get_current_user_id(),
  13. 'post_type' => 'shop_order', // WC orders post type
  14. 'post_status' => 'wc-completed' // Only orders with status "completed"
  15. ) );
  16. foreach ( $customer_orders as $customer_order ) {
  17. // Updated compatibility with WooCommerce 3+
  18. $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
  19. $order = wc_get_order( $customer_order );
  20. //print_r($order);
  21. // Iterating through each current customer products bought in the order
  22. foreach ($order->get_items() as $item) {
  23. // WC 3+ compatibility
  24. if ( version_compare( WC_VERSION, '3.0', '<' ) )
  25. $product_id = $item['product_id'];
  26. else
  27. $product_id = $item->get_product_id();
  28.  
  29. // Your condition related to your 2 specific products Ids
  30. if ( in_array( $product_id, $prod_arr ) )
  31. $bought = true;
  32. }
  33. }
  34. // return "true" if one the specifics products have been bought before by customer
  35. return $bought;
  36. }
  37. ?>
  38.  
  39. <?php //COLOCA ISSO NO SEU LOOP OU NO SINGLE. OBS: VOCE PODE USAR NORMALMENTE OS CAMPOS PERSONALIZADOS ?>
  40. <?php if(has_bought_items($product->get_id())): ?>
  41. <span>Produto Adquirido</span>
  42. <?php else : ?>
  43. <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt">comprar</button>
  44. <?php endif; ?>
Add Comment
Please, Sign In to add comment