2

I am using Drupal Commerce Bulk Product Creation to add products to my site.

I would like to theme the Bulk Add Products form to make it more user-friendly to edit.

I can use page--admin--commerce--products--add-bulk--PRODUCTNAME.tpl.php to override the form. However, I don't know what PHP code to use to print out the product fields.

I found some articles online (1, 2, 3) and have tried the following, but none of them work:

<?php print render($content['form']['product:field_FIELDNAME']); ?>
<?php print render($content['form']['field_FIELDNAME']); ?>
<?php print drupal_render($field_FIELDNAME) ?>
<?php print render($field_FIELDNAME) ?>
<?php print drupal_render($vars['form']['field_FIELDNAME']);     ?>
<?php print drupal_render_children($form['field_FIELDNAME']); ?>

Any pointers would be appreciated.

2 Answers 2

3

I have figured this out.

Step 1): Find the ID of your form. To do this, view the source of the form and search for "form" keep on searching until you see a field like this:

<form enctype="multipart/form-data" action="SOME_URL_HERE" method="post" id="commerce-bpc-create-bulk-form" accept-charset="UTF-8"><div>

The id you want is in the id="" string

You need to tell Drupal where to find your tpl.php file. In template.php add the following:

function YOURTHEME_theme($existing, $type, $theme, $path) {
    return array(
        'INSERT_ID_HERE' => array(
            'arguments' => array('form' => NULL),
            'template' => 'templates/add/node--add--TEMPLATE_NAME',
            'render element' => 'form',
            ),
    );
}

So if you were themeing a Drupal Commerce Bulk Product Creation form, your code would look like this:

function YOURTHEME_theme($existing, $type, $theme, $path) {
    return array(
        'commerce_bpc_create_bulk_form' => array(
            'arguments' => array('form' => NULL),
            'template' => 'templates/add/node--add--bpc',
            'render element' => 'form',
            ),
    );
}

3) Create a tpl.php template file and save it in the appropriate directory. In our example, the file is called node--add--bpc.tpl.php and is saved in templates/add

4) In your tpl.php file, insert variables for the fields on the form. Here is a list of the most common fields:

<?php print drupal_render($form['product']['sku_fragment']); ?>
<?php print drupal_render($form['product']['title_fragment']); ?>
<?php print drupal_render($form['product']['commerce_price']); ?>
<?php print drupal_render($form['submit']); ?>
<?php print drupal_render($form['static_values']['field_YOUR_CUSTOM_FIELD']); ?>

5) At the end of the tpl.php file, put the following code:

 <?php print  drupal_render_children($form) ?>  

It's very important you put this code, or you will get errors when trying to upload images.

6) The code in step 5, might print out fields you do not want. So you to have unset these fields. Here is a list of the most common fields to unset.

<?php unset ($form['product']); ?>
<?php unset ($form['combinations']); ?>
<?php unset ($form['title']); ?>
<?php unset ($form['static_values']); ?>
<?php unset ($form['description']); ?>

You put all this code on a line before:

 <?php print  drupal_render_children($form) ?>  

7) If you find that any of the steps don't work, be sure to flush your cache. You can do this at: yousite.com/admin/config/development/performance

0

Thanks. It worked great!

function STARTERKIT_theme($existing, $type, $theme, $path) {
    return array(
            'project_node_form' => array(
                    'arguments' => array('form' => NULL),
                    'template' => 'templates/node--add--projects',
                    'render element' => 'form',
            ),
            'idea_or_proposal_node_form' => array(
                    'arguments' => array('form' => NULL),
                    'template' => 'templates/node--add--idea-or-proposal',
                    'render element' => 'form',
            ),
            'quick_announce_node_form' => array(
                    'arguments' => array('form' => NULL),
                    'template' => 'templates/node--add--quick-announce',
                    'render element' => 'form',
            ),
    );
}

And next i created all 3 templates files inside my themes templates directory (clear cache).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.