Extending order item with custom field, class doesn´t load

Questions around the TYPO3 integration and plugins
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Extending order item with custom field, class doesn´t load

Post by boettner » 18 Mar 2019, 12:35

Hi all,

I´m trying to extend mshop_order_base with a custom field for a customer order reference number. I followed the instructions in the manual:
https://aimeos.org/docs/Developers/Libr ... gers_items

So far the DB table gets extended via setup but my class doesn´t seem to get loaded. I am in a TYPO3 9.5.5 environment running Aimeos 18.10.7. typo3conf/autoload includes my class but calling a template in FE with a method call to my property results in

Code: Select all

Call to undefined method Aimeos\MShop\Order\Item\Base\Standard::getCustref()
This is my class

Code: Select all

<?php

namespace Aimeos\MShop\Order\Item\Base;


use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

class Leorder extends Standard {
	protected $levalues;


	/**
	 * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00)
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency
	 * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID
	 * @param array $products List of ordered products implementing \Aimeos\MShop\Order\Item\Base\Product\Iface
	 * @param array $addresses List of order addresses implementing \Aimeos\MShop\Order\Item\Base\Address\Iface
	 * @param array $services List of order services implementing \Aimeos\MShop\Order\Item\Base\Service\Iface
	 * @param array $coupons Associative list of coupon codes as keys and ordered products implementing \Aimeos\MShop\Order\Item\Base\Product\Iface as values
	 */
	public function __construct(
		\Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
		array $values = [], array $products = [], array $addresses = [],
		array $services = [], array $coupons = []
	) {
		parent::__construct($price, $locale, $values, $products, $addresses, $services, $coupons);
		$this->levalues = $values;
	}

	public function getCustref() {
		if (isset($this->levalues['order.base.custref'])) {
			return (string) $this->levalues['order.base.custref'];
		}

		return '';
	}

	public function setCustref($val) {
		if ((string) $val !== $this->getCustref()) {
			$this->levalues['order.base.custref'] = (string) $val;
			$this->setModified();
		}

		return $this;
	}


	public function fromArray(array $list) {
		$unknown = [];
		$list    = parent::fromArray($list);

		foreach ($list as $key => $value) {
			switch ($key) {
				case 'order.base.custref':
					$this->setCustref($value);
					break;

				default:
					$unknown[ $key ] = $value;
			}
		}

		return $unknown;
	}

	public function toArray($private = false) {
		$list = parent::toArray($private);

		if ($private === true) {
			$list['order.base.custref'] = $this->getCustref();
		}

		return $list;
	}
}
Cheers
Robert.

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Extending order item with custom field, class doesn´t lo

Post by aimeos » 19 Mar 2019, 09:35

You have to extend the order manager too which must create your new item class in the "createItemBase()" method.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Extending order item with custom field, class doesn´t lo

Post by boettner » 19 Mar 2019, 10:45

To extend the manager and create my custom item base I need a \Aimeos\MShop\Price\Item\Iface for initialization:

Code: Select all

public function __construct(
		\Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale,
		array $values = [], array $products = [], array $addresses = [],
		array $services = [], array $coupons = []
	) {
		parent::__construct($price, $locale, $values, $products, $addresses, $services, $coupons);
		$this->levalues = $values;
	}
I have no idea on how to create one. new \Aimeos\MShop\Price\Item\Standard() ist definitely the wrong way, I guess?

This is my extended order manager:

Code: Select all

<?php

/**
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
 * @copyright Metaways Infosystems GmbH, 2011
 * @copyright Aimeos (aimeos.org), 2015-2018
 * @package MShop
 * @subpackage Order
 */


namespace Aimeos\MShop\Order\Manager;


use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

/**
 * Default order manager implementation.
 *
 * @package MShop
 * @subpackage Order
 */
class Leorder
	extends Standard {

	/**
	 * Creates the manager that will use the given context object.
	 *
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
	 */
	public function __construct(\Aimeos\MShop\Context\Item\Iface $context) {
		parent::__construct($context);
	}

	/**
	 * Creates a new order item.
	 *
	 * @param array $values List of attributes for order item
	 *
	 * @return \Aimeos\MShop\Order\Item\Base\Leorder New order item
	 */
	protected function createItemBase(array $values = []) {
		$locale = $this->getContext()->getLocale();

		return new \Aimeos\MShop\Order\Item\Base\Leorder($values, $locale);
	}
}

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Extending order item with custom field, class doesn´t lo

Post by boettner » 19 Mar 2019, 15:15

Hm, I can´t seem to get working.

As I read here: https://aimeos.org/docs/Developers/Libr ... gers_items, there´s definitely more to it but the information in the docs still leave questions open.

Is there maybe a code example somewhere covering all aspects, including how to code the saveItem() method and how to form the sql for config/mshop.php?

These parts are mentioned in the docs but I currently lack depth of Aimeos knowledge on how to set it up porperly.

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Extending order item with custom field, class doesn´t lo

Post by aimeos » 20 Mar 2019, 10:43

Extending the saveItem() method is only copy&paste, adding your new column and renumbering the following columns. The SQL config for INSERT, UPDATE and SELECT has to be extended by your new column as well.

Others have done that in the past too. Maybe their posts help you to find the missing link:
https://www.google.com/search?q=site%3A ... +base+item
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Extending order item with custom field, class doesn´t lo

Post by boettner » 21 Mar 2019, 15:33

Halfway there. I am able to see the new field in the account history when adding values directly in the database.

But my baskets don´t include the new field. Is there anything special when extending the order item?

Here´s a Gist of my current implementation. I had to alter the filenames Leorder.php since they must be unique in a Gist. They are LeshopItem.php and LeshopManager.php for that reason.

https://gist.github.com/boettner-it/081 ... 24907fe91b

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Extending order item with custom field, class doesn´t lo

Post by aimeos » 25 Mar 2019, 11:16

You need to add your new field in the SQL statements before the "siteid" column and you must add it to the GROUP BY clause of the SELECT statement. Everything else seems fine at the first glimpse.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Extending order item with custom field, class doesn´t lo

Post by boettner » 26 Mar 2019, 12:39

Besides the field order in SQL a Decorator was necessary to actually save the value to the object. I updated the Gist with all final changes:
https://gist.github.com/boettner-it/081 ... 24907fe91b

Post Reply