Which price type should be used if the developer wants to provide a discount for a product based on quantity, for example, being able to buy two for X amount each?
Correct Answer:A
Tier prices are used to provide discounts for products based on quantity. For example, you could set a tier price that allows customers to buy two products for X amount each.
The tier price is used when a developer wants to offer a discount based on the quantity purchased. For example, buying two or more units of a product at a reduced price per unit. Tier pricing allows setting different prices for different quantities, encouraging customers to buy more. Special price is a flat discounted price regardless of quantity, and group price is used to set special prices for specific customer groups, not for quantity-based discounts.
An Adobe Commerce Developer is tasked with creating a custom form which submits its data to a frontend controller They have decided to create an action and have implemented the MagentoFrameworkAppActionHttpPostActioninterface class, but are not seeing the data being persisted in the database, and an error message is being shown on the frontend after submission.
After debugging and ensuring that the data persistence logic is correct, what may be cause and solution to this?
Correct Answer:C
According to the Magento Stack Exchange answer, form key validation is a security feature that prevents CSRF attacks by checking if the form key in the request matches the one generated by Magento. If the developer does not include the form_key in their custom form, the validation will fail and an error will be shown. Therefore, the developer needs to add the form_key to their requests by using <?= $block->getBlockHtml (??formkey??) ?> in their template file. Verified References:https://magento.stackexchange.com/questions/95171/magento-2-form- validation
In Adobe Commerce, when handling POST requests from forms on the frontend, form key validation is enabled by default as a security measure to prevent Cross-Site Request Forgery (CSRF) attacks. This validation checks that the form submission is coming from the same origin by including a unique token (form key) in the request. If this form key is missing or incorrect, the request will fail, and an error message may be shown on the frontend.
In this scenario:
✑ Since the developer has used
\Magento\Framework\App\Action\HttpPostActionInterface, which is appropriate for handling POST requests, it??s likely that the error they encounter is due to missing form key validation.
✑ The solution is to ensure that the form includes a hidden input field for the form key. Adobe Commerce automatically adds this key in forms if you use the
\Magento\Framework\Data\Form\FormKey model to get the form key value. To implement this:
✑ Ensure the form includes the form key:
<input name="form_key" type="hidden" value="<?= $block->escapeHtml($block-
>getFormKey()) ?>" />
✑ The form key should also be included in the POST data sent to the controller. If it??s missing, Adobe Commerce will not process the request.
Additional Resources:
✑ Adobe Commerce Developer Guide: Form Key
✑ Magento 2.4 Form Key and CSRF Protection
In a new release of a module, a developer decides to rename a table that was defined in the earlier versions. Which action, if any, allows the developer to prevent data loss?
Correct Answer:C
When renaming a table in Magento, to prevent data loss, the developer must define the table and its columns mapping in thedb_schema_whitelist.jsonfile. This declarative schema approach ensures that the data migration tool knows about the changes and can migrate data from the old table to the newly named table without losing any data.
An Adobe Commerce developer is tasked with adding custom data to orders fetched from the API. While keeping best practices in mind, how would the developer achieve this?
Correct Answer:B
The developer should create an extension attribute on theMagento\Sales\Api\Data\OrderInterfaceinterface and an after plugin on theMagento\Sales\Api\OrderRepositoryInterface::get()andMagento\Sales\Api\OrderReposit oryInterface::getList()methods.
The extension attribute will store the custom data. The after plugin will be used to add the custom data to the order object when it is fetched from the API.
Here is the code for the extension attribute and after plugin: PHP
namespace MyVendor\MyModule\Api\Data;
interface OrderExtensionInterface extends \Magento\Sales\Api\Data\OrderInterface
{
/**
* Get custom data.
*
* @return string|null
*/
public function getCustomData();
/**
* Set custom data.
*
* @param string $customData
* @return $this
*/
public function setCustomData($customData);
}
namespace MyVendor\MyModule\Model;
class OrderRepository extends \Magento\Sales\Api\OrderRepositoryInterface
{
/**
* After get order.
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
*/
public function afterGetOrder($subject, $order)
{
if ($order instanceof OrderExtensionInterface) {
$order->setCustomData('This is custom data');
}
return $order;
}
/**
* After get list.
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param \Magento\Sales\Api\Data\OrderInterface[] $orders
* @return \Magento\Sales\Api\Data\OrderInterface[]
*/
public function afterGetList($subject, $orders)
{
foreach ($orders as $order) {
if ($order instanceof OrderExtensionInterface) {
$order->setCustomData('This is custom data');
}
}
return $orders;
}
}
Once the extension attribute and after plugin are created, the custom data will be added to orders fetched from the API.
What will happen if a developer fails to mention the start date in the "From" field when creating a price rule?
Correct Answer:B
If a developer fails to mention the start date in the "From" field when creating a price rule, the price rule will be saved. However, the price rule will not go into effect until the start date is added.
If a developer fails to mention the start date in the "From" field when creating a price rule in Adobe Commerce, the system will default to the current date, and the price rule will go into effect as soon as it is saved. The absence of a start date means there is no delay in the activation of the rule, and it becomes effective immediately upon saving.