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: add discount indication to payment method #3

Open
sammyskills opened this issue Aug 28, 2023 · 23 comments
Open

feat: add discount indication to payment method #3

sammyskills opened this issue Aug 28, 2023 · 23 comments
Assignees
Labels
enhancement New feature or request

Comments

@sammyskills
Copy link
Owner

image

@sammyskills sammyskills added the enhancement New feature or request label Aug 28, 2023
@sammyskills sammyskills self-assigned this Aug 28, 2023
@katalin2k
Copy link

katalin2k commented Oct 15, 2023

I made some modifications in catalog/model/extension/total/payment_discount.php
Works but has a little issue: on mobile devices it hows the discount twice in the payment method title.
Also I added sprintf to $desc_text so in backend settings you just need to add text like this: "%s discount on payment method"(this should be added as placeholder or tooltip for future users.

I can't seem to be able to fix the duplication issue on mobile devices.

class ModelExtensionTotalPaymentDiscount extends Controller
{
    public function getTotal($total)
    {
        if ($this->config->get('total_payment_discount_status') && $this->cart->getSubTotal()) {
            $discount_payment_method = $this->config->get('total_payment_discount_payment_type');
            $discount = $this->config->get('total_payment_discount_percentage');
	    $desc_text = sprintf($this->config->get('total_payment_discount_description'), $discount.'%');
            $sort_order = $this->config->get('total_payment_discount_sort_order');

            // Loop through available payment methods
            foreach ($this->session->data['payment_methods'] as &$payment_method) {
                // Check if the current payment method matches
                if ($discount_payment_method == $payment_method['code']) {
                    // Update payment method title with discount percentage
                    $payment_method['title'] .= ' (-' . $discount . '%)';

                    // If payment method is selected, add the discount to totals
                    if ($this->session->data['payment_method']['code'] == $discount_payment_method) {
                        $total['totals'][] = array(
                            'code' => 'payment_discount',
                            'title' => $desc_text,
                            'value' => '-' . (($discount / 100) * $total['total']),
                            'sort_order' => $sort_order,
                        );

                        $total['total'] -= (($discount / 100) * $total['total']);
                    }
                }
            }
        }
    }
}

@sammyskills
Copy link
Owner Author

Nice work @katalin2k.

At a glance, I can guess that the issue is coming from the loop: the discount is appended to the payment method title more than once here: $payment_method['title'] .= ' (-' . $discount . '%)'.

I'll see how I can fix the issue and push an update as soon as I can.

Thank you.

@sammyskills
Copy link
Owner Author

Which version of opencart are you currently using?

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

Please try this code instead:

class ModelExtensionTotalPaymentDiscount extends Controller
{
    public function getTotal($total)
    {
        if ($this->config->get('total_payment_discount_status') && $this->cart->getSubTotal()) {
            $discount_payment_method = $this->config->get('total_payment_discount_payment_type');
            $discount = $this->config->get('total_payment_discount_percentage');
	    $desc_text = sprintf($this->config->get('total_payment_discount_description'), $discount.'%');
            $sort_order = $this->config->get('total_payment_discount_sort_order');

            // Update the title of the discounted payment method
            $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)';
            // If payment method is selected, add the discount to totals
            if ($this->session->data['payment_method']['code'] == $discount_payment_method) {
                    $total['totals'][] = array(
                        'code' => 'payment_discount',
                        'title' => $desc_text,
                        'value' => '-' . (($discount / 100) * $total['total']),
                        'sort_order' => $sort_order,
                    );

                    $total['total'] -= (($discount / 100) * $total['total']);
            }
        }
    }
}

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

Can't find the screenshot you are talking about. But update the code as follows:

// Update the title of the discounted payment method
if (isset($this->session->data['payment_methods'])) {
    $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)';
}

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

Screenshot?

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

Your screenshots do not display when you send via email.

Try replying directly on github.

@katalin2k
Copy link

IMG_1227
/

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

I don't think this is a full fix.

This will not work for a new user. You can try by clearing your browser session and cookies, then try to checkout.

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

On initial visit, a user will be presented with the error:

image

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

@sammyskills
Copy link
Owner Author

Yes, this fix is not applicable to all themes. It doesn't work on the default theme either.

@katalin2k
Copy link

katalin2k commented Oct 15, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants