VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: niosme on September 21, 2023, 13:38:59 PM

Title: Bug on child product delete Vm 4.2.2
Post by: niosme on September 21, 2023, 13:38:59 PM
When we delete a child product it always this child product not deleted from the fields in database and always throws an error
e.g we have a product with id 1
and 4 child procuts with id's 2,3,4,5

When we go to delete the product e.g. with id 5 then everytime it throws the following error:

vmError: Empty slug product with id 5, entries exists for language? el-GR.  (or en-GB) langugage dont make any difference on that

Then we must go to edit the table from mysql by hand because
inside the
##_virtuemart_product_customfields

on the custom field params it remains like that


selectoptions=[
    {
        "voption": "clabels",
        "clabel": "\u03a7\u03c1\u03ce\u03bc\u03b1",
        "values": "\u039c\u03b1\u03cd\u03c1\u03bf\/\u0393\u03ba\u03c1\u03b9\/\u039b\u03b5\u03c5\u03ba\u03cc\r\n\u0393\u03ba\u03c1\u03b9\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u03a0\u03bf\u03c1\u03c4\u03bf\u03ba\u03b1\u03bb\u03af\r\n\u039b\u03b5\u03c5\u03ba\u03cc\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u039a\u03cc\u03ba\u03ba\u03b9\u03bd\u03bf\r\n\u039b\u03b5\u03c5\u03ba\u03cc\/\u0393\u03ba\u03c1\u03b9\/\u039c\u03c0\u03bb\u03b5"
    }
]|options={
    "1": [
        "\u039c\u03b1\u03cd\u03c1\u03bf\/\u0393\u03ba\u03c1\u03b9\/\u039b\u03b5\u03c5\u03ba\u03cc"
    ],
    "2": [
        "\u0393\u03ba\u03c1\u03b9\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u03a0\u03bf\u03c1\u03c4\u03bf\u03ba\u03b1\u03bb\u03af"
    ],
    "3": [
        "\u039b\u03b5\u03c5\u03ba\u03cc\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u039a\u03cc\u03ba\u03ba\u03b9\u03bd\u03bf"
    ],
    "4": [
        "\u039b\u03b5\u03c5\u03ba\u03cc\/\u0393\u03ba\u03c1\u03b9\/\u039c\u03c0\u03bb\u03b5"
    ],
    "5": [
        "\u039b\u03b5\u03c5\u03ba\u03cc\/\u0393\u03ba\u03c1\u03b9\/\u039c\u03c0\u03bb\u03b5"
    ]
}|


Which contains the id 5 and we should manually delete it and make it like this


selectoptions=[
    {
        "voption": "clabels",
        "clabel": "\u03a7\u03c1\u03ce\u03bc\u03b1",
        "values": "\u039c\u03b1\u03cd\u03c1\u03bf\/\u0393\u03ba\u03c1\u03b9\/\u039b\u03b5\u03c5\u03ba\u03cc\r\n\u0393\u03ba\u03c1\u03b9\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u03a0\u03bf\u03c1\u03c4\u03bf\u03ba\u03b1\u03bb\u03af\r\n\u039b\u03b5\u03c5\u03ba\u03cc\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u039a\u03cc\u03ba\u03ba\u03b9\u03bd\u03bf\r\n\u039b\u03b5\u03c5\u03ba\u03cc\/\u0393\u03ba\u03c1\u03b9\/\u039c\u03c0\u03bb\u03b5"
    }
]|options={
    "1": [
        "\u039c\u03b1\u03cd\u03c1\u03bf\/\u0393\u03ba\u03c1\u03b9\/\u039b\u03b5\u03c5\u03ba\u03cc"
    ],
    "2": [
        "\u0393\u03ba\u03c1\u03b9\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u03a0\u03bf\u03c1\u03c4\u03bf\u03ba\u03b1\u03bb\u03af"
    ],
    "3": [
        "\u039b\u03b5\u03c5\u03ba\u03cc\/\u039c\u03b1\u03cd\u03c1\u03bf\/\u039a\u03cc\u03ba\u03ba\u03b9\u03bd\u03bf"
    ],
    "4": [
        "\u039b\u03b5\u03c5\u03ba\u03cc\/\u0393\u03ba\u03c1\u03b9\/\u039c\u03c0\u03bb\u03b5"
    ]
}|


Which is unacceptable.

Also the characters are saved in translated format. They should be in utf8 or something to be readable.

Please fix it asap!
Title: Re: Bug on child product delete Vm 4.2.2
Post by: niosme on October 05, 2023, 15:44:18 PM
Any support? Also opened ticket because have passed 15 days and have no answer on bugs.
Title: Re: Bug on child product delete Vm 4.2.2
Post by: Milbo on October 05, 2023, 20:18:34 PM
Just go to the parent product, and store it again, to update this. The line of the deleted product is removed. Could be better yes.
Title: Re: Bug on child product delete Vm 4.2.2
Post by: niosme on October 10, 2023, 08:46:12 AM
Going to the parent product and storing it again doesnt work.
The registration stays forever in the database.

It could be better or it should be right and be deleted instead of going to the db and delete it by hand?
Title: Re: Bug on child product delete Vm 4.2.2
Post by: Milbo on October 16, 2023, 20:48:16 PM
No, I tested it before I wrote that. In my case it works. I remember when I wrote the whole thing, I had to write a lot stuff to store it always right. For example if a product is missing. Or if you load it.
so if you load it and the product is missing, it should not be displayed and when you store it, it overrides the old stuff, always completly!

So please test again. Maybe you used 2 tabs. you deleted on one tab, stored with the other. That cant work!
you must, if you use 2 tabs, delete it on one tab. On the tab with the parent reload the page and THEN store it. I know it is not a cooool solution. But from my point of view an acceptable workaround. This problem is quite rare in real shops. My estimation, there is a store with 5000 multivariants and they use this 1-2 times a year.

I mean, I think you know, that the whole thing is dynamic. So for example if you use stocking and one product is without stock, it wont be displayed in the dropdowns anylonger. The whole data for the dropdowns is stored and if they are created, the not available products are filtered out.

So most shops wont need it. They may just set the product on unpublished, because they may use it later anyway. Your case only happens if someone created too many options, or so.
Title: Re: Bug on child product delete Vm 4.2.2
Post by: niosme on November 09, 2023, 11:14:52 AM
Problem still exists in the latest version.
If we make a product with child products and we use the multi variant, then we delete from the main list of products,when we click the parent it throws the "vmError: Empty slug product with id ##, entries exists for language el-GR" and it stays in the database.

No multiple tabs, no parallel saving, no nothing. Just delete a child product which is assign to multi variant from the parent.Then in the parent it throws that error.
Title: Re: Bug on child product delete Vm 4.2.2
Post by: anihilator on July 01, 2024, 22:56:30 PM
I can agree that this problem still persist in version 4.2.14.
Title: Re: Bug on child product delete Vm 4.2.2
Post by: AlfredJK on September 24, 2024, 15:27:27 PM
In version 4.2.18 11050, the bug is still present. Both the parent and child record are not present in the database. Does anyone have an idea how to fix the problem?
Title: Re: Bug on child product delete Vm 4.2.2
Post by: AlfredJK on September 24, 2024, 21:19:41 PM
delete the cookies and it works again. Problem here was the recently viewed products
Title: Re: Bug on child product delete Vm 4.2.2
Post by: PRO on May 27, 2025, 00:55:27 AM
I build up lots of these deleted children over the years,

I ran the script below to delete them all. (made it with chatgpt)
IF YOU USE IT, BACKUP YOUR DATABASE BEFORE,
I ran it from the backend, and it listed everything it did.

PLEASE NOTE, in the script .     ->where($db->quoteName('virtuemart_custom_id') . ' = 149');
The 149, is the custom id I ran this on. I did not run it on every custom as I didnt have problems with them all.


// pro 2025 delete
// PRO REMOVE DELETED CHILDREN
use Joomla\CMS\Factory;

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__DIR__)); // Adjust if not in /administrator/
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$app = Factory::getApplication('administrator');
$db  = Factory::getDbo();

echo "Starting...\n";

$query = $db->getQuery(true)
    ->select('*')
    ->from($db->quoteName('#__virtuemart_product_customfields'))
    ->where($db->quoteName('virtuemart_custom_id') . ' = 149');
$db->setQuery($query);
$rows = $db->loadObjectList();

foreach ($rows as $row) {
    echo "Checking customfield ID: {$row->virtuemart_product_customfield_id}\n";
    $parts = explode('|', $row->customfield_params);

    $selectoptions = null;
    $options = null;

    foreach ($parts as $part) {
        if (str_starts_with($part, 'selectoptions=')) {
            $json = substr($part, strlen('selectoptions='));
            $selectoptions = json_decode($json, true);
        } elseif (str_starts_with($part, 'options=')) {
            $json = substr($part, strlen('options='));
            $options = json_decode($json, true);
        }
    }

    if (!is_array($options)) {
        echo " - Options not found or invalid.\n";
        continue;
    }

    $newOptions = [];
    $removedLabels = [];

    foreach ($options as $productId => $labels) {
        $checkQuery = $db->getQuery(true)
            ->select('COUNT(*)')
            ->from($db->quoteName('#__virtuemart_products'))
            ->where('virtuemart_product_id = ' . (int) $productId);
        $db->setQuery($checkQuery);
        $exists = $db->loadResult();

        if ($exists) {
            $newOptions[$productId] = $labels;
        } else {
            $removedLabels = array_merge($removedLabels, $labels);
            echo " - Removed product ID: $productId\n";
        }
    }

    if (empty($removedLabels)) {
        echo " - No cleanup needed.\n";
        continue;
    }

    // Clean "values" in selectoptions
    if (is_array($selectoptions)) {
        foreach ($selectoptions as &$entry) {
            if (isset($entry['values'])) {
                $vals = explode("\r\n", trim($entry['values']));
                $vals = array_filter($vals, fn($v) => !in_array($v, $removedLabels));
                $entry['values'] = implode("\r\n", $vals);
            }
        }
    }

    // Rebuild customfield_params
    $newParams = 'selectoptions=' . json_encode($selectoptions, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
        . '|options=' . json_encode($newOptions, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

    $updateQuery = $db->getQuery(true)
        ->update($db->quoteName('#__virtuemart_product_customfields'))
        ->set($db->quoteName('customfield_params') . ' = ' . $db->quote($newParams))
        ->where('virtuemart_customfield_id = ' . (int) $row->virtuemart_customfield_id);
    $db->setQuery($updateQuery);

    try {
        $db->execute();
        echo " - Updated successfully.\n";
    } catch (Exception $e) {
        echo " - Update failed: " . $e->getMessage() . "\n";
    }
}

echo "Done.\n";
// PRO REMOVE DELETED CHILDREN

//// PRO 2025 delete deleted child products