Skip to content

Add a product attribute to a custom button on product pages, Magento 2.4.2

I am trying to add a product attribute to a custom button on product pages.

I created a custom attribute called ebay_url. In custombutton.phtml I want to use the ebay_url product attribute to replace ‘https://www.ebay.com’.

Help appreciated!

/app/design/frontend/Solwin/freego/Magento_Catalog/layout/catalog_product_view.xml

contents:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="product.info.stock.sku" destination="product.info.price" after="product.price.final"/>
        <move element="product.info.review" destination="product.info.main" before="product.info.price"/>
        <move element="product.info.overview" destination="product.info.main" after="product.info.price"/>
        <referenceContainer name="content">
            <block class="MagentoCatalogBlockProductView" name="product.info.custombutton" template="Magento_Catalog::product/view/custombutton.phtml"/>
        </referenceContainer>
        <move element="product.info.custombutton" destination="product.info.main" after="product.info.price" />
    </body>
</page>

/app/design/frontend/Solwin/freego/Magento_Catalog/templates/product/view/custombutton.phtml

contents:

<button title="Ebay Button" class="newbtn"  onclick="location.href='https://www.ebay.com'">
    <span>Get Ebay Price</span> </button>

<style type="text/css">
.newbtn{
       display:block;
       width:175px;
       height:55px;
       background:black;
       padding:10px;
       text-align:center;
       border-radius:10px;
       color:white;
}
</style>