Zen Cart Logo
Forums / Zen Cart Code Suggestions / admin duplicate a product: also copy metatags and linked categories

admin duplicate a product: also copy metatags and linked categories

Views: 128

Results 1 to 7 of 7
3 Apr 2015, 7:55 AM
#1
torvista avatar

torvista

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.

  1.  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
  1.  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?');
16 Aug 2017, 2:47 PM
#2
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,869
Plugin Contributions:
7

Re: admin duplicate a product: also copy metatags and linked categories

I use this functionality all the time, I still think it would be of use to others make "Duplicate" a full duplicate, with metatags and linked categories as options.

16 Aug 2017, 8:32 PM
#3
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: admin duplicate a product: also copy metatags and linked categories

+1 on putting the option to copy in the core. And thanks for bringing this back to the top of the forum because I am doing the changes you listed. I appreciate you sharing this code, it is very useful!

16 Aug 2017, 8:58 PM
#4
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: admin duplicate a product: also copy metatags and linked categories

I made a slight layout change to admin/product.php so it's more like the rest of the section

$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 /><br />' . TEXT_COPY_METATAGS . ' <br /> ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . '<br /> ' . zen_draw_radio_field('copy_metatags', 'copy_metatags_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO .
                                '<br /><br />' . TEXT_COPY_CATEGORIES . ' <br /> ' . zen_draw_radio_field('copy_categories', 'copy_categories_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . '<br /> ' . zen_draw_radio_field('copy_categories', 'copy_categories_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO . '<br /><br />');//mod to copy metags and linked categories 

Basically just a few break tags to space things a little and put yes/no on top of each other to match the copy attributes part and make it a little easier on the eyes.

17 Aug 2017, 11:11 AM
#5
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,869
Plugin Contributions:
7

Re: admin duplicate a product: also copy metatags and linked categories

I've just had a look at this now since I first posted the code.
I decided to reduce the space it all takes up rather than make it consistent.
I also reduced the hard-coded 25% for the td the infobox is located in (look for 25%).
I added a link to the Multiple Categories link manager in a more useful place.

 $contents = array('form' => zen_draw_form('copy_to', $type_admin_handler, 'action=copy_to_confirm&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . zen_draw_hidden_field('products_id', $pInfo->products_id));        $contents[] = array('text' => TEXT_INFO_COPY_TO_INTRO);
//steve this section added duplicate metatags/linked categories and moved sections around
//steve added products name and model for goldfish memory
          $contents[] = array('text' => '<b>' . $pInfo->products_model . ' - ' . $pInfo->products_name . '</b>');

        $contents[] = array('text' => '<br />' . TEXT_INFO_CURRENT_CATEGORIES . '<br /><b>' . zen_output_generated_category_path($pInfo->products_id, 'product') . '</b>');

          $contents[] = array('text' => '<a class="smallText" href="' . zen_href_link(FILENAME_PRODUCTS_TO_CATEGORIES . '.php', 'products_filter=' . $pInfo->products_id . '&current_category_id=' . $current_category_id). '">' . BUTTON_PRODUCTS_TO_CATEGORIES . '<a>');
          //$contents[] = array('text' => TEXT_INFO_COPY_TO_INTRO);
          //steve category dropdown moved up, copy button duplicated.
          $contents[] = array(
              'text' => TEXT_INFO_COPY_TO_INTRO . '<br />' . zen_draw_pull_down_menu('categories_id',
                      zen_get_category_tree(), $current_category_id)
          );
          $contents[] = array(
              'align' => 'left',
              'text' => zen_image_submit('button_copy.gif',
                      IMAGE_COPY) . ' <a href="' . zen_href_link(FILENAME_CATEGORIES,
                      'cPath=' . $cPath . '&pID=' . $pInfo->products_id . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . zen_image_button('button_cancel.gif',
                      IMAGE_CANCEL) . '</a>'
          );

//steve metatags and linked categories added to copy
          $contents[] = array(
              'text' => '<p>' . 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 .
                  '</p><p>' . TEXT_COPY_METATAGS . '<br >' . 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 .
                  '</p><p>' . TEXT_COPY_CATEGORIES . '<br />' . 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 . '</p>'
          );
        // only ask about attributes if they exist
        if (zen_has_product_attributes($pInfo->products_id, 'false')) {
            $contents[] = array('text' => zen_image(DIR_WS_IMAGES . 'pixel_black.gif','','100%','1'));
            $contents[] = array('text' => '<p>' . TEXT_COPY_ATTRIBUTES_ONLY);
          $contents[] = array('text' => TEXT_COPY_ATTRIBUTES . '<br />' . zen_draw_radio_field('copy_attributes', 'copy_attributes_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_attributes', 'copy_attributes_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO . '</p>');
// future          $contents[] = array('align' => 'center', 'text' => '<br />' . ATTRIBUTES_NAMES_HELPER . '<br />' . zen_draw_separator('pixel_trans.gif', '1', '10'));
          $contents[] = array('text' => zen_image(DIR_WS_IMAGES . 'pixel_black.gif','','100%','1'));
        }

        // only ask if product has discounts
17 Aug 2017, 11:58 PM
#6
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: admin duplicate a product: also copy metatags and linked categories

Yes, that seems better. I made one slight change to make it a little more clear that it is just for duplicating products by putting the line and the "Only used for duplicate product..." message which is the define TEXT_COPY_ATTRIBUTES_ONLY, above the new radio buttons so that it's included with the section to copy attributes. Very nice to be able to include these 2 areas when duplicating!

$contents = array('form' => zen_draw_form('copy_to', $type_admin_handler, 'action=copy_to_confirm&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . zen_draw_hidden_field('products_id', $pInfo->products_id));        $contents[] = array('text' => TEXT_INFO_COPY_TO_INTRO);
//steve this section added duplicate metatags/linked categories and moved sections around
//steve added products name and model for goldfish memory
          $contents[] = array('text' => '<b>' . $pInfo->products_model . ' - ' . $pInfo->products_name . '</b>');

        $contents[] = array('text' => '<br />' . TEXT_INFO_CURRENT_CATEGORIES . '<br /><b>' . zen_output_generated_category_path($pInfo->products_id, 'product') . '</b>');

          $contents[] = array('text' => '<a class="smallText" href="' . zen_href_link(FILENAME_PRODUCTS_TO_CATEGORIES . '.php', 'products_filter=' . $pInfo->products_id . '&current_category_id=' . $current_category_id). '">' . BUTTON_PRODUCTS_TO_CATEGORIES . '<a>');
          //$contents[] = array('text' => TEXT_INFO_COPY_TO_INTRO);
          //steve category dropdown moved up, copy button duplicated.
          $contents[] = array(
              'text' => TEXT_INFO_COPY_TO_INTRO . '<br />' . zen_draw_pull_down_menu('categories_id',
                      zen_get_category_tree(), $current_category_id) . '<br />'
          );
          $contents[] = array(
              'align' => 'left',
              'text' => zen_image_submit('button_copy.gif',
                      IMAGE_COPY) . ' <a href="' . zen_href_link(FILENAME_CATEGORIES,
                      'cPath=' . $cPath . '&pID=' . $pInfo->products_id . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . zen_image_button('button_cancel.gif',
                      IMAGE_CANCEL) . '</a>'
          );

//steve metatags and linked categories added to copy
          $contents[] = array(
              'text' => '<p>' . 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 .
                  '</p>' . zen_image(DIR_WS_IMAGES . 'pixel_black.gif','','100%','1') . '<p>' . TEXT_COPY_ATTRIBUTES_ONLY . '</p><p>' . TEXT_COPY_METATAGS . '<br >' . 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 .
                  '</p><p>' . TEXT_COPY_CATEGORIES . '<br />' . 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 . '</p>'
          );
        // only ask about attributes if they exist
        if (zen_has_product_attributes($pInfo->products_id, 'false')) {
          $contents[] = array('text' => TEXT_COPY_ATTRIBUTES . '<br /><p>' . zen_draw_radio_field('copy_attributes', 'copy_attributes_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES . ' ' . zen_draw_radio_field('copy_attributes', 'copy_attributes_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO . '</p>');
// future          $contents[] = array('align' => 'center', 'text' => '<br />' . ATTRIBUTES_NAMES_HELPER . '<br />' . zen_draw_separator('pixel_trans.gif', '1', '10'));
          //$contents[] = array('text' => zen_image(DIR_WS_IMAGES . 'pixel_black.gif','','100%','1'));
        }
// only ask if product has discounts

Now to do the same to copy_to_confirm for some other things such as product dimensions, thanks for making me remember I needed to do that!

12 Jun 2019, 9:35 PM
#7
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,869
Plugin Contributions:
7

Re: admin duplicate a product: also copy metatags and linked categories

Done in ZC1.57.