Magento 2 - 将产品属性设置为使用默认值

时间:2016-10-21 12:59:46

标签: magento magento2

我想检查特定商店视图的所有产品的使用默认值

我使用此代码

<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="tabs">
<ul> 
    <li><a href="#tabs1" id="lan1" class="lantabs">Dutch</a></li>
    <li><a href="#tabs2" id="lan2" class="lantabs">English</a></li>
    <li><a href="#tabs3" id="lan3" class="lantabs">German</a></li>
</ul>
<div id="tabs1">
    <table style="margin:0px; border: 1px solid #0498BE;background-color: #FFFEFE; float:none " class="excolltable" align="left" width="100%" cellpadding="10">
        <tbody>
            <tr>
                <td style="font-size: 14px;" align="center" width="40%"><b>Exclude List</b></td>
                <td style="font-size: 14px;" align="center" width="60%"><b>Consider as one Word</b></td>            
            </tr>
            <tr>
                <td style="font-size: 12px;" align="left" width="40%">als, is, te, een, mee, met, de, om, in, aan, het, je, zijn, en, van, op, worden, ook, voor, deze, ik, we, die, bij, over, er, heeft, haar, dit, dat, zo, hem, zijn, maar, mij, was, heel, zeer, heb, door, niet, kan, mijn, had, zij, hij, ga, of, nog, zou, dus, kon, wel, n, v, t, ze, ben, wordt, iets, naar, kunnen, n, v, t, per, dan, daarna, via, doen, gaat, wilde, hier, u, moet, krijg, gestuurd, mag, zegt, gaf, erg, al, hoe, anders, bedoel, wat, wij, na, daar, tot, m, gehad, meer, nu, uw, nee, meeus, kroller, zie, zal, ligt, me, e, weet, af, omdat, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, geeft, wil, staat, uit, hebben, andere, gaan, ons, veel, doe, onze, vind, toch, Obvion, alleen, geen, toe, dingen, denkt, doet, weer, ilse, westerveld, boer, brenda, bruijn, debeij, eddy, ramon, gerry, florius, abn, amro, aegon, ing, asr, syntrus, argenta, regiobank, daarnaast, hetgeen, aangaf, ongeveer, am, acc, accountmanger, accountmanagers, abnamro, als, danwel, etc., nav, werd, jullie, ', 'm, etc, hun, alles, maken, loopt, niets, allemaal, denk, misschien, moment, moeten, verbeterpunten, hypotheek, eigenlijk, altijd, zelf, keer, willen, kunt, echt, toen, bijv, vinden, verder, neen, vooral, meneer, heer, mevrouw, gijs</td>
                <td style="font-size: 12px;" align="left" width="60%">Goede voorwaarden, toegevoegde waarde, up to date, kennis van zaken, Bijzonder Beheer, face to face, eigen belang, min-max</td>
            </tr>
        </tbody>
    </table>
    <br>
    <table style="margin:10px;border: 1px solid #0498BE;background-color: #FFFEFE;float:none" class="excolltable" align="left" width="100%" cellpadding="10">
        <tbody>
        <tr>
            <td style="font-size: 14px;" align="center" width="40%"><b>Synonymwords</b></td>
            <td style="font-size: 14px;" align="center" width="60%"><b>Displayword</b></td>         
        </tr>
        <tr>
            <td style="font-size: 12px;" align="left" width="40%">goed,goede,geod</td>
            <td style="font-size: 12px;" align="left" width="60%">goed</td>
        </tr>
        <tr>
            <td style="font-size: 12px;" align="left" width="40%">prettig,prettige</td>
            <td style="font-size: 12px;" align="left" width="60%">prettig</td>
        </tr>
        <tr>
            <td style="font-size: 12px;" align="left" width="40%">ideeen,ideeën</td>
            <td style="font-size: 12px;" align="left" width="60%">ideeën</td>

        </tr>
        </tbody>
    </table>
</div>
<div id="tabs2"></div>
<div id="tabs3"></div></div>

但这是从类别中删除产品。 您能告诉我如何以编程方式选中使用默认复选框。

1 个答案:

答案 0 :(得分:3)

Magento 2.1.3 EE

以下解决方案创建CLI命令以直接操作数据库并删除特定于商店的产品属性信息。它是为Magento Enterprise版本编写的,因此如果您使用的是Community Edition,则必须修改此代码才能使用entity_id代替row_id

请小心这一点。此处提出的解决方案绕过模型类并在默认数据库连接catalog_product_entity_datetimecatalog_product_entity_decimalcatalog_product_entity_intcatalog_product_entity_text和{{1}上执行直接删除查询表格。首先备份数据库。

步骤1:创建模块

catalog_product_entity_varchar

app/code/StackOverflow/Question40177336/registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'StackOverflow_Question40177336', __DIR__ );

app/code/StackOverflow/Question40177336/etc/module.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="StackOverflow_Question40177336" setup_version="0.0.1"/> </config>

app/code/StackOverflow/Question40177336/etc/di.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandListInterface"> <arguments> <argument name="commands" xsi:type="array"> <item name="stackoverflow_question40177336" xsi:type="object">StackOverflow\Question40177336\Console\Command\Product\UseDefaultValue</item> </argument> </arguments> </type> </config>

app/code/StackOverflow/Question40177336/Console/Command/Product/UseDefaultValue.php

步骤2:启用模块

<?php namespace StackOverflow\Question40177336\Console\Command\Product; use Magento\Catalog\Model\Product; use Magento\Eav\Setup\EavSetup; use Magento\Framework\App\ResourceConnection; use Magento\Store\Model\StoreManagerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class UseDefaultValue extends Command { /** * flag indicating if the command has been initialized yet * * @var bool */ protected $initialized = false; /** * The attribute_id to use for the current command. * * @var int */ protected $attributeId; /** * The row_id values(s) to use for the command (if any). * * @var array|bool */ protected $rowIds; /** * The store_id to use for the current command. * * @var int */ protected $storeId; /** * The table name to use for the current command. * * @var string */ protected $tableName; /** * @var \Magento\Framework\DB\Adapter\AdapterInterface */ protected $connection; /** * @var EavSetup */ protected $eavSetup; /** * @var StoreManagerInterface */ protected $storeManager; public function __construct( EavSetup $eavSetup, ResourceConnection $resourceConnection, StoreManagerInterface $storeManager ) { $this->connection = $resourceConnection->getConnection(); $this->eavSetup = $eavSetup; $this->storeManager = $storeManager; parent::__construct(); } /** * Configures the current command. */ protected function configure() { $this ->setName('catalog:product:attributes:use-default-value') ->setDescription('Removes store specific data from a product(s) of given attribute code.') ->addArgument( 'attribute_code', InputArgument::REQUIRED, 'Attribute Code' ) ->addArgument( 'store', InputArgument::REQUIRED, 'Store code or store_id (cannot be \'admin\' or \'0\')' ) ->addArgument( 'sku', InputArgument::OPTIONAL, 'Sku (omit to apply to all products)' ) ; } /** * Executes the current command. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance */ protected function execute(InputInterface $input, OutputInterface $output) { $this->init($input); $conn = $this->connection; $bind = [ $conn->quoteInto('store_id = ?', $this->getStoreId()), $conn->quoteInto('attribute_id = ?', $this->getAttributeId()) ]; if ($this->getRowIds()) { $bind[] = $conn->quoteInto('row_id IN (?)', $this->getRowIds()); } $rows = $conn->delete($this->getTableName(), $bind); $output->writeln($rows.' rows deleted.'); } /** * Return the row_id value(s) to use for the command (if any). * * @return array|boolean */ protected function getRowIds() { if (!$this->initialized) { $this->errorInit(__METHOD__); } return $this->rowIds; } /** * Initializes some class properties. * * @param InputInterface $input */ protected function init(InputInterface $input) { if (!$this->initialized) { $attributeCode = trim($input->getArgument('attribute_code')); if ($attributeCode == '') { throw new \RuntimeException(__('attribute_code is required.')); } elseif (is_numeric($attributeCode)) { throw new \RuntimeException(__('attribute_code cannot be numeric.')); } $attribute = $this->eavSetup->getAttribute( Product::ENTITY, $attributeCode ); if (!$attribute) { throw new \RuntimeException(__('Invalid attribute_code "%1"', $attributeCode)); } $backendType = $attribute['backend_type']; $allowedTypes = ['datetime','decimal','int','text','varchar']; if (!in_array($backendType, $allowedTypes)) { throw new \RuntimeException(__( 'backend_type "%1" is not allowed. Allowed types include: %2', $backendType, implode(', ', $allowedTypes) )); } $this->tableName = $this->connection->getTableName('catalog_product_entity_'.$backendType); $this->attributeId = (int) $attribute['attribute_id']; $store = $this->storeManager->getStore($input->getArgument('store')); if ($store->getCode() == 'admin') { throw new \RuntimeException(__('Admin Store is not allowed for this command.')); } $this->storeId = (int) $store->getId(); $sku = trim($input->getArgument('sku')); if ($sku != '') { $sql = $this->connection->select() ->from($this->connection->getTableName('catalog_product_entity'), 'row_id') ->where('sku = ?', $sku) ; $rowIds = $this->connection->fetchCol($sql); if (!$rowIds) { throw new \RuntimeException(__('Invalid Sku "%1"', $sku)); } foreach ($rowIds as $k => $v) { $rowIds[$k] = (int) $v; } $this->rowIds = $rowIds; } else { $this->rowIds = false; } $this->initialized = true; } } /** * Returns the attribute_id to use for the current command. * * @return int */ protected function getAttributeId() { if (!$this->attributeId) { $this->errorInit(__METHOD__); } return $this->attributeId; } /** * Return the store id to use for the current command. * * @param InputInterface $input */ protected function getStoreId() { if (!$this->storeId) { $this->errorInit(__METHOD__); } return $this->storeId; } /** * Return the qualified table name to use for the current command. * * @param InputInterface $input */ protected function getTableName() { if (!$this->tableName) { $this->errorInit(__METHOD__); } return $this->tableName; } /** * Throws an exception. * * @param string $methodName * @throws \LogicException */ protected function errorInit($methodName) { throw new \LogicException( __('Command has not been intialized. Call UseDefaultValue::init() before calling '.$methodName)); ; } }

第3步:使用新的CLI命令。

该命令有两个必需参数php -f bin/magento module:enable StackOverflow_Question40177336attribute_code。商店可以是ID或代码。出于明显的原因,不允许使用管理员商店如果您希望仅定位特定SKU,则该命令还具有SKU的可选第三个参数(省略此选项适用于所有产品)。

例如,如果你想删除所有&#34; name&#34;来自&#34;默认&#34;的值存储视图您的命令如下:

store