Dynamic variables
The dynamic variable is a user-defined php code that must return a string value. The PHP code is stored on your server in your Magento installation folder. The extension fetches it from the file.
To create a new dynamic variable, follow these steps:
- Go to Catalog → Advanced Product Feeds → Dynamic Variables. Press the button Add Variable.
-
Fill in the following fields:
- Name - name of the dynamic variable
- Variable Code - code of the dynamic variable. You will see this code when you select this dynamic variable in the templates.
- Create the PHP code for the Dynamic variable:
- Login to your server.
- Navigate to the var/mst_feed/ or app/code/Mirasvit/Feed/ folder
- Create a PHP file in one of these folders named exactly as the Variable Code you have entered previously.
For example, if the Variable Code is GTIN_variable, you need to create a filevar/mst_feed/GTIN_variable.php
orapp/code/Mirasvit/Feed/GTIN_variable.php
- Paste the PHP code for your variable in the file and save it.
Note
Use the Echo function in the PHP code instead of Return to get the product data.
Note
For Magento Cloud, it is recommended to move variables from var/mst_feed
to app/code/Mirasvit/Feed
folder before deployment.
- Return to your Magento admin and reload the page or click Save button. The section Code will show the PHP code that was fetched for this varible from your server.
To see an example of the dynamic variable output you can use the field right to the Code section.
Enter product ID's separated by commas to see the data for several products.
Examples
Note
Ensure the code of your dynamic variable is enclosed in PHP opening <?php
tag:
<?php
*Your Dynamic Variable code*
-
How to get the correct GTIN
Name: GTIN
Code: gtin
PHP Code:<?php $gtin = $product->getGtin(); if (!$gtin) { $gtin = $product->getId(); $gtin .= str_repeat('0', 12 - strlen($gtin)); } echo $gtin;
Usage:
{{ product.variable:gtin }}
Output:
124200000000
-
How to add 20% to the final price
Name: Add Percentage
Code: percentage
PHP Code:
<?php $price = $product->getFinalPrice(); echo number_format($price*1.2, 2);
Usage:
{{ product.variable:percentage }}
-
How to get the qty of product from all stocks
Name: Summary stocks qty
Code: sum_stocks_qty
PHP Code:
<?php
$sourcesData = $objectManager->get('\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku')
->execute($product->getSku());
$sumQty = 0;
foreach ($sourcesData as $sourceData){
$sumQty += $sourceData['quantity'];
}
echo $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:
<?php
$finalPrice = $product->getFinalPrice();
$regularPrice = $product->getPrice();
if ($finalPrice < $regularPrice) {
echo number_format($finalPrice, 2);
} else {
echo '';
}
Usage: {{ product.variable:custom_spec_price }}