简体   繁体   English

Magento在产品详细信息页面加载中运行自定义脚本

[英]Magento run custom script on product details page load

I'm pretty new to Magento custom modules and just want to be pointed in the right direction to read up on things. 我对Magento定制模块还很陌生,只想指出正确的方向以继续学习。

I'm creating a few websites that I want to have stock dynamically updated from an API (that I'm developing). 我正在创建一些网站,希望通过API(我正在开发)动态更新库存。

Currently I have a script that runs every day to update every product with the current stock count, and another script that gets the difference since the last update and does it (every 10minutes). 目前,我有一个脚本每天运行一次,以使用当前库存数量来更新每个产品,另一个脚本是自上次更新以来获取差异并执行(每10分钟一次)。

I don't like it as there is still room for error and it just doesn't sit right with me. 我不喜欢它,因为仍有错误的余地,而且它并不适合我。 What I would like is as you click on the product, it makes an API call, updates my custom field, and renders the page. 我想要的是单击产品时,它会调用API,更新我的自定义字段并呈现页面。 I also have a custom stock status plugin so I really need it to do the call before page load. 我也有一个自定义的库存状态插件,因此我真的需要它才能在页面加载之前进行调用。 . I can write some logic on timeout to render the page anyway if there is a problem with the API. 如果API有问题,我可以在超时时编写一些逻辑以渲染页面。

Any pointers would be really helpful. 任何指针都会非常有用。

You can use the Event/Observer feature to call the API. 您可以使用事件/观察器功能来调用API。 Try one of the following events:- 尝试以下事件之一:

catalog_controller_product_init_before
catalog_controller_product_init_after
catalog_controller_product_view

Got it working... Thanks for your help, there is still some work to do, but its calling the script at the correct time! 运行正常...感谢您的帮助,还有很多工作要做,但是它会在正确的时间调用脚本! Thankyou 谢谢

Events.xml Events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_controller_product_init_after">
        <observer name="Apicall" instance="Olisco\Tpcconnector\Observer\ApiCall" />
    </event>
</config>


<?php
namespace Olisco\Tpcconnector\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class ApiCall implements ObserverInterface
{

    public function execute(EventObserver $observer)
    {
      #Code here to execute!
    }
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM