News:

Looking for documentation? Take a look on our wiki

Main Menu

Copy part of description to short description

Started by joozen, December 26, 2023, 22:07:32 PM

Previous topic - Next topic

joozen

Hello I need the hack to copy part of description to short description. I think function probably could look like this:
function copyDescription(&$product) {
    if (isset($product['product_desc'])) {
        // Copy up to 300 characters of 'product_desc' to 'product_s_desc'
        $product['product_s_desc'] = substr($product['product_desc'], 0, 300);
    }
}

But I can't find right place where to insert this function
Can you help me please?
Joomla! 4.3.4
VirtueMart 4.2.4 10922
PHP: 8.1.26

hazael

What do you want to achieve with this? Why create additional features?
Wouldn't it be easier for you to just display a short description of 300 words for the product_desc variable?

joozen

Quote from: hazael on December 27, 2023, 09:49:47 AM
What do you want to achieve with this? Why create additional features?
Wouldn't it be easier for you to just display a short description of 300 words for the product_desc variable?
Problem is I never inserted short description, now I have 3000 products and its too late now. That's why I looking to way to copy it automatically somehow as meta description generated from short description that why I want 300 characters.
Joomla! 4.3.4
VirtueMart 4.2.4 10922
PHP: 8.1.26

joozen

#3
Created Virtuemart plugin, successfully installed to plugins/vmcustom but not working, something missed.

<?php
defined
('_JEXEC') or die;

use 
Joomla\CMS\Plugin\CMSPlugin;
use 
Joomla\CMS\Factory;
use 
Joomla\CMS\Log\Log// Import Log class for error logging

class PlgVmCopyProductDesc extends vmCustomPlugin {

    function 
__construct(&$subject$config)
    {
        
parent::__construct ($subject$config);
        
$this->app Factory::getApplication();
    }

    
// Verify the correct event trigger for VirtueMart 4
    
public function plgOnVmProductUpdate($context, &$product)
    {
        try {
            
$this->plgOnVmCopyDesc($product);
        } catch (
\Exception $e) {
            
Log::add($e->getMessage(), Log::ERROR'vmcopyproductdesc'); // Log errors for troubleshooting
            // Optionally display a user-friendly error message
        
}
    }

private function 
plgOnVmCopyDesc(&$product)
{
    if (isset(
$product->virtuemart_product_id) && !empty($product->product_desc)) {
        
// Get a database object
        
$db Factory::getDbo();

        
// Define the maximum length of the short description
        
$maxLength = (int) $this->params->get('max_length'300);

        
// Prepare the short description
        
$shortDesc $db->quote(substr(trim($product->product_desc), 0$maxLength));

        
// Construct the query to update the short description
        
$query $db->getQuery(true)
            ->
update($db->quoteName('#__virtuemart_products_en_gb'))
            ->
set($db->quoteName('product_s_desc') . " = " $shortDesc)
            ->
where($db->quoteName('virtuemart_product_id') . " = " . (int) $product->virtuemart_product_id);

        
// Set and execute the query
        
$db->setQuery($query);

        try {
            
$result $db->execute();
            if (
$result && $this->app->isClient('site')) {
                
$this->app->enqueueMessage('Product description copied successfully');
            }
        } catch (
\RuntimeException $e) {
            
Log::add('Database error in PlgVmCopyProductDesc: ' $e->getMessage(), Log::ERROR'vmcopyproductdesc');
            if (
$this->app->isClient('site')) {
                
$this->app->enqueueMessage('Failed to copy description');
            }
        }
    }
}
}


I think problem is I can't find right class for product update. I searched reference and there is no any relevant as I see.
Joomla! 4.3.4
VirtueMart 4.2.4 10922
PHP: 8.1.26

Roderic

I would then just export all products with description with RO CSVI, then reimport the description as short description, but limit characters to 300 and perhaps a few other manipulations with "Rules".

hazael

#5
Quote from: Roderic on December 28, 2023, 18:10:45 PM
I would then just export all products with description with RO CSVI

Exactly, or if you don't have a similar component for importing and exporting data, then log in to the phpmyadmin database and export the table #__virtuemart_products_en_gb (or in another language version) in CSV format, then in a spreadsheet (libre office) copy the product_desc column to the product_s_desc column.
If you are familiar with functions in Excel, you can easily shorten the descriptions to 300 characters or to full sentences, while cleaning up unnecessary HTML code.
If you do this, import the modified csv file into your database. Before importing, make a backup copy of this table (for example with a changed name)

Doing special features can unnecessarily impact your store's performance - the less of this junk, the better.