There is the task to create a custom product attribute that controls the display of a message below the product title on the cart page, in order to identify products that might be delivered late.
The new EAV attribute is_delayed has been created as a boolean and is working correctly in the admin panel and product page.
What would be the next implementation to allow the is_delayed EAV attribute to be used in the .phtml cart page such as $block->getProduct()->getIsDelayed()?
A)
Create a new file etc/catalog_attributes.xmi:
B)
Create a new file etc/extension attributes.xmi:
C)
Create a new file etc/eav attributes.xmi:
Correct Answer:
A
To allow theis_delayedEAV attribute to be used in the .phtml cart page, the developer needs to create a new file calledetc/catalog_attributes.xmi. This file will contain the definition of theis_delayedattribute.
The following code shows how to create theetc/catalog_attributes.xmifile: XML
<?xml version="1.0"?>
<catalog_attributes>
<attribute code="is_delayed" type="int">
<label>Is Delayed</label>
<note>This attribute indicates whether the product is delayed.</note>
<sort_order>10</sort_order>
<required>false</required>
</attribute>
</catalog_attributes>
Once theetc/catalog_attributes.xmifile has been created, theis_delayedattribute will be available in the .phtml cart page. The attribute can be accessed using thegetIsDelayed()method of theProductclass.
PHP
$product = $block->getProduct();
$isDelayed = $product->getIsDelayed();
TheisDelayedvariable will contain the value of theis_delayedattribute. If the value of the attribute is 1, then the product is delayed. If the value of the attribute is 0, then the product is not delayed.
An Adobe Commerce developer is being tasked with creating a new cron job to run a method that has already been written. What are the minimally required steps to accomplish this?
Correct Answer:
C
According to the Configure and run cron guide for Magento 2 developers, the crontab.xmi file is used to declare and configure cron jobs for a module. The file should specify the name, instance, method and schedule of the cron job. Therefore, creating a crontab.xmi file and setting a schedule for the new cron job are the minimally required steps to accomplish this task. Verified References:https://devdocs.magento.com/guides/v2.3/config-guide/cli/config-cli- subcommands-cron.html
To set up a new cron job in Adobe Commerce, you need to define it in the crontab.xml file. This file is essential to schedule cron tasks and is all that's required for simple cron job configurations. You specify the cron job schedule, method, and class in this file.
Here's a minimal example of crontab.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="your_custom_cron_job" instance="Vendor\Module\Cron\YourJob" method="execute">
<schedule>0 * * * *</schedule> <!-- Runs every hour -->
</job>
</group>
</config>
✑ group id: This specifies the cron group; "default" is commonly used, but you can define custom groups in cron_groups.xml if necessary.
✑ job: Defines the cron job, with name, instance (path to the class), and method
attributes.
✑ schedule: Uses standard cron expressions to specify the frequency.
Additional Resources:
✑ Adobe Commerce Developer Guide: Cron Jobs
✑ Magento 2 Crontab XML Configuration
An Adobe Commerce developer has installed a module from a third-party vendor. This module fires a custom event named third_party_event_after and also defines an observer named third_party_event_after_observer that listens to that event. The developer wants to listen to this custom event in their own module but wants to execute their observer's logic after the third_party_event_after_observer observer has finished executing.
What would the developer do to ensure their observer runs after the observer defined by the third-party module?
Correct Answer:
C
https://developer.adobe.com/commerce/php/best- practices/extensions/observers/#do-not-rely-on-invocation-order
A merchant has noticed an error in the checkout. The accessed URL is /checkout.
Where can the developer find the responsible controller in the Magento.Checkout module?
Correct Answer:
C
The controller responsible for handling the /checkout URL is located in Controller/Checkout/Index.php in the Magento.Checkout module1. This controller extends from \Magento\Checkout\Controller\Index\Index, which implements the execute() method that renders the checkout page1.
In the Magento_Checkout module, the responsible controller for the /checkout URL can be found inController/Checkout/Index.php. This controller handles the index action for the checkout process, initiating the checkout page when a customer navigates to /checkout. The organization of controllers in Magento follows a convention where the URL path corresponds to the directory structure within the module, making it easier to locate and understand the functionality associated with specific routes.
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