0

I created a module in Magento 2 to connect to a third-party API (which I created). The problem is that it's not connecting and giving me a 404 error in the view path. local-project.test/getprovider/index/offer

app/code/Vendor/Module/view/frontend/layout/offer_index_index.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">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Offer" name="vendor.module.offer" template="Vendor_Module::offer.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/Vendor/Module/Controller/Index/OfferController.php

<?php

namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;

class OfferController extends Action
{
    protected $resultJsonFactory;

    public function __construct(
        Context $context,
        JsonFactory $resultJsonFactory
    ) {
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        // die("hola");
        $sku = $this->getRequest()->getParam('sku');

        // Aquí realiza la llamada a la API del proveedor utilizando la librería cURL

        // Llamada a la API utilizando cURL
        $providerUrl = 'http://127.0.0.1:8000/';
        // $apiUrl = $providerUrl . '/getAllSkuOffers/' . $sku;
        $apiUrl = $providerUrl . '/getAllSkuOffers/SKU1';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $apiUrl);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        // Procesa la respuesta de la API y prepara los datos para ser devueltos

        $result = $this->resultJsonFactory->create();
        return $result->setData(['response' => $response]);
    }
}

app/code/Vendor/Module/view/frontend/templates/offer.phtml

<div class="offer-container">
    <h2>Ofertas disponibles</h2>
    <?php if ($offers && count($offers) > 0) : ?>
        <ul>
            <?php foreach ($offers as $offer) : ?>
                <li>
                    <h3>ID: <?php echo $offer['id']; ?></h3>
                    <p>Precio: <?php echo $offer['price']; ?></p>
                    <p>Stock: <?php echo $offer['stock']; ?></p>
                    <p>Precio de envío: <?php echo $offer['shipping_price']; ?></p>
                    <p>Fecha de entrega: <?php echo $offer['delivery_date']; ?></p>
                    <p>Puede ser devuelto: <?php echo $offer['can_be_refunded'] ? 'Sí' : 'No'; ?></p>
                    <p>Estado: <?php echo $offer['status']; ?></p>
                    <p>Garantía: <?php echo $offer['guarantee'] ? 'Sí' : 'No'; ?></p>
                    <p>Vendedor: <?php echo $offer['seller']['name']; ?></p>
                    <p>Calificación del vendedor: <?php echo $offer['seller']['qualification']; ?></p>
                    <p>Cantidad de calificaciones: <?php echo $offer['seller']['reviews_quantity']; ?></p>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php else : ?>
        <p>No hay ofertas disponibles en este momento.</p>
    <?php endif; ?>
</div>
2
  • Where you are getting 404 error to access your controllers? Can you please elaborate more?
    – Ankit
    Jun 17, 2023 at 8:28
  • OK, I've modified the question. It's about the route /getprovider/index/offer. @Ankit Jun 17, 2023 at 14:27

2 Answers 2

1

@Arturo You are gettting 404 error because controller is not getting called, because your xml file named wrong it should be getprovider_index_offercontroller.xml Please refer: https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html#step-2-create-layout-file-xml

The how you can connect to 3rd party is looks right to me. Please share screesnshot if its still not working with URL you are hitting on browser.

0

I solved it by renaming the layout.

getprovider_index_index.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">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Offer" name="vendor.module.offer" template="Vendor_Module::offer.phtml" />
        </referenceContainer>
    </body> </page>

I changed the name of the controller. app/code/Vendor/Module/Controller/Index/Index.php

Thank you very much

1
  • Happy to help!!
    – Ankit
    Jul 18, 2023 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.