【问题标题】:Magento2 hide custom shipping method based on product attributeMagento2 隐藏基于产品属性的自定义运输方式
【发布时间】:2022-06-18 10:13:16
【问题描述】:

我是 Magento2 的新手。

我以编程方式创建了一种新的发货方式,并将其命名为“当日发货”。 另外,我以编程方式创建了一个产品属性,并将其命名为“可当天送达:(是/否)。

我想在结帐页面上添加限制 - 如果购物车中的所有商品都有(当日送达为是)。然后显示新的货件选项或将其隐藏在货件选择部分。

这是我的源代码: https://github.com/balajimrv/app

【问题讨论】:

  • 你需要有你的代码等来回答你的问题,请不要链接到第三方网站。

标签: php magento2 magento-2.0


【解决方案1】:

在方法Balaji\CustomShipping\Model\Carrier\Customshipping::collectRates启用验证后添加

if (!$this->getConfigFlag('active')) {
    return false;
}

此代码检查是否可以当天送达

foreach ($request->getAllItems() as $item) {
    if (!$item->getProduct()->getData('same_day_delivery_available')) { //use here the real attribute code you have on the product
        return false;
    }
}

在结帐时加载部分产品数据时,您还需要告诉 magento 加载您的属性。 为此,使用此内容创建文件etc/catalog_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">    
    <group name="quote_item">
        <attribute name="same_day_delivery_available"/><!-- Use here the real attribute code -->
    </group>
    
</config>

【讨论】:

  • 感谢您发布 Marius,我会检查并通知您。再次感谢。
【解决方案2】:

我使用了您的代码...不工作,这可能是我的错,但我对 Magento2 很陌生...请帮助我...请帮助...不知道错误发生在哪里... .

https://github.com/balajimrv/magentotest1

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2018-09-08
    • 2016-03-16
    • 1970-01-01
    • 2016-06-01
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多