Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. <?php
  2. /**
  3. * Client wants only logged in users to see prices and buy non-sale items while allowing all users to see prices and buy
  4. * on sale items
  5. */
  6.  
  7. /**
  8. *
  9. * filters woocommerce_is_purchasable for not onsale items
  10. *
  11. * @param $new_cart
  12. *
  13. * @return mixed
  14. */
  15. function prefix_hack_for_on_sale_only( $purchasable, $product ) {
  16. if ( ! is_admin() && ! is_user_logged_in() && ! $product->is_on_sale() ) {
  17. return false;
  18. }
  19. return $purchasable;
  20. }
  21. add_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1, 2 );
  22.  
  23. /**
  24. * Output account link for non-sale products when user is not logged in
  25. *
  26. * @return bool|string
  27. */
  28. function prefix_login_for_prices() {
  29.  
  30. if ( ! class_exists('WooCommerce') )
  31. return false;
  32.  
  33. global $product;
  34.  
  35. $login = false;
  36.  
  37. if ( ! is_user_logged_in() && ! $product->is_purchasable() ) {
  38.  
  39. remove_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1 );
  40.  
  41. $login_url = wc_get_account_endpoint_url( 'dashboard' );
  42.  
  43. if ( is_singular( 'product' ) ) {
  44. $login_url = add_query_arg( 'redirect_to', urlencode( get_permalink() ), $login_url );
  45. }
  46.  
  47. if ( $product->is_purchasable() ) {
  48. $login = sprintf(
  49. '<a href="%1$s">%2$s</a>',
  50. esc_url( $login_url ),
  51. esc_html__( 'Sign in to view price', 'text-domain' )
  52. );
  53. }
  54.  
  55. add_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1, 2 );
  56.  
  57. }
  58.  
  59. return $login;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement