Magento 2 모든 활성 배송 방법을 얻는 방법?


10

내 마 젠토 버전은 2.1.0입니다. 모든 활성 운송 방법 목록을 얻으려면 어떻게해야합니까?

어떤 도움이라도 대단히 감사하겠습니다

답변:


10

또는 Magento \ Shipping \ Model \ Config \ Source \ All 메소드 를 사용할 수 있습니다 !


1
이것이 정답입니다!
— Milan Simek

다중 선택 필드에서 사용하는 것은 어떻습니까?
— spiil

그러나 어떻게 모든 활성 메소드를 얻기 위해 플래그를 전달할 수 있습니까? 내 UI 구성 요소에서
— Himanshu

7

keyur shah의 답변 외에도

아래 코드를 사용하여 모든 활성 배송을 받으실 수 있습니다.

protected $scopeConfig;

protected $shipconfig;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Shipping\Model\Config $shipconfig
) {
    $this->shipconfig=$shipconfig;
    $this->scopeConfig = $scopeConfig;
}

public function getShippingMethods(){

        $activeCarriers = $this->shipconfig->getActiveCarriers();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            foreach($activeCarriers as $carrierCode => $carrierModel)
            {
               $options = array();
               if( $carrierMethods = $carrierModel->getAllowedMethods() )
               {
                   foreach ($carrierMethods as $methodCode => $method)
                   {
                        $code= $carrierCode.'_'.$methodCode;
                        $options[]=array('value'=>$code,'label'=>$method);

                   }
                   $carrierTitle =$this->scopeConfig->getValue('carriers/'.$carrierCode.'/title');

               }
                $methods[]=array('value'=>$options,'label'=>$carrierTitle);
            }
        return $methods;        

    }

아래 코드를 사용하면 phtml 파일로 이동 통신사 목록을 얻을 수 있습니다. 여기 $block위에서 함수를 정의한 블록과 관련이 있습니다.

<?php $carriers = $block->getShippingMethods(); ?>
<select name="shipping"  class="control-select">
    <option value=""><?php /* @escapeNotVerified */ echo __('Please Select'); ?></option>
        <?php foreach ($carriers as $carrier): ?>
            <optgroup label="<?php /* @escapeNotVerified */ echo $carrier['label'] ?>">
                <?php foreach ($carrier['value'] as $child): ?>
                    <option value="<?php /* @escapeNotVerified */ echo $child['value'] ?>">
                    <?php /* @escapeNotVerified */ echo $child['label']; ?>
                    </option>
                <?php endforeach; ?>
            </optgroup>
        <?php endforeach; ?>
</select>

이 변경 배송 방법 라디오는 선택
— ????

3
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 $activeShipping = $objectManager->create('Magento\Shipping\Model\Config')->getActiveCarriers();

참고 : $ objectManager로 객체를 직접로드하는 것에 반대합니다. 생성자에 객체를 주입 할 수 있습니다. 나는 당신이 그것을 달성 할 수있는 방법을 예를 들었습니다. `


더 좋은 방법

protected $_shippingConfig;

public function __construct(
\Magento\Shipping\Model\Config $shippingConfig
) {
    $this->_shippingConfig=$shippingConfig
}

이제 모든 배송 방법을

$this->_shippingConfig->getActiveCarriers();

store특정 active shipping method을 원한다면 아래 에서이 메소드 accept 매개 변수를 볼 수 있듯이 $store객체를 전달할 parameter수 있습니다$store

public function getActiveCarriers($store = null)
    {
        $carriers = [];
        $config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
        foreach (array_keys($config) as $carrierCode) {
            if ($this->_scopeConfig->isSetFlag('carriers/' . $carrierCode . '/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)) {
                $carrierModel = $this->_carrierFactory->create($carrierCode, $store);
                if ($carrierModel) {
                    $carriers[$carrierCode] = $carrierModel;
                }
            }
        }
        return $carriers;
    }

Keyur 배송 방법 목록이 필요합니다. 배송 방법 객체를 제공하는 코드를 추가 한 후 지원에 대한 답변을 보내 주셔서 감사합니다.
— Prashant Valanda

나는 배송 방법 라디오를 배송 요금 (맞춤형)으로 결제 방법으로 드롭 다운 배송 방법으로 변환하는 작업을 수행하고 있습니다. .. 이렇게하는 방법
— sangan
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.