How would you do this?

Phoca Cart - complex e-commerce extension
User avatar
Nidzo
Phoca Professional
Phoca Professional
Posts: 386
Joined: 07 Nov 2018, 14:55

How would you do this?

Post by Nidzo »

Hi guys! I need advice. I want to create discount for all registered customers that are older than 60 year.

I created customers group 60+ and created discount for that group.

My main problem is the way how to assign customers to that group.

I think about creating custom field in Joomla registration form with date of birth.

If user registers and make order before I manually assigned him to that customer group, discount will not be included.

Is it possible to automatically assign user to some Phoca Cart customer group based on Joomla custom field?

How would you manage this scenario?
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: How would you do this?

Post by Jan »

Hi,

we have plg_user_phocacart.zip - user plugin for Phoca Cart where we have a method:
onUserAfterSave which assign users to group.

Such plugin can be copied and the method can be created by your needs. E.g. to somehow get the information about the birthday of user and if it is e.g. 60+ then assign this user to specific group.

Just see the main Phoca Cart package for this plugin.

Jan
If you find Phoca extensions useful, please support the project
User avatar
Nidzo
Phoca Professional
Phoca Professional
Posts: 386
Joined: 07 Nov 2018, 14:55

Re: How would you do this?

Post by Nidzo »

I don't have PHP skills but this AI result make sense but it does not work :D

Code: Select all

/**
     * Assigns user to Phoca Cart customer groups based on their custom date of birth field.
     * Users older than 60 years should be assigned to a specific group.
     *
     * @param   int  $userId
     *
     * @since 4.1.0
     */
    private function assignRegistrationGroups(int $userId): void
    {
        if (!$this->loadPhocaCart()) {
            return;
        }

        $db = Factory::getDBO();

        // Fetch the user's date of birth from custom fields table
        $query = $db->getQuery(true)
            ->select($db->qn('value'))
            ->from($db->qn('#__fields_values')) // Custom fields are stored in #__fields_values table
            ->where($db->qn('item_id') . ' = ' . (int)$userId) // item_id corresponds to the user ID
            ->where($db->qn('field_id') . ' = ' . $db->quote($this->params->get('dob_custom_field_id'))); // Fetching value by custom field ID

        $db->setQuery($query);

        $dob = $db->loadResult();

        // Check if DOB exists
        if (!$dob) {
            return;
        }

        // Calculate age based on the date of birth
        $dobDate = new DateTime($dob);
        $currentDate = new DateTime();
        $age = $currentDate->diff($dobDate)->y;

        // Senior group ID - configured in the plugin parameters
        $groupIdForSenior = $this->params->get('senior_group_id', 2); // Default group ID 2

        // Check if the user is older than 60
        if ($age > 60) {
            // Fetch other customer groups
            $query = $db->getQuery(true)
                ->select($db->qn('id'))
                ->from($db->qn('#__phocacart_groups'))
                ->where($db->qn('activate_registration') . ' = 1 OR ' . $db->qn('id') . ' = 1');

            $db->setQuery($query);
            $groups = $db->loadColumn();

            if ($groups) {
                // Add the senior group to the list of groups
                $groups[] = $groupIdForSenior;

                // Store the groups for the user
                if (PhocacartGroup::storeGroupsById((int)$userId, 1, $groups)) {
                    // Successful assignment
                } else {
                    // Failed assignment
                }
            }
        }
    }
and at the end

Code: Select all

 // Assign user to customer groups based on settings
        if ($isnew) {
            $this->assignRegistrationGroups($user['id']);
        } else {
            // Call assignRegistrationGroups to check for existing users as well
            $this->assignRegistrationGroups($user['id']);
Entire file https://github.com/nidzo80/phoca/blob/m ... cacart.php
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: How would you do this?

Post by Jan »

Hi, unfortunately, I didn't do such change yet and unforutunately, I don't think, AI can do this. Programming is something where you go step by step to the outcome, so it is hard to say how the part of program can look before really doint it :idea:

Jan
If you find Phoca extensions useful, please support the project
Post Reply