function fetchElement($name, $value, &$node, $control_name) { $output = '<span class="k2Note">' . JText::_('Click Apply to be able to create product attributes') . '</span>'; $id = JRequest::getInt('cid'); if ($id) { $K2Item = JTable::getInstance('K2Item', 'Table'); $K2Item->load($id); $params = new K2Parameter($K2Item->plugins, JPATH_PLUGINS . '/k2/tienda.xml', 'tienda'); $productID = $params->get('productID'); if ($productID) { Tienda::load('TiendaUrl', 'library.url'); Tienda::load("TiendaHelperProduct", 'helpers.product'); $attributes = TiendaHelperProduct::getAttributes($productID); $output = '<div class="tiendaButton">' . TiendaUrl::popup("index.php?option=com_tienda&controller=products&task=setattributes&id=" . $productID . "&tmpl=component", JText::_('Set Attributes')) . '</div>'; $output .= '<div>'; foreach (@$attributes as $attribute) { $output .= ' <div> <span>' . $attribute->productattribute_name . '(' . $attribute->option_names_csv . ')</span> <div class="tiendaButton">' . TiendaUrl::popup("index.php?option=com_tienda&controller=products&task=setattributeoptions&id=" . $attribute->productattribute_id . "&tmpl=component", JText::_('Set Attribute Options')) . '</div> <div class="tiendaButton"><a href="' . "index.php?option=com_tienda&controller=productattributes&task=delete&cid[]=" . $attribute->productattribute_id . "&return=" . base64_encode("index.php?option=com_k2&view=item&cid=" . $id) . '">' . JText::_('Remove') . '</a></div> </div>'; } $output .= '</div>'; } } return $output; }
defined('_JEXEC') or die('Restricted access'); ?> <div class="product_buy"> <div> <form action="" method="post" class="adminform" name="adminForm" enctype="multipart/form-data"> <!--base price--> <span class="product_price"><?php echo TiendaHelperBase::currency($product->price); ?> </span> <!--attribute options--> <div id='product_attributeoptions'> <?php $attributes = TiendaHelperProduct::getAttributes($product->product_id); foreach ($attributes as $attribute) { ?> <div class="pao" id='productattributeoption_<?php echo $attribute->productattribute_id; ?> '> <?php echo TiendaSelect::productattributeoptions($attribute->productattribute_id, '', 'attribute_' . $attribute->productattribute_id); ?> </div> <?php } ?> </div>
<?php if (!empty($this->display_cartbutton)) { ?> <!--attribute options--> <div id='product_attributeoptions_<?php echo $item->product_id; ?> ' class="product_attributeoptions"> <?php // Selected attribute options (for child attributes) $selected_opts = !empty($this->selected_opts) ? json_decode($this->selected_opts) : 0; if (!count($selected_opts)) { $selected_opts = 0; } $attributes = TiendaHelperProduct::getAttributes($item->product_id, $selected_opts); $default = TiendaHelperProduct::getDefaultAttributeOptions($attributes); // First view of the page: select the first value in the list if (!$selected_opts) { $selected_opts = $default; $selected_opts[] = 0; } foreach ($attributes as $attribute) { ?> <div class="pao" id='productattributeoption_<?php echo $attribute->productattribute_id; ?> '> <?php echo "<span>" . $attribute->productattribute_name . " : </span>"; $key = 'attribute_' . $attribute->productattribute_id;
</td> </tr> <?php } else { // display lightbox link to manage attributes ?> <tr> <td class="dsc-key"><?php echo JText::_('COM_TIENDA_PRODUCT_ATTRIBUTES'); ?> :</td> <td class="dsc-value">[<?php echo TiendaUrl::popup("index.php?option=com_tienda&view=products&task=setattributes&id=" . $row->product_id . "&tmpl=component", JText::_('COM_TIENDA_SET_ATTRIBUTES'), array('onclose' => '\\function(){tiendaNewModal(\'' . JText::_('COM_TIENDA_SAVING_THE_PRODUCT') . '\'); submitbutton(\'apply\');}')); ?> ] <?php $attributes = $helper_product->getAttributes($row->product_id); ?> <div id="current_attributes"> <?php foreach (@$attributes as $attribute) { ?> [<a href="<?php echo "index.php?option=com_tienda&view=productattributes&task=delete&cid[]=" . $attribute->productattribute_id . "&return=" . base64_encode("index.php?option=com_tienda&view=products&task=edit&id=" . $row->product_id); ?> "> <?php echo JText::_('COM_TIENDA_REMOVE'); ?> </a>] [ <?php echo TiendaUrl::popup("index.php?option=com_tienda&view=products&task=setattributeoptions&id=" . $attribute->productattribute_id . "&tmpl=component", JText::_('Set Attribute Options'), array('onclose' => '\\function(){tiendaNewModal(\'' . JText::_('COM_TIENDA_SAVING_THE_PRODUCT') . '\'); submitbutton(\'apply\');}')); ?>
public static function calculateProductAttributeProperty(&$product, $attributes, $product_price, $product_weight) { Tienda::load('TiendaHelperBase', 'helpers._base'); $helper_product = TiendaHelperBase::getInstance('Product'); // first we get rid off phantom attributes (the ones that should be hidden because their parent attribute was just unselected) $attr_base = TiendaHelperProduct::getAttributes($product->product_id, array_merge($attributes, array('0'))); $attr_final = TiendaHelperProduct::getDefaultAttributeOptions($attr_base); foreach ($attr_final as $key => $value) { if (isset($attributes['attribute_' . $key])) { $attr_final[$key] = $attributes['attribute_' . $key]; } } Tienda::load('TiendaQuery', 'library.query'); $q = new TiendaQuery(); $q->select('tbl.`productattributeoption_price` , tbl.`productattributeoption_prefix`, tbl.`productattributeoption_id` '); $q->select('tbl.`productattributeoption_code`, tbl.`productattributeoption_weight`, tbl.`productattributeoption_prefix_weight`'); $q->from('`#__tienda_productattributeoptions` tbl'); $q->join('left', '`#__tienda_productattributes` atr ON tbl. productattribute_id = atr.productattribute_id'); $q->where("tbl.productattributeoption_id IN ('" . implode("', '", $attr_final) . "')"); $q->order('atr.ordering ASC'); $db = JFactory::getDbo(); $db->setQuery($q); $res = $db->loadObjectList(); $attributes = array(); for ($i = 0, $c = count($res); $i < $c; $i++) { // update product price // is not + or - if ($res[$i]->productattributeoption_prefix == '=') { $product->{$product_price} = floatval($res[$i]->productattributeoption_price); } else { $product->{$product_price} = $product->{$product_price} + floatval($res[$i]->productattributeoption_prefix . $res[$i]->productattributeoption_price); } // update product weight if ($res[$i]->productattributeoption_prefix_weight == '=') { $product->{$product_weight} = floatval($res[$i]->productattributeoption_weight); } else { $product->{$product_weight} = $product->{$product_weight} + floatval($res[$i]->productattributeoption_prefix_weight . $res[$i]->productattributeoption_weight); } $attributes[] = $res[$i]->productattributeoption_id; } $product->sku = self::getProductSKU($product, $attributes); }
/** * Saves an item and redirects based on task * @return void */ function save() { $task = JRequest::getVar('task'); $model = $this->getModel($this->get('suffix')); $isSaveAs = false; $row = $model->getTable(); $row->load($model->getId()); $row->bind(JRequest::get('POST')); $row->product_description = JRequest::getVar('product_description', '', 'post', 'string', JREQUEST_ALLOWRAW); $row->product_description_short = JRequest::getVar('product_description_short', '', 'post', 'string', JREQUEST_ALLOWRAW); // set the id as 0 for new entry if ($task == "save_as") { unset($row); // load WITHOUT EAV! otherwise the save will fail $row = $model->getTable(); $row->load($model->getId(), true, false); $row->bind(JRequest::get('POST')); $row->product_description = JRequest::getVar('product_description', '', 'post', 'string', JREQUEST_ALLOWRAW); $row->product_description_short = JRequest::getVar('product_description_short', '', 'post', 'string', JREQUEST_ALLOWRAW); $isSaveAs = true; $oldProductImagePath = $row->getImagePath(); $pk = $row->getKeyName(); $oldPk = $row->{$pk}; // these get reset $row->{$pk} = 0; $row->product_images_path = ''; $row->product_rating = ''; $row->product_comments = ''; } $row->_isNew = empty($row->product_id); $fieldname = 'product_full_image_new'; $userfiles = JRequest::getVar($fieldname, '', 'files', 'array'); // save the integrations $row = $this->prepareParameters($row); //check if normal price exists Tienda::load("TiendaHelperProduct", 'helpers.product'); if ($isSaveAs) { // and the prices $prices = TiendaHelperProduct::getPrices($oldPk); } else { $prices = TiendaHelperProduct::getPrices($row->product_id); } if ($row->save()) { $row->product_id = $row->id; $model->setId($row->id); $this->messagetype = 'message'; $this->message = JText::_('COM_TIENDA_SAVED'); // check it's new entry or empty price but not save as if (($row->_isNew || empty($prices)) && !$isSaveAs) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'TiendaTable'); $price->product_id = $row->id; $price->product_price = JRequest::getVar('product_price'); $price->group_id = Tienda::getInstance()->get('default_user_group', '1'); if (!$price->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $price->getError(); } } if ($row->_isNew && !$isSaveAs) { // set category $category = JTable::getInstance('Productcategories', 'TiendaTable'); $category->product_id = $row->id; $category->category_id = JRequest::getVar('category_id'); if (!$category->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $category->getError(); } // save default quantity $quantity = JTable::getInstance('Productquantities', 'TiendaTable'); $quantity->product_id = $row->id; $quantity->quantity = JRequest::getInt('product_quantity'); if (!$quantity->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $quantity->getError(); } } if ($isSaveAs) { // set price when cloning $priceTable = JTable::getInstance('Productprices', 'TiendaTable'); foreach ($prices as $price) { $priceTable->product_id = $row->id; $priceTable->product_price = $price->product_price; $priceTable->product_price_startdate = $price->product_price_startdate; $priceTable->product_price_enddate = $price->product_price_enddate; $priceTable->created_date = $price->created_date; $priceTable->modified_date = $price->modified_date; $priceTable->group_id = $price->group_id; $priceTable->price_quantity_start = $price->price_quantity_start; $priceTable->price_quantity_end = $price->price_quantity_end; if (!$priceTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $priceTable->getError(); } } // set category $categoryTable = JTable::getInstance('Productcategories', 'TiendaTable'); $categories = TiendaHelperProduct::getCategories($oldPk); foreach ($categories as $category) { $categoryTable->product_id = $row->id; $categoryTable->category_id = $category; if (!$categoryTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $categoryTable->getError(); } } // TODO Save Attributes // An array to map attribute id old attribute id are as key and new attribute id are as value $attrbuteMappingArray = array(); $attrbuteParentMappingArray = array(); $attributes = TiendaHelperProduct::getAttributes($oldPk); foreach ($attributes as $attribute) { $attributeTable = JTable::getInstance('ProductAttributes', 'TiendaTable'); $attributeTable->productattribute_name = $attribute->productattribute_name; $attributeTable->product_id = $row->id; $attributeTable->ordering = $attribute->ordering; if ($attributeTable->save()) { $attrbuteMappingArray[$attribute->productattribute_id] = $attributeTable->productattribute_id; $attrbuteParentMappingArray[$attributeTable->productattribute_id] = $attribute->parent_productattributeoption_id; } else { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeTable->getError(); } } // set Attribute options $attrbuteOptionsMappingArray = array(); foreach ($attrbuteMappingArray as $oldAttrbuteId => $newAttributeId) { // set Attribute options $options = TiendaHelperProduct::getAttributeOptionsObjects($oldAttrbuteId); foreach ($options as $option) { $attributeOptionsTable = JTable::getInstance('ProductAttributeOptions', 'TiendaTable'); $attributeOptionsTable->productattribute_id = $newAttributeId; $attributeOptionsTable->productattributeoption_name = $option->productattributeoption_name; $attributeOptionsTable->productattributeoption_price = $option->productattributeoption_price; $attributeOptionsTable->productattributeoption_prefix = $option->productattributeoption_prefix; $attributeOptionsTable->productattributeoption_code = $option->productattributeoption_code; $attributeOptionsTable->ordering = $option->ordering; if ($attributeOptionsTable->save()) { $attrbuteOptionsMappingArray[$option->productattributeoption_id] = $attributeOptionsTable->productattributeoption_id; } else { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeOptionsTable->getError(); } } // save parent relationship if ($attrbuteParentMappingArray[$newAttributeId]) { $attributeTable = JTable::getInstance('ProductAttributes', 'TiendaTable'); $attributeTable->load($newAttributeId); $attributeTable->parent_productattributeoption_id = $attrbuteOptionsMappingArray[$attrbuteParentMappingArray[$newAttributeId]]; if (!$attributeTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $attributeTable->getError(); } } } // set quantity $quantityTable = JTable::getInstance('Productquantities', 'TiendaTable'); $quantities = TiendaHelperProduct::getProductQuantitiesObjects($oldPk); foreach ($quantities as $quantity) { $quantityTable->product_attributes = $quantity->product_attributes; $quantityTable->product_id = $row->id; $quantityTable->vendor_id = $quantity->vendor_id; $quantityTable->quantity = $quantity->quantity; $optionsCSV = $quantity->product_attributes; $options = explode(",", $optionsCSV); $newOptions = array(); foreach ($options as $option) { $newOptions[] = $attrbuteOptionsMappingArray[$option]; } $optionsCSV = implode(",", $newOptions); $quantityTable->product_attributes = $optionsCSV; if (!$quantityTable->save()) { $this->messagetype = 'notice'; $this->message .= " :: " . $quantityTable->getError(); } } // copy all gallery files jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); $galleryFiles = JFolder::files($oldProductImagePath); // get all gallery images if (count($galleryFiles)) { JFolder::create($row->getImagePath()); // create folder for images JFolder::create($row->getImagePath() . 'thumbs'); // create folder for thumbnails images for ($i = 0, $c = count($galleryFiles); $i < $c; $i++) { // copy only images with both original file and a corresponding thumbnail if (JFile::exists($oldProductImagePath . 'thumbs/' . $galleryFiles[$i]) && JFile::exists($oldProductImagePath . $galleryFiles[$i])) { JFile::copy($oldProductImagePath . $galleryFiles[$i], $row->getImagePath() . DS . $galleryFiles[$i]); JFile::copy($oldProductImagePath . 'thumbs/' . $galleryFiles[$i], $row->getImagePath() . '/thumbs/' . $galleryFiles[$i]); } } } // duplicate product files (only in db) $modelFiles = $this->getModel('productfiles'); $modelFiles->setState('filter_product', $oldPk); $listFiles = $modelFiles->getList(); if (count($listFiles)) { $row_file = JTable::getInstance('Productfiles', 'TiendaTable'); for ($i = 0, $c = count($listFiles); $i < $c; $i++) { $row_file->bind($listFiles[$i]); // bind old data $row_file->productfile_id = 0; // will be set $row_file->product_id = $row->product_id; // use clone's ID $row_file->save(); // save the data } } // create duplicate connections for EAV custom fields JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models'); $model = JModel::getInstance('EavAttributes', 'TiendaModel'); $model->setState('filter_entitytype', 'products'); $model->setState('filter_entityid', $oldPk); $listEAV = $model->getList(); $teav = $model->getTable(); if (is_array($listEAV)) { for ($i = 0, $c = count($listEAV); $i < $c; $i++) { $tblEAV = JTable::getInstance('EavAttributeEntities', 'TiendaTable'); $tblEAV->eaventity_id = $row->product_id; $tblEAV->eaventity_type = 'products'; $tblEAV->eavattribute_id = $listEAV[$i]->eavattribute_id; $tblEAV->save(); // Clone the values too! $teav->load($listEAV[$i]->eavattribute_id); $value = TiendaHelperEav::getAttributeValue($teav, 'products', $row->product_id); $newValue = JTable::getInstance('EavValues', 'TiendaTable'); $newValue->setType($teav->eavattribute_type); $newValue->eavattribute_id = $listEAV[$i]->eavattribute_id; $newValue->eaventity_id = $row->product_id; // Store the value $newValue->eavvalue_value = $value; $newValue->eaventity_type = 'products'; $newValue->store(); } } } // Multiple images processing $i = 0; $error = false; while (!empty($userfiles['size'][$i])) { $dir = $row->getImagePath(true); if ($upload = $this->addimage($fieldname, $i, $dir)) { // The first One is the default (if there is no default yet) if ($i == 0 && (empty($row->product_full_image) || $row->product_full_image == '')) { $row->product_full_image = $upload->getPhysicalName(); // need to re-save in this instance // should we be storing or saving? $row->save(); } } else { $error = true; } $i++; } if ($error) { $this->messagetype = 'notice'; $this->message .= " :: " . $this->getError(); } $helper = new TiendaHelperProduct(); $helper->onAfterSaveProducts($row); $model->clearCache(); $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onAfterSave' . $this->get('suffix'), array($row)); } else { $this->messagetype = 'notice'; $this->message = JText::_('COM_TIENDA_SAVE_FAILED') . " - " . $row->getError(); } $redirect = "index.php?option=com_tienda"; switch ($task) { case "save_as": $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $row->product_id; $this->message .= " - " . JText::_('COM_TIENDA_YOU_ARE_NOW_EDITING_NEW_PRODUCT'); break; case "saveprev": $redirect .= '&view=' . $this->get('suffix'); // get prev in list Tienda::load("TiendaHelperProduct", 'helpers.product'); $surrounding = TiendaHelperProduct::getSurrounding($model->getId()); if (!empty($surrounding['prev'])) { $redirect .= '&task=edit&id=' . $surrounding['prev']; } break; case "savenext": $redirect .= '&view=' . $this->get('suffix'); // get next in list Tienda::load("TiendaHelperProduct", 'helpers.product'); $surrounding = TiendaHelperProduct::getSurrounding($model->getId()); if (!empty($surrounding['next'])) { $redirect .= '&task=edit&id=' . $surrounding['next']; } break; case "savenew": $redirect .= '&view=' . $this->get('suffix') . '&task=add'; break; case "apply": $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $model->getId(); break; case "save": default: $redirect .= "&view=" . $this->get('suffix'); break; } $redirect = JRoute::_($redirect, false); $this->setRedirect($redirect, $this->message, $this->messagetype); }