Totally Zenned
- Join Date:
- Aug 2007
- Location:
- Gijón, Asturias, Spain
- Posts:
- 2,869
- Plugin Contributions:
- 7
admin duplicate a product: also copy metatags and linked categories
When I duplicate a product I will usually want almost exactly the same metatags and probably the same (huge number) of linked categories, so I usually want these characteristics copied too, or at least be offered the option when copying.
This is my code to allow the metatags and linked categories to be copied to a duplicate product as part of the product copy function.
-
In admin/product.php
While it would be logical to add the two new extra options to two new entries in the $contents array, that means two more table rows in the info box and it takes up too much space visually.
Find
$contents[] = array('text' => '<br />' . TEXT_HOW_TO_COPY . '<br />' . zen_draw_radio_field('copy_as', 'link', true) . ' ' . TEXT_COPY_AS_LINK . '<br />' . zen_draw_radio_field('copy_as', 'duplicate') . ' ' . TEXT_COPY_AS_DUPLICATE);
Change to
```php
$contents[] = array('text' => '<br />' . TEXT_HOW_TO_COPY . '<br />' . zen_draw_radio_field('copy_as', 'link', true) . ' ' . TEXT_COPY_AS_LINK . '<br />' . zen_draw_radio_field('copy_as', 'duplicate') . ' ' . TEXT_COPY_AS_DUPLICATE .
'<br />' . TEXT_COPY_METATAGS . ' ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO .
'<br />' . TEXT_COPY_CATEGORIES . ' ' . zen_draw_radio_field('copy_categories', 'copy_categories_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_categories', 'copy_categories_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO);//mod to copy metags and linked categories
In this example the radio buttons are checked as true as default. To make them false use this:
```php
'<br />' . TEXT_COPY_METATAGS . ' ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_yes') . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_no', true) . ' ' . TEXT_COPY_ATTRIBUTES_NO .
'<br />' . TEXT_COPY_CATEGORIES . ' ' . zen_draw_radio_field('copy_categories', 'copy_categories_yes') . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_categories', 'copy_categories_no', true) . ' ' . TEXT_COPY_ATTRIBUTES_NO); //mod to copy metags and linked categories
-
In admin/modules/copy_to_confirm.php
Find
$description->MoveNext();
// FIX HERE
Change to
```php
$description->MoveNext();
//bof mod to copy meta tags to a DUPLICATE product
if ($_POST['copy_metatags'] == 'copy_metatags_yes') {
$metatags_status = $db->Execute("select metatags_title_status, metatags_products_name_status, metatags_model_status, metatags_price_status, metatags_title_tagline_status
from " . TABLE_PRODUCTS . "
where products_id = '" . (int)$old_products_id . "'");
$db->Execute("update " . TABLE_PRODUCTS . " set
metatags_title_status = '" . zen_db_input($metatags_status->fields['metatags_title_status']). "',
metatags_products_name_status = '" . zen_db_input($metatags_status->fields['metatags_products_name_status']). "',
metatags_model_status = '" . zen_db_input($metatags_status->fields['metatags_model_status']). "',
metatags_price_status= '" . zen_db_input($metatags_status->fields['metatags_price_status']). "',
metatags_title_tagline_status = '" . zen_db_input($metatags_status->fields['metatags_title_tagline_status']). "'
where products_id = '" . (int)$dup_products_id . "'");
$metatags_descriptions = $db->Execute("select language_id, metatags_title, metatags_keywords, metatags_description
from " . TABLE_META_TAGS_PRODUCTS_DESCRIPTION . "
where products_id = '" . (int)$old_products_id . "'");
while (!$metatags_descriptions->EOF) {//one row per language
$db->Execute("insert into " . TABLE_META_TAGS_PRODUCTS_DESCRIPTION . " (products_id, language_id, metatags_title, metatags_keywords, metatags_description)
values (
'" . (int)$dup_products_id . "',
'" . (int)$metatags_descriptions->fields['language_id'] . "',
'" . zen_db_input($metatags_descriptions->fields['metatags_title']) . "',
'" . zen_db_input($metatags_descriptions->fields['metatags_keywords']) . "',
'" . zen_db_input($metatags_descriptions->fields['metatags_description']). "')");
$messageStack->add_session('Metatags for language ' . (int)$metatags_descriptions->fields['language_id'] . ' copied from Product ID# ' . $old_products_id . ' to duplicate Product ID# ' . $dup_products_id , 'success');
$metatags_descriptions->MoveNext();
}
}//eof mod copy metatags
//bof mod to copy linked categories to a DUPLICATE product
if ($_POST['copy_categories'] == 'copy_categories_yes') {
$categories_from = $db->Execute("select categories_id
from " . TABLE_PRODUCTS_TO_CATEGORIES . "
where products_id = '" . (int)$old_products_id . "'");
while (!$categories_from->EOF) {//"insert ignore" as the new product already has one entry for the master category
$db->Execute("insert ignore into " . TABLE_PRODUCTS_TO_CATEGORIES . "
(products_id, categories_id)
values ('" . (int)$dup_products_id . "', '" . (int)$categories_from->fields['categories_id'] . "')");
$messageStack->add_session('Linked category ' . (int)$categories_from->fields['categories_id'] . ' copied from Product ID# ' . $old_products_id . ' to duplicate Product ID# ' . $dup_products_id , 'success');
$categories_from->MoveNext();
}
}//eof mod copy categories
// FIX HERE
3) Language defines
Put in languages/english/product.php (or some other file in /extra_defines if you want to keep this file original).
```php
//Product Copy to a Duplicate
define('TEXT_COPY_METATAGS', 'Copy metatags?');
define('TEXT_COPY_CATEGORIES', 'Copy linked categories?');