VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: joozen on February 26, 2024, 22:15:14 PM

Title: Characters count for meta description
Post by: joozen on February 26, 2024, 22:15:14 PM
Hello.
I converted plugin metacharacterscount (no longer supported) for Joomla 4. It works as stated in articles and other default locations

(https://footymania.co.uk/images/custom/metacount_article.jpg)

but I can't find the way to add virtuemart to plugin.

There is part of code "valid forms" for articles for example:

        $validForms = [
            'com_content.article'=> [
                'title'=>'jform[title]',
                'alias'=>'jform[alias]',
                'metadesc'=>'jform[metadesc]',
                'metakey'=>'jform[metakey]',
                'articletext'=>'jform[articletext]'
            ],

I tried add virtuemart but it not working.

'com_virtuemart.product'=> [
                'title'=>'jform[customtitle]',
                'metadesc'=>'jform[metadesc]',
                'metakey'=>'jform[metakey]'
            ],
Can you advise right code for meta forms in virtuemart please.
Thank you
Title: Re: Characters count for meta description
Post by: joozen on February 26, 2024, 23:19:26 PM
Looks like it won't work with Virtuemart. I checked page source and Virtuemart not using jform. Tried modify accordingly but not working anyway.
Title: Re: Characters count for meta description
Post by: joozen on February 27, 2024, 00:51:25 AM
Here is solution. Working in Old Admin Template (I'm not using new template due to bug with product sorting drag/drop)

Modify in admin com_virtuemart views/product/tmpl/product_edit_description.php (here is full code):

<fieldset>
    <legend><?php echo vmText::_('COM_VIRTUEMART_PRODUCT_FORM_S_DESC'); echo $this->origLang ?></legend>
    <textarea class="inputbox" name="product_s_desc" id="product_s_desc" cols="65" rows="3"><?php echo $this->product->product_s_desc?></textarea>
    <span id="s_desc-count"></span>
</fieldset>

<fieldset>
    <legend><?php echo vmText::_('COM_VIRTUEMART_PRODUCT_FORM_DESCRIPTION'); echo $this->origLang ?></legend>
    <?php echo $this->editor->display('product_desc',  $this->product->product_desc'90%;''450''55''10', array('pagebreak''readmore')); ?>
</fieldset>

<fieldset>
    <legend><?php echo vmText::_('COM_VIRTUEMART_METAINFO'); echo $this->origLang ?></legend>
    <?php echo shopFunctions::renderMetaEdit($this->product); ?>
    <span id="metadesc-count"></span>
</fieldset>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        var sDescTextarea = document.getElementById('product_s_desc');
        var sDescCount = document.getElementById('s_desc-count');

        sDescTextarea.addEventListener('input', function () {
            var charCount = this.value.length;
            sDescCount.textContent = charCount + " characters";
        });

        var metaDescTextarea = document.getElementById('metadesc');
        var metaDescCount = document.getElementById('metadesc-count');

        metaDescTextarea.addEventListener('input', function () {
            var charCount = this.value.length;
            metaDescCount.textContent = charCount + " characters";
        });
    });
</script>

Modify in admin com_virtuemart /helpers/shopfunctions.php around line 1545:

$html = '<table>
'.VmHTML::row('input','COM_VIRTUEMART_CUSTOM_PAGE_TITLE','customtitle',$obj->customtitle).'
'.VmHTML::row('textarea','COM_VIRTUEMART_METAKEY','metakey',$obj->metakey,'class="inputbox"',80).'
'.VmHTML::row('textarea','COM_VIRTUEMART_METADESC','metadesc',$obj->metadesc,'class="inputbox"',80).'
'.VmHtml::row('selectList','COM_VIRTUEMART_METAROBOTS','metarobot',$obj->metarobot,$options).'
'.VmHTML::row('input','COM_VIRTUEMART_METAAUTHOR','metaauthor',$obj->metaauthor).'
</table>';
return $html;

(https://footymania.co.uk/images/custom/meta_count.jpg)

(https://footymania.co.uk/images/custom/s_desc_count.jpg)

Title: Re: Characters count for meta description
Post by: joozen on February 27, 2024, 23:27:19 PM
Also. If you add to views/product/tmpl/product_edit_description.php script

<script>
var metaKeywordsField = document.getElementById('metakey');

metaKeywordsField.addEventListener('input', function() {
  var keywords = this.value.toLowerCase();

  // Replace only spaces that aren't immediately followed by a comma
  keywords = keywords.replace(/\s+(?=[^,\s])/g, ', ');

  keywords = keywords.trimEnd(); // Remove any trailing comma and space
  this.value = keywords;                         
});
</script>

It will convert text in metakeyword textarea dynamically to lovercase and add comma and space (for example if you want extract metakeyword from product title just paste title to textarea).
Title: Re: Characters count for meta description
Post by: joozen on March 03, 2024, 12:53:50 PM
Here is modified keyword converter script adding junk word blacklist, you can add your own anytime. (Please note, it works only for first paste, if you add any word manually after, it add comma to every word for some reason, I don't know how to fix it, so prepare text first and than paste it to field)

<script>
var metaKeywordsField = document.getElementById('metakey');
var blacklist = ['the', 'and', 'to', 'of', '&']; // Add your blacklist words here

metaKeywordsField.addEventListener('input', function() {
  var keywords = this.value.toLowerCase();

  // Replace only spaces that aren't immediately followed by a comma
  keywords = keywords.replace(/\s+(?=[^,\s])/g, ', ');

  // Split keywords into an array
  var keywordArray = keywords.split(', ');

  // Filter out blacklisted words
  keywordArray = keywordArray.filter(function(word) {
    return !blacklist.includes(word);
  });

  // Join the filtered array back into a string
  keywords = keywordArray.join(', ');

  keywords = keywords.trimEnd(); // Remove any trailing comma and space
  this.value = keywords;                         
});
</script>