简体   繁体   English

Prestashop 在前端结帐的承运商上添加自定义单选按钮

[英]Prestashop add custom radio button on shipping carriers on front end checkout

I have created one custom module in Prestashop 1.7.8.7 which will add multiple shipping methods (Carrier) and will show shipping cost based on product dimensions and delivery address.我在 Prestashop 1.7.8.7 中创建了一个自定义模块,它将添加多种运输方式(承运人),并将根据产品尺寸和送货地址显示运输成本。 So shipping carrier will look something like this on front-end checkout page.因此,承运商在前端结账页面上看起来像这样。 https://prnt.sc/E1avDASyJYYW https://prnt.sc/E1avDASyJYYW

Now if someone select SameDay Courier Shipping then i need to show two radio button to select which service they want.现在,如果有人 select SameDay Courier Shipping那么我需要向 select 显示他们想要的服务的两个单选按钮。 It will have two radio options like它将有两个单选选项,例如

  1. Delivery pickup (by default this option will be selected)送货取件(默认情况下会选择此选项)
  2. Locker pickup储物柜取件

So if someone select Delivery pickup then it will have different shipping cost and if someone select Locker pickup then it will have different shipping cost.因此,如果有人 select Delivery pickup那么它会有不同的运费,如果有人 select Locker pickup那么它会有不同的运费。

Can anybody help me how can i achieve this functionality.任何人都可以帮助我如何实现此功能。

While digging, i found that we have file called DeliveryOptionsFinder.php and in that file we have one public function called getDeliveryOptions() where we have this line of code在挖掘时,我发现我们有一个名为DeliveryOptionsFinder.php的文件,在该文件中我们有一个名为getDeliveryOptions()的公共 function,我们有这行代码

    if ($moduleId = Module::getModuleIdByName($carrier['external_module_name'])) {
        $carrier['extraContent'] = Hook::exec('displayCarrierExtraContent', ['carrier' => $carrier], $moduleId);
    }
}

So if i set is_module to 1 to all my carriers directly from DB then on frontend checkout page, no carriers is being displayed.因此,如果我直接从 DB 将is_module设置为 1 到我的所有运营商,那么在前端结帐页面上,不会显示任何运营商。

Thanks in advance.提前致谢。

I've already done something like this in the last 2 year as prestashop developer.在过去的 2 年里,作为 prestashop 开发人员,我已经做过类似的事情。 The truth is that you can't achieve what you want by "respecting" prestashop processes.事实是,您无法通过“尊重”prestashop 流程来实现您想要的。

Maybe you can hook using a module the hookDisplayCarrierExtraContent($data) and then return the 2 radios if carrier is certain one (use $data).也许您可以使用hookDisplayCarrierExtraContent($data) ,则返回 2 个无线电。 But you can't handle a form submit or something else, or include it to prestashop checkout data.但是您无法处理表单提交或其他内容,或将其包含到 prestashop 结帐数据中。 But what you can do as workaround, for example, is the following.但是,例如,您可以采取以下措施作为解决方法。

In your module, as I said, hook the extra content, render a template with the 2 radios.正如我所说,在你的模块中,挂钩额外的内容,渲染一个带有 2 个无线电的模板。 Hook displayHeader too and use $this->context->controller->addJS() to add your own js if the current controller is the checkout one.如果当前 controller 是结帐的,也挂钩 displayHeader 并使用$this->context->controller->addJS()添加您自己的 js。

Then in this JS code you can handle the "change" event of the radios and send an ajax request to your module.然后在此 JS 代码中,您可以处理无线电的“更改”事件并向您的模块发送 ajax 请求。 You can create inside {your_module}/controllers/front/ a controller called, for example, radio-choose and handle the js ajax request by saving inside your own table the choosen one.您可以在 {your_module}/controllers/front/ 中创建一个 controller 调用,例如,通过在您自己的表中保存选择的一个来选择和处理 js ajax 请求。

Obviously you can disable the "next" button in checkout untill one of the two radios are selected, or maybe you can just set a radio button as default one to simplify.显然,您可以在结帐时禁用“下一步”按钮,直到选择了两个无线电之一,或者您可以将一个单选按钮设置为默认按钮以简化操作。

For example your table "ps_cart_choosen_radio" could look like this |id_cart|choosen_radio|.例如,您的表“ps_cart_choosen_radio”可能看起来像这样|id_cart|choosen_radio|。

Then you have all the data you needed.然后你就有了你需要的所有数据。 When a cart is converted into an order you will have inside Order object (and ps_orders table as well) the id_cart.当购物车转换为订单时,您将在订单 object(以及 ps_orders 表)中拥有 id_cart。

Just select / join choosen radio from your own table by using order's id_cart.只需 select / 使用订单的 id_cart 从您自己的表中加入选择的电台。

"SELECT choosen_radio FROM ps_cart_choosen_radio WHERE id_cart = {$order.id_cart}"

If you need to show data depending on choosen_radio in frontend you can hook everywhere an order is present and select these data.如果您需要根据前端的 choosen_radio 显示数据,您可以在存在订单的任何地方挂钩和 select 这些数据。 Or maybe you can edit carrier name in ps_orders table by adding a piece of string.或者您可以通过添加一段字符串来编辑 ps_orders 表中的运营商名称。 Let' say carrier is "express" and customer choosed "24h".假设承运人是“快递”,客户选择“24 小时”。 You can update that column with carrier name by changing it to "express-24h" so around the whole prestashop ecosystem everybody will see that's a 24h choice.您可以通过将运营商名称更改为“express-24h”来更新该列,以便在整个 prestashop 生态系统中每个人都会看到这是一个 24 小时的选择。

Remember that the carrier name related to an order is not the carrier name inside the carriers table.请记住,与订单相关的承运人名称不是承运人表中的承运人名称。 So you can edit it without having trouble.所以你可以毫不费力地编辑它。

All these problems comes from the "need" to show some nested choices instead of listing all these in the main selections.所有这些问题都来自“需要”显示一些嵌套的选择,而不是在主要选择中列出所有这些。 In that case it would be easy (just create carrier in the prestashop backoffice)在那种情况下会很容易(只需在 prestashop 后台创建运营商)

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

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