Dynamic variables

The dynamic variable is a user-defined php code that must return a string value.

To create a new dynamic variable, follow these steps:

  1. Go to Catalog → Advanced Product Feeds → Dynamic Variables. Press the button Add Variable.
  2. Fill in the following fields:

    • Name - name of the dynamic variable
    • Code - code of the dynamic variable. You will see this code when you select this dynamic variable in the templates.
    • PHP Code


  • How to get the correct GTIN

    Name: GTIN
    Code: gtin
    PHP Code:

    $gtin = $product->getGtin();
    if (!$gtin) {
        $gtin = $product->getId();
        $gtin .= str_repeat('0', 12 - strlen($gtin));
    return $gtin;

    {{ product.variable:gtin }}


  • How to add 20% to the final price

    Name: Add Percentage
    Code: percentage
    PHP Code:

    $price = $product->getFinalPrice();
    return number_format($price*1.2, 2);

    {{ product.variable:percentage }}

  • How to get the qty of product from all stocks

    Name: Summary stocks qty
    Code: sum_stocks_qty
    PHP Code:

$sourcesData = $objectManager->get('\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku')

$sumQty = 0;

foreach ($sourcesData as $sourceData){
    $sumQty += $sourceData['quantity'];

return $sumQty;

Usage: {{ product.variable:sum_stocks_qty }}

  • I would like to keep the Special Price field empty, unless the final price is lower then the regular price

    Name: Custom Special Price
    Code: custom_spec_price
    PHP Code:

$finalPrice = $product->getFinalPrice();
$regularPrice = $product->getPrice();  

if ($finalPrice < $regularPrice) {
    return number_format($finalPrice, 2);
} else {
    return '';

Usage: {{ product.variable:custom_spec_price }}