the creation of this little vqmod took me awhile.
Its for a shop which is selling braceletts. The customer can choose between many different pendants and should be limted to choose only a maximum amount of pendants.
Iam using the checkbox option. The mod creates a new field in the admin option form called "max_option". There you can enter the max amount.
The Checkbox option itself is then added a picture.
Since I used an huge amount of posts from this forum, I like to share it with you AND hear your comments and suggestions. I assume that this code has much potential to be improved.
This is the Script:
Code: Select all
- <modification>
<id>adding pictures to the checkbox option. resizing the picture limiting the max number of options to be checked creating backend field to define max_option per option in options</id>
<version>0.1</version>
<vqmer>1.0.8</vqmer>
<author>benjamin lurz</author>
- <!-- START alter the option view
-->
- <file name="catalog/view/theme/Spicylicious/template/product/product.tpl">
- <operation>
- <!-- With this php snipet we get a variable $boxen which contains all information about options and checkboxIDs. this variable is used in the Javascript.
-->
- <search position="after">
- <![CDATA[ <?php if ($options) { ?>
]]>
</search>
- <add>
- <![CDATA[
<?php
foreach ($options as $option)
{
if ($option['type'] == 'checkbox')
{
$boxen=$boxen."start".",".$option['product_option_id'].",".$option['max_option'].",";
foreach ($option['option_value'] as $option_value)
{
$boxen=$boxen.$option_value['product_option_value_id'].",";
}
}
} $option['max_option']
?>
]]>
</add>
</operation>
- <operation>
- <!-- Change the way the Checkboxes are displayed. Make them call the KeepCount Methode with its option name as a parameter
-->
- <search position="replace" offset="15">
- <![CDATA[ <?php if ($option['type'] == 'checkbox') { ?>
]]>
</search>
- <add>
- <![CDATA[
<?php if ($option['type'] == 'checkbox') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option"><b>
<?php if ($option['required']) { ?>
<span class="required">♥</span>
<?php } ?>
<?php echo $option['name']; ?>:</b>
<?php foreach ($option['option_value'] as $option_value) { ?>
<div class="option_checkbox">
<div class="checkbox_pic">
<img src="<?php echo $option_value['image']; ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" />
</div>
<div class="checkbox_label">
<input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" value="<?php echo $option_value['product_option_value_id']; ?>" id="option-value-<?php echo $option_value['product_option_value_id']; ?>" onClick="return KeepCount(<?php echo $option['product_option_id']; ?>)" />
<label for="option-value-<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
<?php } ?>
</label>
</div>
<br />
</div>
<?php } ?>
</div> <div class="clear"></div>
<script type="text/javascript">
function KeepCount(boxOption)
{
/*temp calls the php variable and split it into an array */
var count = 0;
var temp = "<?php echo $boxen; ?>";
var box = temp.split(",");
box.pop(); /* last element is empty becaus of the "," and gets deleted here */
var boxen = new Array();
var opnr=0;
var pos=1; /* hast to be 0 for it will be pos++ when filling beginns*/
var boxlength=box.length;
/*arranging the box array into the multiarray to use it later */
for(var i=0; i<boxlength; i++)
{
if(box[i]!="start")
{
pos++;
boxen[opnr][pos]=box[i];
}
if(box[i]=="start")
{
opnr++;
boxen[opnr]= new Array();
boxen[opnr][0]=box[i+1];
boxen[opnr][1]=box[i+2];
i=i+2;
pos=1
}
}
/* checking the boxes. Deciding which group of boxes with the given variable boxOption */
opnr=1;
pos=0;
while(boxen[opnr][pos]!=null)
{
if(boxen[opnr][pos]==boxOption)
{
pos=2;
while(boxen[opnr][pos]!=null)
{
var name="option-value-"+boxen[opnr][pos];
if(document.getElementById(name).checked)
{
count++;
}
pos++;
}
}
else
{
opnr++;
}
}
if (count > boxen[opnr][1] && boxen[opnr][1]!=0)
{
return false;
}
}
</script>
]]>
</add>
</operation>
</file>
- <file name="catalog/controller/product/product.php">
- <operation>
- <search position="replace">
- <![CDATA[
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
]]>
</search>
- <add>
- <![CDATA[
'image' => $this->model_tool_image->resize($option_value['image'], 300, 300),
]]>
</add>
</operation>
</file>
- <!-- END alter the option view
-->
- <!-- START alter the backend variables
-->
- <!-- description in backend variables
-->
- <file name="admin/language/de_DE/catalog/option.php">
- <operation>
- <search position="before">
- <![CDATA[ $_['entry_sort_order']
]]>
</search>
- <add>
- <![CDATA[ $_['entry_max_option'] = 'Limit der Auswahl';
]]>
</add>
</operation>
</file>
- <file name="admin/language/english/catalog/option.php">
- <operation>
- <search position="before">
- <![CDATA[ $_['entry_sort_order']
]]>
</search>
- <add>
- <![CDATA[ $_['entry_max_option'] = 'Limit of options';
]]>
</add>
</operation>
</file>
- <!-- adoption of variables
-->
- <file name="admin/controller/catalog/option.php">
- <operation>
- <search position="before">
- <![CDATA[ $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
]]>
</search>
- <add>
- <![CDATA[ $this->data['entry_max_option'] = $this->language->get('entry_max_option');
]]>
</add>
</operation>
- <!-- read write
-->
- <operation>
- <search position="before">
- <![CDATA[ if (isset($this->request->post['sort_order'])) {
]]>
</search>
- <add>
- <![CDATA[
if (isset($this->request->post['max_option'])) {
$this->data['max_option'] = $this->request->post['max_option'];
} elseif (!empty($option_info)) {
$this->data['max_option'] = $option_info['max_option'];
} else {
$this->data['max_option'] = '';
}
]]>
</add>
</operation>
</file>
- <!-- Db entry
-->
- <file name="admin/model/catalog/option.php">
- <!-- set New
-->
- <operation>
- <search position="replace">
- <![CDATA[ $this->db->query("INSERT INTO `" . DB_PREFIX . "option` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "'");
]]>
</search>
- <add>
- <![CDATA[ $this->db->query("INSERT INTO `" . DB_PREFIX . "option` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "', max_option = '" . $this->db->escape($data['max_option']) . "'");
]]>
</add>
</operation>
- <!-- set update
-->
- <operation>
- <search position="replace">
- <![CDATA[ this->db->query("UPDATE `" . DB_PREFIX . "option` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE option_id = '" . (int)$option_id . "'");
]]>
</search>
- <add>
- <![CDATA[ this->db->query("UPDATE `" . DB_PREFIX . "option` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "', max_option = '" . $this->db->escape($data['max_option']) . "' WHERE option_id = '" . (int)$option_id . "'");
]]>
</add>
</operation>
- <!-- get
-->
- <operation>
- <search position="replace" offset="0">
- <![CDATA[ 'o.sort_order'
]]>
</search>
- <add>
- <![CDATA[
'o.sort_order',
'o.max_option'
]]>
</add>
</operation>
</file>
- <!-- admin view change
-->
- <file name="admin/view/template/catalog/option_form.tpl">
- <operation>
- <search position="replace" offset="2">
- <![CDATA[ <td><?php echo $entry_sort_order; ?></td>
]]>
</search>
- <add>
- <![CDATA[
<td><?php echo $entry_sort_order; ?></td>
<td><input type="text" name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
</tr>
<tr>
<td><?php echo $entry_max_option; ?></td>
<td><input type="text" name="max_option" value="<?php echo $max_option; ?>" size="1" /> </td>
</tr>
]]>
</add>
</operation>
</file>
- <!-- front end change
-->
- <!-- we dont neet to create text variables, since this is only for our litte php/js script
-->
- <!-- getoption
-->
- <file name="catalog/model/catalog/product.php">
- <operation>
- <search position="after">
- <![CDATA[ 'type' => $product_option['type'],
]]>
</search>
- <add>
- <![CDATA[ 'max_option' => $product_option['max_option'],
]]>
</add>
</operation>
</file>
- <!-- takeover into the controller
-->
- <file name="catalog/controller/product/product.php">
- <operation>
- <search position="after">
- <![CDATA[ 'type' => $option['type'],
]]>
</search>
- <add>
- <![CDATA[ 'max_option' => $option['max_option'],
]]>
</add>
</operation>
</file>
</modification>