Dynamic variables

Dynamic variable - its 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

Examples

  • How to get 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;

    Usage:
    {{ product.variable:gtin }}

    Output:
    124200000000


  • 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);

    Usage:
    {{ product.variable:percentage }}


  • How to get qty of product from all stocks:

    Name: Summary stocks qty
    Code: sum_stocks_qty
    PHP Code:

    $sourcesData = $objectManager->get('\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku')
                                 ->execute($product->getSku());
    $sumQty = 0;
    
    foreach ($sourcesData as $sourceData){
            $sumQty += $sourceData['quantity'];
    }
    
    return $sumQty;

    Usage: {{ product.variable:sum_stocks_qty }}


  • I would like to keep the field Special Price empty, unless if 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 }}