Nice programing

Magento 시스템 구성에서 데이터를 가져 오는 방법

nicepro 2020. 12. 2. 21:58
반응형

Magento 시스템 구성에서 데이터를 가져 오는 방법


사용자 지정 모듈에 대한 구성 데이터를 얻는 방법에 대해 배회했습니다. 구성은 관리자에서 설정할 수 있으며 system->configuration프런트 엔드에서 가져 오는 방법은 무엇입니까?


$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');

sectionName , groupNamefieldName모듈의 etc / system.xml 파일에 있습니다.

위의 코드는 현재보고있는 상점의 구성 값을 자동으로 가져옵니다.

현재보고있는 상점이 아닌 다른 상점의 구성 값을 가져 오려면 다음과 같이 getStoreConfig함수 의 두 번째 매개 변수로 상점 ID를 지정할 수 있습니다.

$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);

다음 코드를 사용해야합니다.

$configValue = Mage::getStoreConfig(
                   'sectionName/groupName/fieldName',
                   Mage::app()->getStore()
               ); 

Mage::app()->getStore() 그러면 가져 오기 값에 상점 코드가 추가되어 현재 상점에 대한 올바른 구성 값을 얻을 수 있습니다. 그러면 magento는 여러 상점 /보기에도 사용되므로 상점 코드를 추가하여 magento로 가져와야합니다.

하나 이상의 상점 또는 다중보기가 구성된 경우 현재 상점에 대한 값을 가져 오는 것을 보장합니다.


마 젠토 1.x

(아래에 제공된 마 젠토 2 예제)

sectionName , groupNamefieldName 은 모듈의 etc / system.xml 파일에 있습니다.

PHP 구문 :

Mage::getStoreConfig('sectionName/groupName/fieldName');

CMS 페이지 또는 정적 블록의 콘텐츠와 같은 관리자의 편집기 내에서 카탈로그 카테고리, 카탈로그 제품 등에 대한 설명 / 짧은 설명

{{config path="sectionName/groupName/fieldName"}}

"편집기 내"접근 방식이 작동하려면 {{...}} 내용을 구문 분석 할 필터를 통해 필드 값을 전달해야합니다. 기본적으로 Magento는 카테고리 및 제품 설명, CMS 페이지 및 정적 블록에 대해이 작업을 수행합니다. 그러나 사용자 정의보기 스크립트 내에서 콘텐츠를 출력하고 이러한 변수를 구문 분석하려면 다음과 같이 할 수 있습니다.

<?php
    $example = Mage::getModel('identifier/name')->load(1);
    $filter  = Mage::getModel('cms/template_filter');
    echo $filter->filter($example->getData('field'));
?>

교체 식별자 / 이름을 당신이 로딩되어 모델과의 적절한 값 필드 포함 할 수 있습니다 당신은 출력 할 속성의 이름이 {{...}} 발생 할 필요가 파싱 할 것이다.

Magento 2.x

\ Magento \ Framework \ View \ Element \ AbstractBlock을 확장하는 모든 Block 클래스에서

$this->_scopeConfig->getValue('sectionName/groupName/fieldName');

기타 모든 PHP 클래스 :

클래스 (부모 클래스가 아님)가 \Magento\Framework\App\Config\ScopeConfigInterface생성자를 통해 주입되지 않으면 클래스에 추가해야합니다.

// ... Remaining class definition above...

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
protected $_scopeConfig;

/**
 * Constructor
 */
public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    // ...any other injected classes the class depends on...
) {
  $this->_scopeConfig = $scopeConfig;
  // Remaining constructor logic...
}

// ...remaining class definition below...

클래스에 삽입 한 후에는 이제 블록 클래스에 대해 위에 제공된 동일한 구문 예제를 사용하여 저장소 구성 값을 가져올 수 있습니다.

클래스의 __construct () 매개 변수 목록을 수정 한 후 생성 된 클래스와 종속성 주입 디렉토리를 지워야 할 수 있습니다. var/generation &var/di


예를 들어 config-> store 이메일 주소에서 EMAIL ADDRESS를 얻으려는 경우. 주소를 원하는 상점에서 지정할 수 있습니다.

$store=Mage::app()->getStore()->getStoreId(); 
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_general/name',$store); 
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_general/email',$store);

참고 URL : https://stackoverflow.com/questions/5892476/how-to-get-data-from-magento-system-configuration

반응형