Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds attribute to hide course prices when zero #1788

1 change: 1 addition & 0 deletions enterprise/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class Meta:
"enable_audit_data_reporting",
"replace_sensitive_sso_username",
"hide_course_original_price",
"hide_course_price_when_zero",
"enable_portal_code_management_screen",
"enable_portal_subscription_management_screen",
"enable_learner_portal",
Expand Down
23 changes: 23 additions & 0 deletions enterprise/migrations/0198_hide_course_price_when_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.23 on 2023-12-08 05:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('enterprise', '0197_auto_20231130_2239'),
]

operations = [
migrations.AddField(
model_name='enterprisecustomer',
name='hide_course_price_when_zero',
field=models.BooleanField(default=False, help_text='Specify whether course cost should be hidden in the landing page when the final price is zero.'),
),
migrations.AddField(
model_name='historicalenterprisecustomer',
name='hide_course_price_when_zero',
field=models.BooleanField(default=False, help_text='Specify whether course cost should be hidden in the landing page when the final price is zero.'),
),
]
5 changes: 5 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ class Meta:
help_text=_("Email address that will receive learner replies to automated edX emails.")
)

hide_course_price_when_zero = models.BooleanField(
default=False,
help_text=_("Specify whether course cost should be hidden in the landing page when the final price is zero.")
)

enable_generation_of_api_credentials = models.BooleanField(
verbose_name="Allow generation of API credentials",
default=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,21 @@ <h2>{{ confirmation_text }}</h2>

<label for="radio{{ forloop.counter0 }}">
<strong class="title">{{ course_mode.title }}</strong>
<span class="price">
{{ price_text }}:
{% if course_mode.final_price and course_mode.original_price != course_mode.final_price %}
{% if hide_course_original_price %}
{{ course_mode.final_price }}
{% if not course_mode.hide_price %}
<span class="price">
{{ price_text }}:
{% if course_mode.final_price and course_mode.original_price != course_mode.final_price %}
{% if hide_course_original_price %}
{{ course_mode.final_price }}
{% else %}
<strike>{{ course_mode.original_price }}</strike> {{ course_mode.final_price }}
<div>{{discount_text|safe }}</div>
{% endif %}
{% else %}
<strike>{{ course_mode.original_price }}</strike> {{ course_mode.final_price }}
<div>{{discount_text|safe }}</div>
{{ course_mode.original_price }}
{% endif %}
{% else %}
{{ course_mode.original_price }}
{% endif %}
</span>
</span>
{% endif %}
<span class="description">{{ course_mode.description }}</span>
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1 id="modal-header-text-{{ index }}" class="modal-header-text">{{ course_title
<ul class="details-list">
{% if premium_modes|length > 0 %}
{% with course_mode=premium_modes.0 %}
{% if course_mode.original_price %}
{% if course_mode.original_price and not course_mode.hide_price %}
<li>
<div class="detail-container">
<div class="detail-title-container">
Expand Down
25 changes: 25 additions & 0 deletions enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2382,3 +2382,28 @@ def truncate_string(string, max_length=MAX_ALLOWED_TEXT_LENGTH):
was_truncated = True
return (truncated_string, was_truncated)
return (string, was_truncated)


def hide_price_when_zero(enterprise_customer, course_modes):
"""
Adds a "hide_price" flag to the course modes if price is zero and "Hide course price when zero" flag is set.

Arguments:
enterprise_customer: The EnterpriseCustomer that the enrollemnt is being done.
course_modes: iterable with dictionaries containing a required 'final_price' key
"""
if not enterprise_customer.hide_course_price_when_zero:
return course_modes

for mode in course_modes:
mode['hide_price'] = False
try:
numbers = re.findall(r'\d+', mode['final_price'])
mode['hide_price'] = int(''.join(numbers)) == 0
except ValueError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can happen only when mode['original_price'] does not contain digits (mode['final_price'] is generated in get_course_final_price).
Technically, it could also happen when the mode['min_price'] does not contain digits, but this should raise an exception in the format_price function.

Is it even possible? If yes, what does it mean? Is the course free? We can keep this exception handling; I'm just curious.

Copy link
Contributor Author

@tecoholic tecoholic Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Agrendalath To be fair, I didn't quite find an edge case while testing for this. The final final_price is a formatted string from format_price which is bound to either provide a properly formatted string, or throw an exception.

But for some reason, I wasn't comfortable with parsing string using regex with no safeguards, so added the try block.

LOGGER.warning(
'hide_price_when_zero: Could not convert price "%s" of course mode "%s" to int.',
mode['final_price'],
mode['title']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also log the final_price here to understand why this happened.

)
return course_modes
4 changes: 4 additions & 0 deletions enterprise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
get_enterprise_customer_user,
get_platform_logo_url,
get_program_type_description,
hide_price_when_zero,
is_course_run_enrollable,
localized_utcnow,
track_enrollment,
Expand Down Expand Up @@ -1542,6 +1543,9 @@ def get_enterprise_course_enrollment_page(
# Filter audit course modes.
course_modes = filter_audit_course_modes(enterprise_customer, course_modes)

# Set a flag to hide the $0 when the customer doesn't want it to be shown
course_modes = hide_price_when_zero(enterprise_customer, course_modes)

# Allows automatic assignment to a cohort upon enrollment.
cohort = request.GET.get('cohort')
# Add a message to the message display queue if the learner
Expand Down
28 changes: 28 additions & 0 deletions tests/test_enterprise/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_default_invite_key_expiration_date,
get_idiff_list,
get_platform_logo_url,
hide_price_when_zero,
is_pending_user,
localized_utcnow,
parse_lms_api_datetime,
Expand Down Expand Up @@ -536,3 +537,30 @@ def test_truncate_string(self):
(truncated_string, was_truncated) = truncate_string(test_string_2)
self.assertTrue(was_truncated)
self.assertEqual(len(truncated_string), MAX_ALLOWED_TEXT_LENGTH)

@ddt.data(True, False)
def test_hide_course_price_when_zero(self, hide_price):
customer = factories.EnterpriseCustomerFactory()
zero_modes = [
{"final_price": "$0"},
{"final_price": "$0.000"},
{"final_price": "Rs. 0.00"},
{"final_price": "0.00 EURO"},
]
non_zero_modes = [
{"final_price": "$100"},
{"final_price": "$73.50"},
{"final_price": "Rs.8000.00"},
{"final_price": "4000 Euros"},
]
customer.hide_course_price_when_zero = hide_price

processed_zero_modes = hide_price_when_zero(customer, zero_modes)
processed_non_zero_modes = hide_price_when_zero(customer, non_zero_modes)

if hide_price:
self.assertTrue(all(mode["hide_price"] for mode in processed_zero_modes))
self.assertFalse(all(mode["hide_price"] for mode in processed_non_zero_modes))
else:
self.assertEqual(zero_modes, processed_zero_modes)
self.assertEqual(non_zero_modes, processed_non_zero_modes)
1 change: 1 addition & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def setUp(self):
"system_wide_role_assignments",
"reply_to",
"hide_labor_market_data",
"hide_course_price_when_zero",
"chat_gpt_prompts",
"enable_generation_of_api_credentials",
"career_engagement_network_message",
Expand Down
Loading