Добавить переменные в документ

Хочу расширить список переменных для документа “Упаковочный лист”, а именно добавить отметку о бесплатной доставке конкретного товара.
Понимаю что нужно расширять класс ProductVariable (app/Tygh/Template/Snippet/Table/ProductVariable.php), но не до конца понимаю как.
Подскажите куда копнуть, чтобы не менять в ядре ничего

Если разберетесь - скиньте инструкцию.
Пытался найти как добавить туда конкретную Характеристику, но не удалось…

Как пожелаете)

Сделал в два файла, возможно не совсем правильно, но всё работает как и ожидал.

app/addons/my_changes/schemas/snippets/packing_slip_products_table.post.php

<?php
/**
 * Created by PhpStorm.
 * User: Yaroslav
 * Email: yarik.lev7@gmail.com
 * Date: 21.02.2019
 * Time: 14:35
 */


return array(
    'product' => array(
        'class' => '\Tygh\Template\Snippet\Table\ProductVariableExtended',
        'arguments' => array('#context', '#config', '@view', '@formatter'),
        'alias' => 'p',
        'null_display' => '-'
    )
);

ProductVariableExtended - Имя класса, который расширяет набор переменных.

app/addons/my_changes/Tygh/Template/Snippet/Table/ProductVariableExtended.php

<?php
/**
 * Created by PhpStorm.
 * User: Yaroslav
 * Email: yarik.lev7@gmail.com
 * Date: 07.03.2019
 * Time: 17:12
 */
namespace Tygh\Template\Snippet\Table;

use Tygh\SmartyEngine\Core;
use Tygh\Tools\Formatter;

class ProductVariableExtended extends ProductVariable
{
    public $free_shipping;

    public function __construct(ItemContext $context, array $config, Core $view, Formatter $formatter)
{
    parent::__construct($context, $config, $view, $formatter);
    $this->free_shipping=$this->product['free_shipping'];
}
public static function attributes()
{
    $temp = parent::attributes(); // TODO: Change the autogenerated stub
    array_push($temp,'free_shipping');
    return $temp;
}
}

Собственно описание самого класса.

За подробностями обращайтесь, коль будет не понятно

3 лайка

Вот упрощенный пример добавления веса товара к сниппету “Таблица товаров” (products_table)

app/addons/my_changes/schemas/snippets/order_products_table.post.php

<?php

$schema['product_extended'] = array(
    'class' => '\Tygh\Template\Document\Variables\GenericVariable',
    'data' => function (\Tygh\Template\Snippet\Table\ItemContext $context) {

        $product = $context->getItem();

        $product_extended['weight'] = db_get_field('SELECT weight FROM ?:products WHERE product_id =?i', $product['product_id']);
        $product_extended['weight'] = round($data['weight'],1). ' кг';

        return $product_extended;
    },
    'arguments' => array('#context', '#config', '@formatter'),
    'attributes' => array(
        'weight'
    )
);

return $schema;
5 лайков

Не знал раньше что можно схемы использовать для этого, спасибо