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

4月 16, 2022

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

/**
 * 商品がカテゴリーに属しているかどうかチェックする 子カテゴリーは考慮しない
 * @param $category \Eccube\Entity\Category|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 \Eccube\Entity\Tag) {
            if ($T->getTag() === $Tag) {
                return true;
            }
        }
        return false;
    })->isEmpty();
}
-------------------------------------------------------



Twigテンプレート使用例
-------------------------------------------------------
{% if Product.hasTag('新着商品') %}
  新着商品です。
{% else %}
  新着商品ではありません。
{% endif %}
-------------------------------------------------------






[!] デバッグで下記エラーが出た場合
-------------------------------------------------------
Neither the property "hasTag" nor one of the methods "hasTag()", "gethasTag()"/"ishasTag()"/"hashasTag()" or "__call()" exist and have public access in class "Eccube\Entity\Product".
-------------------------------------------------------

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



You Might Also Like

0 コメント

Popular Posts

About Me

Flickr Images