首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何获取当前选定的发货地址Id magneto 2.3.4

在Magento 2.3.4中,要获取当前选定的发货地址ID,可以通过以下步骤实现:

  1. 创建一个自定义模块(如果已经有自定义模块,请跳过此步骤)。
  2. 在自定义模块的目录下创建一个Observer文件夹,并在其中创建一个文件,命名为GetSelectedShippingAddressId.php
  3. GetSelectedShippingAddressId.php文件中,编写以下代码:
代码语言:txt
复制
<?php
namespace [Your_Custom_Module]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Quote\Model\QuoteRepository;

class GetSelectedShippingAddressId implements ObserverInterface
{
    protected $quoteRepository;

    public function __construct(
        QuoteRepository $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
    }

    public function execute(Observer $observer)
    {
        $quote = $observer->getEvent()->getQuote();
        $shippingAddressId = $quote->getShippingAddress()->getId();
        // 在这里可以使用$shippingAddressId进行后续操作,比如保存到数据库或者进行其他逻辑处理
    }
}
  1. 在自定义模块的目录下创建一个etc文件夹,并在其中创建一个文件,命名为events.xml
  2. events.xml文件中,添加以下代码:
代码语言:txt
复制
<?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="checkout_controller_onepage_save_shipping_method">
        <observer name="[your_observer_name]" instance="[Your_Custom_Module]\Observer\GetSelectedShippingAddressId" />
    </event>
</config>

请注意将[Your_Custom_Module]替换为您自定义模块的命名空间,[your_observer_name]替换为您的观察者名称。

完成上述步骤后,当客户在Magento 2.3.4的结账页面选择并保存发货地址时,execute方法中的代码将被执行,并且您可以在其中使用$shippingAddressId变量获取当前选定的发货地址ID。您可以根据需要对该ID进行后续操作,比如保存到数据库或进行其他逻辑处理。

请注意,以上代码仅适用于Magento 2.3.4版本,其他版本可能会有所不同。此外,如果您使用的是自定义主题或其他模块,可能需要根据您的具体情况进行适当的修改。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券