[EC-CUBE4]特定のタグがあるかを判断

app/Customize/Entity/Product.php(もしくはProductTrait.php)に下記を記入

——————————————————-

/**

 * 商品がカテゴリーに属しているかどうかチェックする 子カテゴリーは考慮しない

 * @param $category EccubeEntityCategory|integer|string

 * @return bool

 */

public function hasTag($Tag){
return false === $this->ProductTag->filter(function (ProductTag $T) use ($Tag){
if (is_int($Tag)) {
if ($T->getTagId() === $Tag) {
return true;
}
} else if (is_string($Tag)) {
if ($T->getTag()->getName() === $Tag) {
return true;
}
} else if ($Tag instanceof EccubeEntityTag) {
if ($T->getTag() === $Tag) {
return true;
}
}
return false;
})->isEmpty();
}

——————————————————-

Twigテンプレート使用例

——————————————————-

{% if Product.hasTag(‘新着商品’) %}

  新着商品です。

{% else %}

  新着商品ではありません。

{% endif %}

——————————————————-

参考リンク:https://umebius.com/eccube/check_if_product_has_tag/

[!] デバッグで下記エラーが出た場合

——————————————————-

Neither the property “hasTag” nor one of the methods “hasTag()”, “gethasTag()”/”ishasTag()”/”hashasTag()” or “__call()” exist and have public access in class “EccubeEntityProduct”.

——————————————————-

app/proxy/entity/src/Eccube/Entity の中にある Product.php にも同じ内容を記述すれば直る

コメント

タイトルとURLをコピーしました