/** * Initializes page content variables */ public function initContent() { parent::initContent(); $this->setTemplate(_PS_THEME_DIR_ . 'category.tpl'); if (!$this->customer_access) { return; } if (isset($this->context->cookie->id_compare)) { $this->context->smarty->assign('compareProducts', CompareProduct::getCompareProducts((int) $this->context->cookie->id_compare)); } // Product sort must be called before assignProductList() $this->productSort(); $this->assignScenes(); $this->assignSubcategories(); $this->assignProductList(); $products = $this->category->getProducts($this->context->language->id, (int) $this->p, (int) $this->n, $this->orderBy, $this->orderWay); $products_options = ProductOption::getProductOptions($this->context->language->id); $combinations = $this->getProductAttributeCombinations($products); // $this->context->smarty->assign('combinations', $combinations); $this->context->smarty->assign(array('category' => $this->category, 'combinations' => $combinations, 'description_short' => Tools::truncateString($this->category->description, 350), 'products' => isset($this->cat_products) && $this->cat_products ? $this->cat_products : null, 'products_options' => isset($products_options) && $products_options ? $products_options : null, 'id_category' => (int) $this->category->id, 'id_category_parent' => (int) $this->category->id_parent, 'return_category_name' => Tools::safeOutput($this->category->name), 'path' => Tools::getPath($this->category->id), 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'categorySize' => Image::getSize(ImageType::getFormatedName('category')), 'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')), 'thumbSceneSize' => Image::getSize(ImageType::getFormatedName('m_scene')), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), 'allow_oosp' => (int) Configuration::get('PS_ORDER_OUT_OF_STOCK'), 'comparator_max_item' => (int) Configuration::get('PS_COMPARATOR_MAX_ITEM'), 'suppliers' => Supplier::getSuppliers(), 'body_classes' => array($this->php_self . '-' . $this->category->id, $this->php_self . '-' . $this->category->link_rewrite))); }
public function __clone() { parent::__clone(); $original = $this->originalRecord; $original->loadSpecification(); $this->specificationInstance = clone $original->getSpecification(); $this->specificationInstance->setOwner($this); $original->loadPricing(); $this->pricingHandlerInstance = clone $original->pricingHandlerInstance; $this->pricingHandlerInstance->setProduct($this); $this->save(); // images if ($original->defaultImage->get()) { foreach ($original->getRelatedRecordSet('ProductImage', $original->getImageFilter()) as $image) { $image->_clone($this); } } // options foreach (ProductOption::getProductOptions($original) as $option) { $clonedOpt = clone $option; $clonedOpt->product->set($this); $clonedOpt->save(); } // related products $groups[] = array(); foreach ($original->getRelationships() as $relationship) { $group = $relationship->productRelationshipGroup->get(); $id = $group ? $group->getID() : null; if ($id) { $groups[$id] = clone $group; $groups[$id]->product->set($this); $groups[$id]->save(); } $cloned = ProductRelationship::getNewInstance($this, $relationship->relatedProduct->get(), $id ? $groups[$id] : null); $cloned->save(); } }
public function testClone() { $image = ActiveRecordModel::getNewInstance('ProductImage'); $image->product->set($this->product); $image->save(); $this->assertSame($image, $this->product->defaultImage->get()); $numField = SpecField::getNewInstance($this->productCategory, SpecField::DATATYPE_NUMBERS, SpecField::TYPE_NUMBERS_SIMPLE); $numField->save(); $this->product->setAttributeValue($numField, 100); $this->product->save(); $option = ProductOption::getNewInstance($this->product); $option->type->set(ProductOption::TYPE_SELECT); $option->setValueByLang('name', 'en', 'test'); $option->save(); $related = Product::getNewInstance($this->productCategory, 'related'); $related->save(); $relGroup = ProductRelationshipGroup::getNewInstance($this->product, ProductRelationship::TYPE_CROSS); $relGroup->save(); $rel = ProductRelationship::getNewInstance($this->product, $related, $relGroup); $rel->save(); $this->assertEquals(1, $this->product->getRelationships()->size()); $cloned = clone $this->product; $this->assertEquals(100, $cloned->getSpecification()->getAttribute($numField)->value->get()); $cloned->setAttributeValue($numField, 200); $cloned->setPrice($this->usd, 80); $cloned->save(); $this->assertNotEquals($cloned->getID(), $this->product->getID()); ActiveRecordModel::clearPool(); $reloaded = Product::getInstanceByID($cloned->getID(), true); $reloaded->loadPricing(); $this->assertEquals(80, $reloaded->getPrice($this->usd)); $reloaded->loadSpecification(); $this->assertEquals(200, $reloaded->getSpecification()->getAttribute($numField)->value->get()); // related products $rel = $reloaded->getRelationships(); $this->assertEquals(1, $rel->size()); $this->assertSame($reloaded, $rel->get(0)->productRelationshipGroup->get()->product->get()); // options $clonedOpts = ProductOption::getProductOptions($reloaded); $this->assertEquals(1, $clonedOpts->size()); // image $this->assertTrue(is_object($reloaded->defaultImage->get())); $this->assertNotEquals($reloaded->defaultImage->get()->getID(), $this->product->defaultImage->get()->getID()); }
public function __clone() { parent::__clone(); $original = $this->originalRecord; $original->loadSpecification(); $this->specificationInstance = clone $original->getSpecification(); $this->specificationInstance->setOwner($this); $original->loadPricing(); $this->pricingHandlerInstance = clone $original->pricingHandlerInstance; $this->pricingHandlerInstance->setProduct($this); $this->save(); // images if ($original->defaultImage->get()) { foreach ($original->getRelatedRecordSet('ProductImage', $original->getImageFilter()) as $image) { $image->_clone($this); } } // options foreach (ProductOption::getProductOptions($original) as $option) { $clonedOpt = clone $option; $clonedOpt->product->set($this); $clonedOpt->save(); } // variations $variations = $original->getRelatedRecordSet('Product'); if ($variations->size()) { $idMap = array(); foreach ($original->getRelatedRecordSet('ProductVariationType') as $type) { $newType = clone $type; $newType->product->set($this); $newType->save(); foreach ($type->getRelatedRecordSet('ProductVariation') as $var) { $newVar = clone $var; $newVar->type->set($newType); $newVar->save(); $idMap[$var->getID()] = $newVar->getID(); } } foreach ($variations as $variation) { $newVariation = clone $variation; $newVariation->parent->set($this); $newVariation->save(); foreach ($variation->getRelatedRecordSet('ProductVariationValue') as $value) { $newValue = clone $value; $newValue->product->set($newVariation); $newValue->variation->set(ActiveRecordModel::getInstanceByID('ProductVariation', $idMap[$value->variation->get()->getID()], true)); $newValue->save(); } } } // additional categories foreach ($original->getRelatedRecordSet('ProductCategory') as $additionalCat) { $newCat = clone $additionalCat; $newCat->product->set($this); $newCat->save(); } // related products $groups[] = array(); foreach ($original->getRelationships() as $relationship) { $group = $relationship->productRelationshipGroup->get(); $id = $group ? $group->getID() : null; if ($id) { $groups[$id] = clone $group; $groups[$id]->product->set($this); $groups[$id]->save(); } $cloned = ProductRelationship::getNewInstance($this, $relationship->relatedProduct->get(), $id ? $groups[$id] : null); $cloned->save(); } }
/** * @param Product $product * @throws Exception * @throws SmartyException */ public function initFormAttributes($product) { $data = $this->createTemplate($this->tpl_form); $options = ProductOption::getProductOptions($this->context->language->id); foreach ($options as &$option) { if (isset($product->id) && !empty($product->id)) { $option['selected'] = Db::getInstance()->getValue("SELECT id_product_option AS `selected` FROM " . _DB_PREFIX_ . "product_option_product\n WHERE id_product={$product->id} AND id_product_option={$option['id_product_option']}"); $option['amount'] = Db::getInstance()->getValue("SELECT amount AS `amount` FROM " . _DB_PREFIX_ . "product_option_product\n WHERE id_product={$product->id} AND id_product_option={$option['id_product_option']}"); } else { $option['selected'] = null; } } $data->assign('options', $options); if (!Combination::isFeatureActive()) { $this->displayWarning($this->l('This feature has been disabled. ') . ' <a href="index.php?tab=AdminPerformance&token=' . Tools::getAdminTokenLite('AdminPerformance') . '#featuresDetachables">' . $this->l('Performances') . '</a>'); } elseif (Validate::isLoadedObject($product)) { if ($this->product_exists_in_shop) { if ($product->is_virtual) { $data->assign('product', $product); $this->displayWarning($this->l('A virtual product cannot have combinations.')); } else { $attribute_js = array(); $attributes = Attribute::getAttributes($this->context->language->id, true); foreach ($attributes as $k => $attribute) { $attribute_js[$attribute['id_attribute_group']][$attribute['id_attribute']] = $attribute['name']; natsort($attribute_js[$attribute['id_attribute_group']]); } $currency = $this->context->currency; $data->assign('attributeJs', $attribute_js); $data->assign('attributes_groups', AttributeGroup::getAttributesGroups($this->context->language->id)); $data->assign('currency', $currency); $images = Image::getImages($this->context->language->id, $product->id); $data->assign('tax_exclude_option', Tax::excludeTaxeOption()); $data->assign('ps_weight_unit', Configuration::get('PS_WEIGHT_UNIT')); $data->assign('ps_use_ecotax', Configuration::get('PS_USE_ECOTAX')); $data->assign('field_value_unity', $this->getFieldValue($product, 'unity')); $data->assign('reasons', $reasons = StockMvtReason::getStockMvtReasons($this->context->language->id)); $data->assign('ps_stock_mvt_reason_default', $ps_stock_mvt_reason_default = Configuration::get('PS_STOCK_MVT_REASON_DEFAULT')); $data->assign('minimal_quantity', $this->getFieldValue($product, 'minimal_quantity') ? $this->getFieldValue($product, 'minimal_quantity') : 1); $data->assign('available_date', $this->getFieldValue($product, 'available_date') != 0 ? stripslashes(htmlentities($this->getFieldValue($product, 'available_date'), $this->context->language->id)) : '0000-00-00'); $i = 0; $type = ImageType::getByNameNType('%', 'products', 'height'); if (isset($type['name'])) { $data->assign('imageType', $type['name']); } else { $data->assign('imageType', ImageType::getFormatedName('small')); } $data->assign('imageWidth', (isset($image_type['width']) ? (int) $image_type['width'] : 64) + 25); foreach ($images as $k => $image) { $images[$k]['obj'] = new Image($image['id_image']); ++$i; } $data->assign('images', $images); $data->assign($this->tpl_form_vars); $data->assign(array('list' => $this->renderListAttributes($product, $currency), 'product' => $product, 'id_category' => $product->getDefaultCategory(), 'token_generator' => Tools::getAdminTokenLite('AdminAttributeGenerator'), 'combination_exists' => Shop::isFeatureActive() && Shop::getContextShopGroup()->share_stock && count(AttributeGroup::getAttributesGroups($this->context->language->id)) > 0 && $product->hasAttributes())); } } else { $this->displayWarning($this->l('You must save the product in this shop before adding combinations.')); } } else { $data->assign('product', $product); $this->displayWarning($this->l('You must save this product before adding combinations.')); } $this->tpl_form_vars['custom_form'] = $data->fetch(); }
/** * Assign template vars related to page content * @see FrontController::initContent() */ public function initContent() { parent::initContent(); if (!$this->errors) { if (Pack::isPack((int) $this->product->id) && !Pack::isInStock((int) $this->product->id)) { $this->product->quantity = 0; } $this->product->description = $this->transformDescriptionWithImg($this->product->description); // Assign to the template the id of the virtual product. "0" if the product is not downloadable. $this->context->smarty->assign('virtual', ProductDownload::getIdFromIdProduct((int) $this->product->id)); $this->context->smarty->assign('customizationFormTarget', Tools::safeOutput(urldecode($_SERVER['REQUEST_URI']))); if (Tools::isSubmit('submitCustomizedDatas')) { // If cart has not been saved, we need to do it so that customization fields can have an id_cart // We check that the cookie exists first to avoid ghost carts if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()])) { $this->context->cart->add(); $this->context->cookie->id_cart = (int) $this->context->cart->id; } $this->pictureUpload(); $this->textRecord(); $this->formTargetFormat(); } elseif (Tools::getIsset('deletePicture') && !$this->context->cart->deleteCustomizationToProduct($this->product->id, Tools::getValue('deletePicture'))) { $this->errors[] = Tools::displayError('An error occurred while deleting the selected picture.'); } $pictures = array(); $text_fields = array(); if ($this->product->customizable) { $files = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_FILE, true); foreach ($files as $file) { $pictures['pictures_' . $this->product->id . '_' . $file['index']] = $file['value']; } $texts = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_TEXTFIELD, true); foreach ($texts as $text_field) { $text_fields['textFields_' . $this->product->id . '_' . $text_field['index']] = str_replace('<br />', "\n", $text_field['value']); } } $this->context->smarty->assign(array('pictures' => $pictures, 'textFields' => $text_fields)); $this->product->customization_required = false; $customization_fields = $this->product->customizable ? $this->product->getCustomizationFields($this->context->language->id) : false; if (is_array($customization_fields)) { foreach ($customization_fields as $customization_field) { if ($this->product->customization_required = $customization_field['required']) { break; } } } // Assign template vars related to the category + execute hooks related to the category $this->assignCategory(); // Assign template vars related to the price and tax $this->assignPriceAndTax(); // Assign template vars related to the images $this->assignImages(); // Assign attribute groups to the template $this->assignAttributesGroups(); // Assign attributes combinations to the template $this->assignAttributesCombinations(); // Pack management $pack_items = Pack::isPack($this->product->id) ? Pack::getItemTable($this->product->id, $this->context->language->id, true) : array(); $this->context->smarty->assign('packItems', $pack_items); $this->context->smarty->assign('packs', Pack::getPacksTable($this->product->id, $this->context->language->id, true, 1)); if (isset($this->category->id) && $this->category->id) { $return_link = Tools::safeOutput($this->context->link->getCategoryLink($this->category)); } else { $return_link = 'javascript: history.back();'; } $product_options = ProductOption::getProductOptions($this->context->language->id); foreach ($product_options as &$product_option) { if (isset($this->product->id) && !empty($this->product->id)) { $product_option['selected'] = Db::getInstance()->getValue("SELECT id_product_option AS `selected` FROM " . _DB_PREFIX_ . "product_option_product\n WHERE id_product={$this->product->id} AND id_product_option={$product_option['id_product_option']}"); $product_option['amount'] = Db::getInstance()->getValue("SELECT amount AS `amount` FROM " . _DB_PREFIX_ . "product_option_product\n WHERE id_product={$this->product->id} AND id_product_option={$product_option['id_product_option']}"); } else { $product_option['selected'] = null; } } $accessories = $this->product->getAccessories($this->context->language->id); if ($this->product->cache_is_pack || count($accessories)) { $this->context->controller->addCSS(_THEME_CSS_DIR_ . 'product_list.css'); } if ($this->product->customizable) { $customization_datas = $this->context->cart->getProductCustomization($this->product->id, null, true); } $this->context->smarty->assign(array('product_options' => count($product_options) ? $product_options : null, 'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'), 'customizationFields' => $customization_fields, 'id_customization' => empty($customization_datas) ? null : $customization_datas[0]['id_customization'], 'accessories' => $accessories, 'return_link' => $return_link, 'product' => $this->product, 'product_manufacturer' => new Manufacturer((int) $this->product->id_manufacturer, $this->context->language->id), 'token' => Tools::getToken(false), 'features' => $this->product->getFrontFeatures($this->context->language->id), 'attachments' => $this->product->cache_has_attachments ? $this->product->getAttachments($this->context->language->id) : array(), 'allow_oosp' => $this->product->isAvailableWhenOutOfStock((int) $this->product->out_of_stock), 'last_qties' => (int) Configuration::get('PS_LAST_QTIES'), 'HOOK_EXTRA_LEFT' => Hook::exec('displayLeftColumnProduct'), 'HOOK_EXTRA_RIGHT' => Hook::exec('displayRightColumnProduct'), 'HOOK_PRODUCT_OOS' => Hook::exec('actionProductOutOfStock', array('product' => $this->product)), 'HOOK_PRODUCT_ACTIONS' => Hook::exec('displayProductButtons', array('product' => $this->product)), 'HOOK_PRODUCT_TAB' => Hook::exec('displayProductTab', array('product' => $this->product)), 'HOOK_PRODUCT_TAB_CONTENT' => Hook::exec('displayProductTabContent', array('product' => $this->product)), 'HOOK_PRODUCT_CONTENT' => Hook::exec('displayProductContent', array('product' => $this->product)), 'display_qties' => (int) Configuration::get('PS_DISPLAY_QTIES'), 'display_ht' => !Tax::excludeTaxeOption(), 'jqZoomEnabled' => Configuration::get('PS_DISPLAY_JQZOOM'), 'ENT_NOQUOTES' => ENT_NOQUOTES, 'outOfStockAllowed' => (int) Configuration::get('PS_ORDER_OUT_OF_STOCK'), 'errors' => $this->errors, 'body_classes' => array($this->php_self . '-' . $this->product->id, $this->php_self . '-' . $this->product->link_rewrite, 'category-' . (isset($this->category) ? $this->category->id : ''), 'category-' . (isset($this->category) ? $this->category->getFieldByLang('link_rewrite') : '')), 'display_discount_price' => Configuration::get('PS_DISPLAY_DISCOUNT_PRICE'))); } $this->setTemplate(_PS_THEME_DIR_ . 'product.tpl'); }