Post by Majnoon » Thu Aug 17, 2023 6:07 pm

Hello fellas,

I am using opencart 3.0.3.8 with default theme.

I'm working on a modifying my opencart to deals with recurring pricing, and I've run into a roadblock that I hope someone here might be able to assist me with.
Recurring Prices: I have recurring products with various frequencies (e.g., day, week, month) and corresponding prices.
Displaying Options: I need to display these recurring options along with their prices as radio buttons on the product page.
Problem: The current code builds a recurring price string for each product, which is making it challenging to separate the prices for different recurring frequencies. I need to display individual prices , but the existing logic in controller doesn't align with this requirement.
Here's a snippet of the relevant code:
i did few changes to test so i am leaving them in code but as comment out.

Code: Select all

				$recurring = '';
				//$recurring_price = 0; // Initialize a variable to hold the recurring price

				if ($product['recurring']) {
					//$this->log->write('Recurring value: ' . print_r($product['recurring'], true));
					$frequencies = array(
						'day'        => $this->language->get('text_day'),
						'week'       => $this->language->get('text_week'),
						'semi_month' => $this->language->get('text_semi_month'),
						'month'      => $this->language->get('text_month'),
						'year'       => $this->language->get('text_year')
					);

					if ($product['recurring']['trial']) {
						$recurring = sprintf($this->language->get('text_trial_description'), $this->currency->format($this->tax->calculate($product['recurring']['trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['trial_cycle'], $frequencies[$product['recurring']['trial_frequency']], $product['recurring']['trial_duration']) . ' ';
					}

					if ($product['recurring']['duration']) {
						$recurring_price = $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']); // Extracting the recurring price
						//$recurring .= sprintf($this->language->get('text_payment_description'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
					} else {
						$recurring .= sprintf($this->language->get('text_payment_cancel'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
					}
				}

				//$data['recurring_price'] = $recurring_price;
				//$this->log->write('Recurring Price: ' . $recurring_price);
And here's the template code for displaying the radio buttons:

Code: Select all

            {% if recurrings %}
<hr>
{#<h3>{{ text_payment_recurring }}</h3>#}
<div class="form-group required">
    <label>
        <input type="radio" name="recurring_id" value="0" checked="checked" />
        One-time purchase: {{ price }}
    </label>
    <br>
    {% for recurring in recurrings %}
    <label>
        <input type="radio" name="recurring_id" value="{{ recurring.recurring_id }}" />
        {{ recurring.name }}: {{ recurring.price }}
    </label>
    <br>
    {% endfor %}
    <div class="help-block" id="recurring-description"></div>
</div>
{% endif %}
P.s attached are references images.
Thank you in advance for your help!

Attachments

sofar_achive_results.png

needed help from this stage - sofar_achive_results.png (69.93 KiB) Viewed 559 times

ideal-output.png

ideal-output - ideal-output.png (170.32 KiB) Viewed 559 times


Active Member

Posts

Joined
Fri Feb 05, 2021 8:29 pm

Post by SohBH » Thu Aug 17, 2023 8:12 pm

controller

Code: Select all

$data['recurring_options'] = array();

if ($product['recurring']) {
    $frequencies = array(
        'day'        => $this->language->get('text_day'),
        'week'       => $this->language->get('text_week'),
        'semi_month' => $this->language->get('text_semi_month'),
        'month'      => $this->language->get('text_month'),
        'year'       => $this->language->get('text_year')
    );

    foreach ($product['recurring']['recurring'] as $recurring) {
        $price = $this->currency->format($this->tax->calculate($recurring['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        
        $data['recurring_options'][] = array(
            'frequency' => $frequencies[$recurring['frequency']],
            'cycle' => $recurring['cycle'],
            'duration' => $recurring['duration'],
            'price' => $price
        );
    }
}
product.twig

Code: Select all

{% if recurring_options %}
<div class="form-group required">
    <label class="control-label">{{ text_payment_recurring }}</label>
    <div class="radio">
        {% for option in recurring_options %}
        <label>
            <input type="radio" name="recurring_option" value="{{ option.frequency }}" />
            {{ option.frequency }} {{ option.cycle }} {{ option.duration }} - {{ option.price }}
        </label>
        {% endfor %}
    </div>
</div>
{% endif %}

View all extensions | Request custom work | Pricing | Contact Me


User avatar
Active Member

Posts

Joined
Mon Nov 02, 2020 12:01 am
Location - Malaysia

Post by Majnoon » Thu Aug 17, 2023 8:57 pm

SohBH wrote:
Thu Aug 17, 2023 8:12 pm
controller

Code: Select all

$data['recurring_options'] = array();

if ($product['recurring']) {
    $frequencies = array(
        'day'        => $this->language->get('text_day'),
        'week'       => $this->language->get('text_week'),
        'semi_month' => $this->language->get('text_semi_month'),
        'month'      => $this->language->get('text_month'),
        'year'       => $this->language->get('text_year')
    );

    foreach ($product['recurring']['recurring'] as $recurring) {
        $price = $this->currency->format($this->tax->calculate($recurring['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        
        $data['recurring_options'][] = array(
            'frequency' => $frequencies[$recurring['frequency']],
            'cycle' => $recurring['cycle'],
            'duration' => $recurring['duration'],
            'price' => $price
        );
    }
}
product.twig

Code: Select all

{% if recurring_options %}
<div class="form-group required">
    <label class="control-label">{{ text_payment_recurring }}</label>
    <div class="radio">
        {% for option in recurring_options %}
        <label>
            <input type="radio" name="recurring_option" value="{{ option.frequency }}" />
            {{ option.frequency }} {{ option.cycle }} {{ option.duration }} - {{ option.price }}
        </label>
        {% endfor %}
    </div>
</div>
{% endif %}
Thanks for your reply but with this approach you are totally diverted from the original opencart code and also converting the original string into Array.


i had this approach in my mind too
Controller

Code: Select all

$recurrings_data = [];

foreach ($products as $product) {
    if ($product['recurring']) {
        // ... other code as before ...

        $recurrings_data[] = [
            'name' => "Test recurring charges:", // Modify as needed
            'price' => $recurring_price,
            'description' => $recurring
        ];
    }
}

$data['recurrings_data'] = $recurrings_data;
twig

Code: Select all

{% if recurrings_data %}
<hr>
<div class="form-group required">
    <label>
        <input type="radio" name="recurring_id" value="0" checked="checked" />
        One-time purchase: {{ price }}
    </label>
    <br>
    {% for recurring_data in recurrings_data %}
    <label>
        <input type="radio" name="recurring_id" value="{{ recurring_data.recurring_id }}" />
        {{ recurring_data.name }}: {{ recurring_data.price }}
    </label>
    <br>
    {% endfor %}
    <div class="help-block" id="recurring-description"></div>
</div>
{% endif %}

Active Member

Posts

Joined
Fri Feb 05, 2021 8:29 pm

Post by Majnoon » Fri Aug 18, 2023 7:49 pm

I have tried your proposed solution as well but then the issue is that
i start getting error like when visiting the cart view page notices can be solve with

Code: Select all

if (isset($product['recurring']) && $product['recurring']) {
and also notice that Recurring options: Array doesn't hold any value at all and the twig is not rendering anything at all.

2023-08-18 11:36:06 - PHP Notice: Undefined index: recurring in /home/xyz/public_html/catalog/controller/checkout/cart.php on line 123
2023-08-18 11:36:06 - PHP Warning: Invalid argument supplied for foreach() in /home/xyz/public_html/catalog/controller/checkout/cart.php on line 123
2023-08-18 11:36:06 - Recurring options: Array
(
)

Active Member

Posts

Joined
Fri Feb 05, 2021 8:29 pm
Who is online

Users browsing this forum: No registered users and 10 guests