I added the following in view/frontend/layout/catalog_product_view.xml
:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="Vendor_Name::js/my_script.js"/>
</head>
</page>
And in view/frontend/web/js/my_script.js
:
require([
// ???
], function (
// ...
) {
// ...
});
Here I thought it would suffice to require something like 'Magento_Catalog/catalog/product'
to get the product, but it doesn’t work.
If I require 'Magento_Catalog/catalog/product'
I get an Uncaught Error: Script error
because it doesn’t exist. Magento_Catalog/js/product/view/product-info
gives me an Observable
object I don’t know how to use. Magento_Catalog/js/product/view/product-info-resolver
gives me a callback that I can use to get the product id.
What should I require
to get the current product’s informations? I’m trying to determine if the product has a boolean custom attribute set to true
.
I’m using Magento 2.4.x.
Thank you.