Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created June 27, 2017 15:03
Show Gist options
  • Save djrmom/03fa5f7d73fe8f1336a9ff9aef7dca96 to your computer and use it in GitHub Desktop.
Save djrmom/03fa5f7d73fe8f1336a9ff9aef7dca96 to your computer and use it in GitHub Desktop.
WooCommerce
<?php
/**
* Client wants only logged in users to see prices and buy non-sale items while allowing all users to see prices and buy
* on sale items
*/
/**
*
* filters woocommerce_is_purchasable for not onsale items
*
* @param $new_cart
*
* @return mixed
*/
function prefix_hack_for_on_sale_only( $purchasable, $product ) {
if ( ! is_admin() && ! is_user_logged_in() && ! $product->is_on_sale() ) {
return false;
}
return $purchasable;
}
add_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1, 2 );
/**
* Output account link for non-sale products when user is not logged in
*
* @return bool|string
*/
function prefix_login_for_prices() {
if ( ! class_exists('WooCommerce') )
return false;
global $product;
$login = false;
if ( ! is_user_logged_in() && ! $product->is_purchasable() ) {
remove_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1 );
$login_url = wc_get_account_endpoint_url( 'dashboard' );
if ( is_singular( 'product' ) ) {
$login_url = add_query_arg( 'redirect_to', urlencode( get_permalink() ), $login_url );
}
if ( $product->is_purchasable() ) {
$login = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $login_url ),
esc_html__( 'Sign in to view price', 'text-domain' )
);
}
add_filter('woocommerce_is_purchasable', 'prefix_hack_for_on_sale_only', 1, 2 );
}
return $login;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment