Magentoの変更方法、または簡単な例で基本機能を変更する方法

ストアのインターフェースを開発するとき、すべてを正しいフォームとロジックにするだけでなく、エンジンのバージョンを更新するタスクに直面したため、メインソフトウェアモジュールの編集をすぐに除外しました。 プラットフォームとして選択されたMagentoは、標準機能を独自の機能に置き換える機能など、独自の拡張機能を開発するための優れたツールを提供します。 これが私たちがやることです。

特定の例を見てみましょう。 現在、ほとんどの店舗では誰もペニーを使用していませんが、デフォルトでは-ロケールのおかげで-表示されています。 それらを削除するタスクがありました。 結局のところ、Zendライブラリのコンポーネントを編集するだけでこれを行うことができますが、第一に、この投稿の最初の文と矛盾し、第二に、簡単な方法を探していません。 :)



そのため、次のことを行う必要があります。
-フォーマットされた価格の表示を担当するクラスを再定義します。
-このクラスを含むモジュールを作成します。
-元のクラスが以前にアクティブ化された場合に再定義されたクラスが呼び出されるようにモジュールを構成します。
-システム内の新しいモジュールをアクティブにします。

指定されたすべてのパスは、Magentoがインストールされているディレクトリに相対的です。

前述したように、Magentoは独自のモジュールを開発し、基本的な機能を拡張する良い機会を提供します。 システムの作業コードの主要部分は(それが記述されているライブラリとフレームワークを除く)、 appディレクトリにあります。 中を見ると、そこに次の内容が表示されます。

アプリ
| ----- Mage.php
| -----コード
| -----デザイン
| -----など
| -----ロケール

Mage.php-システムのメインクラスハブを記述するモジュール-Mage
コード -すべてのコード
設計 -名前が示すように、設計の説明はここにあります。これは、ブロックのロジックと出力テンプレートです。 CSSスタイルの直接説明、スクリプトと写真を別の場所に持ち出した
etc-構成ファイル
ロケール -基本的な言語ファイル、つまり、出力のローカライズ。 特定のインターフェイスについては、ローカライズされた出力を、デザインサブディレクトリ内の独自のインターフェイスの説明で部分的または完全に再定義することもできます。

このタスクでは、 コードディレクトリに興味があります 。 その中には、3つの標準フォルダーがあります: core-システムのメインモジュールのコードと、最初は空で、サードパーティのモジュール(コミュニティ)または独自に開発されたモジュール( ローカル )をインストールするために設計されたコミュニティローカルのコードが含まれています。 実際、フォルダー間に違いはありませんが、便宜上、 ローカルフォルダーに独自のモジュールを追加します。

したがって、まず最初に、モジュールを含むフォルダーを作成する必要があります。 このフォルダーの名前は、開発するモジュールのパッケージの名前になります。 なぜなら 私は自分のプロジェクト用のモジュールを開発しました 。私が持っているフォルダーはプロジェクトの名前です-Cifrum

# pwd
/<Path To Magento>/app/code
# ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
# ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


次に、必要な機能を実装するクラスの1つである、モジュールが配置されるディレクトリを作成する必要があります。 価格のフォーマットを担当する標準モジュールはCoreと呼ばれるため、CoreCという名前を付けました。

次のディレクトリ構造を作成します。

# ll -1aR /<Path To Magento>/app/code/local/
.
..
Cifrum

/<Path To Magento>/app/code/local/Cifrum:
.
..
CoreC

/<Path To Magento>/app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


最後のフォルダーのリストは、モジュールの標準構造です。 ここで詳細に説明することはしませんが、必要に応じて、 php |建築家向けのMagentoでのプログラミングガイドを読むことができます。

このケースでは、 config.xmlファイルを配置するetcなどと 、クラスの説明が配置されるModelの 2つのディレクトリに注目します。

Coreモジュールはフォーマットされた価格の出力に関与し、StoreクラスであるformatPrice()メソッドは具象に関与します。 Mage_Core_Model_Storeの継承クラスである新しいクラスを作成し、そのメソッドをオーバーライドする必要があります。

次の内容のModel / Store.phpファイルを作成しましょう。

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

?>


* This source code was highlighted with Source Code Highlighter .
<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

?>


* This source code was highlighted with Source Code Highlighter .
<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

?>


* This source code was highlighted with Source Code Highlighter .



ご覧のとおり、標準関数Mage_Core_Model_Store :: formatPrice()のコードを取得し、文字列内のサブストリング「rub」の出現のチェックを追加します。 すべてのロケールで機能するかどうかはわかりませんが(「p」がどこかに表示される場合があります)、それは機能します。

ここで、作成したクラスで何をする必要があるかを正確に示す必要があります。 これを行うには、 etc / config.xmlを作成し、次を入力します。

<? xml version ="1.0" ? >
< config >
< modules >

<!-- - , , -->
< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >
<!-- no dependencies -->
</ depends >
</ Cifrum_CoreC >
<!-- -->

</ modules >
< global >
< models >

<!-- -->
<!-- <rewrite> -->
< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >
<!-- -->

</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >
<!-- config values -->
</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >
<!-- config values -->
</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" ? >
< config >
< modules >

<!-- - , , -->
< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >
<!-- no dependencies -->
</ depends >
</ Cifrum_CoreC >
<!-- -->

</ modules >
< global >
< models >

<!-- -->
<!-- <rewrite> -->
< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >
<!-- -->

</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >
<!-- config values -->
</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >
<!-- config values -->
</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" ? >
< config >
< modules >

<!-- - , , -->
< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >
<!-- no dependencies -->
</ depends >
</ Cifrum_CoreC >
<!-- -->

</ modules >
< global >
< models >

<!-- -->
<!-- <rewrite> -->
< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >
<!-- -->

</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >
<!-- config values -->
</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >
<!-- config values -->
</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



繰り返しますが、ファイル構造は標準であり、すべてを必要とするわけではありません。 重要な点をコメントで強調しました。 最初に、モジュールの名前とバージョンを説明し、次に、 <rewrite>タグを使用してコアモジュールのストアクラスのシステムコールを再定義します。

ただし、これだけではありません。 システムに新しいモジュールがあることを伝え、それをアクティブにする必要があります。
覚えているように、システム構成はapp / code /などにあります。 ファイルapp / etc / modules / Cifrum_All.xmlを作成して開きます。このファイルには、Cifrumパッケージのすべてのモジュールの説明が含まれています。

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >


* This source code was highlighted with Source Code Highlighter .



実際、それがすべてです。 ストアページを更新すると、10分の1ルーブルが消えたことがわかります。

質問や説明がある場合-書いて、喜んで伝えます。

PSロシア語と英語のコメントがワイルドに混ざり合っていることをおizeびします-記事の執筆中に追加されたロシア人は、自分で英語を書きました。

Source: https://habr.com/ru/post/J63902/


All Articles