Skip to content

Commit

Permalink
add discount option product page and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cquesadad committed Apr 15, 2024
1 parent db867f7 commit 6004bd4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
14 changes: 1 addition & 13 deletions custom-price-user-role.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function require_members_plugin() {
function require_members_plugin_notice() {
?>
<div class="notice notice-error">
<p>The plugin <b>Members</b> is required to make <b>Custom Role Based Pricing</b> work correctly. Please, activate Member Plugin.</p>
<p>The plugin <b>Members</b> is required to make <b>Custom Role Based Pricing</b> work correctly. Please, install and activate Members Plugin.</p>
</div>
<?php
}
Expand Down Expand Up @@ -69,18 +69,6 @@ function crbp_save_custom_price_fields($product_id) {
}
add_action('woocommerce_process_product_meta', 'crbp_save_custom_price_fields');

// Update cart page price
function crbp_update_cart_item_price($cart_object) {
foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id'];
$custom_price = crbp_get_custom_price_by_role($product_id);
if (!empty($custom_price)) {
$cart_item['data']->set_price($custom_price);
}
}
}
add_action('woocommerce_before_calculate_totals', 'crbp_update_cart_item_price');

//Add settings link to admin page
function cpur_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=cpur-plugin-settings">' . __( 'Settings' ) . '</a>';
Expand Down
48 changes: 31 additions & 17 deletions includes/plugin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ function cpur_render_plugin_settings_page() {
<?php
}

// Función para registrar y agregar campos de ajustes
// Function to register and validate adjustments
function cpur_register_settings() {
register_setting(
'cpur_plugin_settings',
'cpur_show_hide_prices',
'intval' // Callback de validación para asegurar que se recibe un valor entero
'intval' // Validation Callback to ensure it receives an integer
);
register_setting(
'cpur_plugin_settings',
'cpur_show_discount_prices',
'intval' // Validation Callback to ensure it receives an integer
);
add_settings_section(
'cpur_plugin_main_section',
Expand All @@ -50,43 +55,52 @@ function cpur_register_settings() {
'cpur_plugin_settings',
'cpur_plugin_main_section'
);
add_settings_field(
'cpur_show_discount_prices_field',
'Show custom prices as discounts',
'cpur_show_discount_prices_field_cb',
'cpur_plugin_settings',
'cpur_plugin_main_section'
);
}
add_action('admin_init', 'cpur_register_settings');

// Función de callback para la sección principal de ajustes
// Callback function for the main settings section
function cpur_plugin_main_section_cb() {
echo 'Selecciona si deseas mostrar u ocultar los precios por rol:';
}

// Función de callback para el campo de ajustes de mostrar/ocultar precios
// Callback function for show/hide prices settings field
function cpur_show_hide_prices_field_cb() {
$show_hide_prices = get_option('cpur_show_hide_prices', 1); // Set default value to 1 (active)
?>

<label><input type="radio" name="cpur_show_hide_prices" value="1" <?php checked($show_hide_prices, 1); ?>>Show Price by User Role</label><br>
<label><input type="radio" name="cpur_show_hide_prices" value="0" <?php checked($show_hide_prices, 0); ?>>Hide Price by User Role</label><br>

<?php
}

// // Add settings link on plugin page
// function cpur_settings_link($links) {
// $settings_link = '<a href="options-general.php?page=cpur-plugin-settings">Settings</a>';
// array_unshift($links, $settings_link);
// return $links;
// }
// $plugin = plugin_basename(__FILE__);
// add_filter("plugin_action_links_$plugin", 'cpur_settings_link');
// Callback function for show/hide prices settings field
function cpur_show_discount_prices_field_cb() {
$show_discount_prices = get_option('cpur_show_discount_prices', 0); // Set default value to 0 (not active)
?>

<label><input type="checkbox" name="cpur_show_discount_prices" value="1" <?php checked($show_discount_prices, 1); ?>>Show custom prices as discounts</label><br>

<?php
}

// Add settings link on plugin page
function cpur_settings_link($links) {
// Enlace de configuración
// Config links
$settings_link = '<a href="options-general.php?page=cpur-plugin-settings">Settings</a>';
// Agregar el enlace de configuración al inicio de la lista de enlaces
// Add the configuration link to the top of the link list
array_unshift($links, $settings_link);
return $links;
}
// Obtener el nombre del plugin actual

// Get the name of the current plugin
$plugin = plugin_basename(__FILE__);
// Agregar el filtro para mostrar el enlace de configuración en la página de plugins
// Add filter to show settings link on plugins page
add_filter("plugin_action_links_$plugin", 'cpur_settings_link');
60 changes: 53 additions & 7 deletions public/custom-price-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ function crbp_get_custom_price_by_role($product_id) {
$roles = $user->roles;
$custom_price = '';

// Verificar si se debe mostrar el precio personalizado
// Verify if it needs to show custom price
$show_custom_price = get_option('cpur_show_hide_prices', 1); // Valor predeterminado a 1 (activo)

// Si la opción de mostrar precios está desactivada, retornar precio vacío
if (!$show_custom_price) {
return $custom_price;
}

// Verificar cada rol y obtener el precio personalizado
// Verify each role and show the price
foreach ($roles as $role) {
$role_custom_price = get_post_meta($product_id, 'custom_price_' . $role, true);
if (!empty($role_custom_price)) {
Expand All @@ -35,25 +35,71 @@ function crbp_display_custom_price($price, $product) {

// Verify if exist custom price
if (!empty($custom_price)) {
// Format custom price
$formatted_price = wc_price($custom_price);
// Replace regular price with custom price
$price = '<span class="custom-price">' . $formatted_price . '</span>';
// Get regular price
$regular_price = $product->get_regular_price();
// Format regular and custom prices
$regular_price_formatted = wc_price($regular_price);
$custom_price_formatted = wc_price($custom_price);

// Check if discount prices should be shown
$show_discount_prices = get_option('cpur_show_discount_prices', 0);

// If discount prices should be shown, display regular price struck through and custom price
if ($show_discount_prices) {
$price = '<del>' . $regular_price_formatted . '</del> <span class="custom-price">' . $custom_price_formatted . '</span>';
} else {
// If not, just display custom price
$price = '<span class="custom-price">' . $custom_price_formatted . '</span>';
}
}

return $price;
}
add_filter('woocommerce_get_price_html', 'crbp_display_custom_price', 10, 2);

// Update cart page price
function crbp_update_cart_item_price($cart_object) {
foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id'];
$custom_price = crbp_get_custom_price_by_role($product_id);
if (!empty($custom_price)) {
$cart_item['data']->set_price($custom_price);
}
}
}
add_action('woocommerce_before_calculate_totals', 'crbp_update_cart_item_price');

// Show custom price in mini cart
function crbp_elementor_pro_cart_item_price($price, $cart_item, $cart_item_key) {
// Get Product ID
$product_id = $cart_item['product_id'];

// Get product object
$product = wc_get_product($product_id);

// Get regular price
$regular_price = $product->get_regular_price();
// Get custom price
$custom_price = crbp_get_custom_price_by_role($product_id);

// Format regular and custom prices
$regular_price_formatted = wc_price($regular_price);
$custom_price_formatted = wc_price($custom_price);

// Verify if there is custom price
if (!empty($custom_price)) {
$price = wc_price($custom_price);
//$price = wc_price($custom_price);

// Check if discount prices should be shown
$show_discount_prices = get_option('cpur_show_discount_prices', 0);

// If discount prices should be shown, display regular price struck through and custom price
if ($show_discount_prices) {
$price = '<del>' . $regular_price_formatted . '</del> <span class="custom-price">' . $custom_price_formatted . '</span>';
} else {
// If not, just display custom price
$price = '<span class="custom-price">' . $custom_price_formatted . '</span>';
}
}

return $price;
Expand Down

0 comments on commit 6004bd4

Please sign in to comment.