bmxmale/magento2-services

为开发者提供的服务

安装: 844

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 2

类型:magento2-module

dev-main 2022-09-20 14:37 UTC

This package is auto-updated.

Last update: 2024-09-22 01:49:08 UTC


README

__/\\\\\\\\\\\\\____/\\\\____________/\\\\__/\\\_______/\\\__/\\\\____________/\\\\_____/\\\\\\\\\_____/\\\______________/\\\\\\\\\\\\\\\_        
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\_\///\\\___/\\\/__\/\\\\\\________/\\\\\\___/\\\\\\\\\\\\\__\/\\\_____________\/\\\///////////__       
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\___\///\\\\\\/____\/\\\//\\\____/\\\//\\\__/\\\/////////\\\_\/\\\_____________\/\\\_____________      
   _\/\\\\\\\\\\\\\\__\/\\\\///\\\/\\\/_\/\\\_____\//\\\\______\/\\\\///\\\/\\\/_\/\\\_\/\\\_______\/\\\_\/\\\_____________\/\\\\\\\\\\\_____     
    _\/\\\/////////\\\_\/\\\__\///\\\/___\/\\\______\/\\\\______\/\\\__\///\\\/___\/\\\_\/\\\\\\\\\\\\\\\_\/\\\_____________\/\\\///////______    
     _\/\\\_______\/\\\_\/\\\____\///_____\/\\\______/\\\\\\_____\/\\\____\///_____\/\\\_\/\\\/////////\\\_\/\\\_____________\/\\\_____________   
      _\/\\\_______\/\\\_\/\\\_____________\/\\\____/\\\////\\\___\/\\\_____________\/\\\_\/\\\_______\/\\\_\/\\\_____________\/\\\_____________  
       _\/\\\\\\\\\\\\\/__\/\\\_____________\/\\\__/\\\/___\///\\\_\/\\\_____________\/\\\_\/\\\_______\/\\\_\/\\\\\\\\\\\\\\\_\/\\\\\\\\\\\\\\\_ 
        _\/////////////____\///______________\///__\///_______\///__\///______________\///__\///________\///__\///////////////__\///////////////__

Bmxmale_Services

Magento 2 模块,包含一些有用的服务

服务

产品 / 属性
GetAttributeDataByAttributeCode

服务用于选择产品属性(除了 static 后端类型)。如果指定的属性不存在,则抛出 NoSuchEntityException

use Bmxmale\Services\Service\Product\Attribute\GetAttributeDataByAttributeCode;
...
public function __construct(
    private GetAttributeDataByAttributeCode $getAttributeDataByAttributeCode,
) {
}

public function someMethod()
{
    $attributeData = $this->getAttributeDataByAttributeCode->execute('ld_id_ean');

    // ^ array:4 [
    //   "attribute_code" => "ld_id_ean"
    //   "attribute_id" => "583"
    //   "backend_type" => "varchar"
    //   "default_value" => null
    // ]
}

使用 di.xml,您可以扩展来自 eav_attribute 表的额外属性列。只需在服务构造函数中覆盖 $additionalAttributeColumns 参数即可。

    <virtualType name="ExtendedGetAttributeDataByAttributeCode" type="Bmxmale\Services\Service\Product\Attribute\GetAttributeDataByAttributeCode">
        <arguments>
            <argument name="additionalAttributeColumns" xsi:type="array">
                <item name="default_value" xsi:type="string">default_value</item>
                <item name="frontend_label" xsi:type="string">frontend_label</item>
            </argument>
        </arguments>
    </virtualType>

    <type name="Qwerty\Developer\Console\Command\Sample">
        <arguments>
            <argument name="getAttributeDataByAttributeCode" xsi:type="object">ExtendedGetAttributeDataByAttributeCode</argument>
        </arguments>
    </type>
public function someMethod()
{
    $attributeData = $this->getAttributeDataByAttributeCode->execute('ld_id_ean');

    // ^ array:4 [
    //   "attribute_code" => "ld_id_ean"
    //   "attribute_id" => "583"
    //   "backend_type" => "varchar"
    //   "default_value" => null
    //   "frontend_label" => "EAN"
    // ]
}

GetAttributeValuesForEntityId

服务用于选择产品属性值。返回 [store_id => value] 条目。如果没有值,则返回空数组。如果指定的属性不存在,则抛出 NoSuchEntityException

use Bmxmale\Services\Service\Product\Attribute\GetAttributeValuesForEntityId;
...
public function __construct(
    private GetAttributeValuesForEntityId $getAttributeValuesForEntityId,
) {
}

public function someMethod()
{
    $attributeValues = $this->getAttributeValuesForEntityId->execute(
        attributeCode: 'ld_id_ean',
        entityId: 19011
    );

    //    # store_id => value     
    //    ^ array:4 [
    //      0 => "5900988500835"
    //      1 => "5900988500835"
    //      4 => "5900988500835"
    //      12 => "5900988500835"
    //    ]
}