zoukankan      html  css  js  c++  java
  • magento2.2.3 根据产品ID获取栏目名称的正确调用方式

    • 根据product_id 获取 category_ids : 
          /**
           * @param $product_id
           * @return array
           */
          public function mc_getCategoryIds($product_id)
          {
      //       +++ get product the category id
              $registry = $this->mc_C['product'];
              $getObj = $this->mc_get_obj($registry);
              /**
               * @var MagentoCatalogModelProduct $getObj
               * MagentoCatalogModelProduct
               */
              $product = $getObj->load($product_id);
      
              /**
               * @var MagentoCatalogModelProduct $product
               * MagentoCatalogModelProduct
               */
      
              return $product->getCategoryIds();
          }
      View Code
    • 获取 category_name :
      /**
           * @param $product_id
           * @return mixed
           */
          public function mc_getCategoryName($product_id)
          {
              $cats = $this->mc_getCategoryIds($product_id);
              if(count($cats) ){
                  $firstCategoryId = $cats[(count($cats)-1)];
                  $registry = $this->mc_C['category'];
                  $getObj = $this->mc_get_obj($registry);
      
                  /**
                   * @var MagentoCatalogModelCategoryFactory $getObj
                   */
                  $_category = $getObj->create();
                  /**
                   * @var MagentoCatalogModelCategoryFactory $_category
                   */
                  $data = $_category->load($firstCategoryId);
                  /**
                   * @var MagentoCatalogModelCategoryFactory $data
                   */
                  return $data->getName();
              }
              return 0;
          }
      View Code

    以上简单的调用了栏目的名称。

    FAQ:

    1,如果使用不正确的 CategoryFactory 类 : 

    • 're_category_c'=>MagentoCatalogModelResourceModelCategoryCollectionFactory::class,//fixme This class MagentoCatalogModelResourceModelCategoryCollection
    • 're_category_f'=>MagentoCatalogModelResourceModelCategoryFactory::class,//fixme This classMagentoCatalogModelResourceModelCategory
    • 're_category'=>MagentoCatalogModelResourceModelCategory::class,//fixme This  Catalog category model

      正确的是:MagentoCatalogModelCategoryFactory

      ResourceModel 与 Model 的区别:

     

     

    Models : Models are where your main business logic should be handled and is a single instance of an object. The model will use the resource model to talk to the database and get/set data for it on save() and load().

    Resource Model : A resource model is where your main C.R.U.D happens (CreateReadUpdateand delete). The resource model shouldn’t contain business logic however it will talk to the adapters and basically talk to the database.

     

    2,如果使用不正确的通用SQL : 

      SQL: 

    select value from catalog_product_entity_varchar left join eav_attribute on
        eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id
    where
        eav_attribute.attribute_code='name' and 
        catalog_product_entity_varchar.entity_id=2082

      CODE:

    $_connection = $this->mc_get_obj(MagentoFrameworkAppResourceConnection::class);//get class
                $db_connection = $_connection->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);//connection
                $category = $db_connection->fetchAll('select * from '.'`catalog_product_entity_varchar` '.'left join '.'`eav_attribute` '.'on '.
                    '`eav_attribute`.'.'`attribute_id`='.'`catalog_product_entity_varchar`.'.'`attribute_id` '.
                    'where '.'`eav_attribute`.'.'`attribute_code`='.'"name" '.'and '. '`catalog_product_entity_varchar`.'.'`entity_id`='.$vi['product_id']);
    
                var_dump($category[0]['value']);die;

      

      正确的SQL: 
      

    SELECT 
      e.entity_id AS product_id
      , e.type_id AS product_type
      , e.sku,
      (
        SELECT
          GROUP_CONCAT(DISTINCT(cv.value))
        FROM 
          catalog_category_entity_varchar AS cv, catalog_category_product AS at_category_id 
        WHERE
          at_category_id.category_id = cv.entity_id
          AND (at_category_id.product_id = e.entity_id) 
          AND cv.attribute_id = 41 and cv.store_id = 0
      ) AS category_name 
    FROM catalog_product_entity AS e where e.entity_id=2082;
  • 相关阅读:
    稳扎稳打Silverlight(13) 2.0交互之鼠标事件和键盘事件
    稳扎稳打Silverlight(17) 2.0数据之详解DataGrid, 绑定数据到ListBox
    再接再厉VS 2008 sp1 + .NET 3.5 sp1(2) Entity Framework(实体框架)之详解 Linq To Entities 之一
    稳扎稳打Silverlight(8) 2.0图形之基类System.Windows.Shapes.Shape
    稳扎稳打Silverlight(11) 2.0动画之ColorAnimation, DoubleAnimation, PointAnimation, 内插关键帧动画
    稳扎稳打Silverlight(21) 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应
    稳扎稳打Silverlight(16) 2.0数据之独立存储(Isolated Storage)
    稳扎稳打Silverlight(9) 2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    稳扎稳打Silverlight(23) 2.0通信之调用WCF的双向通信(Duplex Service)
    游戏人生Silverlight(1) 七彩俄罗斯方块[Silverlight 2.0(c#)]
  • 原文地址:https://www.cnblogs.com/q1104460935/p/11237280.html
Copyright © 2011-2022 走看看