Hi;
X-cart 5.3.2.7
try my first module... unable to get it working..
i try this :
http://devs.x-cart.com/en/basics/adding_new_property_to_a_product/adding_product_property_via_formmodel_api.html
I made a simple module, activate it.
I made my folders, with code in it.
i want to store my purchaseprice in database of my products (example '4.99').
Problem:
http://i63.tinypic.com/2dqkar6.jpg
Every value i try tot put in gives a warning 'Test field must be unique'.
A string or number of characters are not stored in database when updating my productdetails.
What is going wrong? Thanks for helps
Main.php
Code:
<?php
namespace XLite\Module\Yves\PurchasePrice;
abstract class Main extends \XLite\Module\AModule
{
/**
* Author name
*
* @return string
*/
public static function getAuthorName()
{
return 'Yves';
}
/**
* Module name
*
* @return string
*/
public static function getModuleName()
{
return 'PurchasePrice';
}
/**
* Get module major version
*
* @return string
*/
public static function getMajorVersion()
{
return '5.3';
}
/**
* Module version
*
* @return string
*/
public static function getMinorVersion()
{
return 0;
}
/**
* Module description
*
* @return string
*/
public static function getDescription()
{
return 'Purchae Price Define';
}
}
Decorate the \XLite\Model\Product class We are creating the <X-Cart>/classes/XLite/Module/XCExample/NewProductFieldDemo/Model/Product.php file with the following content:
Code:
<?php
namespace XLite\Module\Yves\PurchasePrice\Model;
use XLite\Core\Cache\ExecuteCachedTrait;
use XLite\Core\Model\EntityVersion\EntityVersionInterface;
use XLite\Core\Model\EntityVersion\EntityVersionTrait;
use XLite\Model\Product\ProductStockAvailabilityPolicy;
class Product extends \XLite\Model\Product implements \XLite\Base\IDecorator
{
/**
* @Column (type="string", length=32)
*/
protected $testField;
/**
* Public getter
* @return string
*/
public function getTestField() {
return $this->testField;
}
/**
* Public setter
* @param string $value
*/
public function setTestField($value) {
return $this->testField;
}
}
To be able to specify value of this property on product details page in admin area, we have to decorate \XLite\View\FormModel\Product\Info class and create <X-Cart>/classes/XLite/Module/XCExample/NewProductFieldDemo/View/FormModel/Product/Info.php file with the following content:
Code:
<?php
namespace XLite\Module\Yves\PurchasePrice\View\FormModel\Product;
class Info extends \XLite\View\FormModel\Product\Info implements \XLite\Base\IDecorator
{
/**
* @return array
*/
protected function defineFields()
{
$schema = parent::defineFields();
$schema[self::SECTION_DEFAULT]['testField'] = [
'label' => static::t('Test field'),
'position' => 100,
];
return $schema;
}
}
The following code will decorate the class \XLite\Model\DTO\Product\Info (which is responsible for transfering data from product editing page):
**i made a file : XLite\Module\Yves\PurchasePrice\Model\DTO\Product\ Info.php
Code:
<?php
namespace XLite\Module\Yves\PurchasePrice\Model\DTO\Product;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use XLite\Core\Translation;
use XLite\Model\DTO\Base\CommonCell;
/**
* Product
*/
class Info extends \XLite\Model\DTO\Product\Info implements \XLite\Base\IDecorator
{
/**
* Method to initialize default data
*
* @param mixed|\XLite\Model\Product $object
*/
protected function init($object)
{
parent::init($object);
// Here you have all sections data available through $this->default, $this->shipping, $this->marketing etc.
$this->default->testField = $object->getTestField();
}
/**
* Method which transfers data from DTO to given object (product entity, in our case)
*
* @param \XLite\Model\Product $object
* @param array|null $rawData
*
* @return mixed
*/
public function populateTo($object, $rawData = null)
{
parent::populateTo($object, $rawData);
// The same here, $this->default, $this->shipping etc. vars contain the state of the field sections.
$object->setTestField($this->default->testField);
}
/**
* @param Info $dto
* @param ExecutionContextInterface $context
*/
public static function validate($dto, ExecutionContextInterface $context)
{
parent::validate($dto, $context);
if (!empty($dto->default->testField) && static::isTestFieldUnique($dto)) {
static::addViolation($context, 'default.testField', Translation::lbl('Test field must be unique'));
}
}
protected static function isTestFieldUnique($dto)
{
$testField = $dto->default->testField;
$identity = $dto->default->identity; // Entity identifier, e.g. product ID
$entity = \XLite\Core\Database::getRepo('XLite\Model\Product')->findOneByTestField($testField);
return !$entity || (int) $entity->getProductId() === (int) $identity;
}
}