Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Commit 72101f1

Browse files
author
Florian Horn
committed
Added support for the flag "--only-empty" to fill only values, which are empty or NULL
1 parent 7cddee2 commit 72101f1

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

Observer/CategoriesObserver.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,34 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8181
*/
8282
protected function updateData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Category $category)
8383
{
84-
$this->pushData($db, $category, 'description', $configuration->getDummyContentText());
85-
$this->pushData($db, $category, 'meta_title', $configuration->getDummyContentText());
86-
$this->pushData($db, $category, 'meta_keywords', $configuration->getDummyContentText());
87-
$this->pushData($db, $category, 'meta_description', $configuration->getDummyContentText());
84+
$this->pushData($db, $configuration, $category, 'description', $configuration->getDummyContentText());
85+
$this->pushData($db, $configuration, $category, 'meta_title', $configuration->getDummyContentText());
86+
$this->pushData($db, $configuration, $category, 'meta_keywords', $configuration->getDummyContentText());
87+
$this->pushData($db, $configuration, $category, 'meta_description', $configuration->getDummyContentText());
8888

8989
return $category;
9090
}
9191

9292
/**
9393
* @param AdapterInterface $db
94+
* @param Configuration $configuration
9495
* @param \Magento\Catalog\Model\Category $category
9596
* @param string $field
9697
* @param string $value
9798
* @throws \Zend_Db_Statement_Exception
9899
*/
99-
protected function pushData(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $field, $value)
100+
protected function pushData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Category $category, $field, $value)
100101
{
101102
// --- Field type TEXT
102103
if ($this->hasAttribute($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field)) {
103-
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field, $value);
104+
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field, $value, $configuration->isUseOnlyEmpty());
104105
} else {
105106
$this->insertAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field, $value);
106107
}
107108

108109
// --- Field type VARCHAR
109110
if ($this->hasAttribute($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field)) {
110-
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value);
111+
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value, $configuration->isUseOnlyEmpty());
111112
} else {
112113
$this->insertAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value);
113114
}
@@ -192,9 +193,10 @@ protected function getAttributeId(AdapterInterface $db, $field)
192193
* @param string $fieldType
193194
* @param string $field
194195
* @param string $value
196+
* @param boolean $useOnlyEmpty
195197
* @throws \Zend_Db_Statement_Exception
196198
*/
197-
protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $fieldType, $field, $value)
199+
protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $fieldType, $field, $value, $useOnlyEmpty)
198200
{
199201
$query = 'UPDATE
200202
%1$s AS e
@@ -205,6 +207,10 @@ protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog
205207
e.store_id = :storeid AND
206208
e.entity_id = :entityid';
207209

210+
if ($useOnlyEmpty) {
211+
$query .= ' AND (e.value = "" OR e.value IS NULL)';
212+
}
213+
208214
$queryText = sprintf(
209215
$query,
210216
$db->getTableName('catalog_category_entity_' . $fieldType)

Observer/ProductsObserver.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,35 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8181
*/
8282
protected function updateData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Product $product)
8383
{
84-
$this->pushData($db, $product, 'description', $configuration->getDummyContentText());
85-
$this->pushData($db, $product, 'short_description', $configuration->getDummyContentText());
86-
$this->pushData($db, $product, 'meta_title', $configuration->getDummyContentText());
87-
$this->pushData($db, $product, 'meta_keyword', $configuration->getDummyContentText());
88-
$this->pushData($db, $product, 'meta_description', $configuration->getDummyContentText());
84+
$this->pushData($db, $configuration, $product, 'description', $configuration->getDummyContentText());
85+
$this->pushData($db, $configuration, $product, 'short_description', $configuration->getDummyContentText());
86+
$this->pushData($db, $configuration, $product, 'meta_title', $configuration->getDummyContentText());
87+
$this->pushData($db, $configuration, $product, 'meta_keyword', $configuration->getDummyContentText());
88+
$this->pushData($db, $configuration, $product, 'meta_description', $configuration->getDummyContentText());
8989

9090
return $product;
9191
}
9292

9393
/**
9494
* @param AdapterInterface $db
95+
* @param Configuration $configuration
9596
* @param \Magento\Catalog\Model\Product $product
9697
* @param string $field
9798
* @param string $value
9899
* @throws \Zend_Db_Statement_Exception
99100
*/
100-
protected function pushData(AdapterInterface $db, \Magento\Catalog\Model\Product $product, $field, $value)
101+
protected function pushData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Product $product, $field, $value)
101102
{
102103
// --- Field type TEXT
103104
if ($this->hasAttribute($db, $product, self::ENTITY_FIELD_TYPE_TEXT, $field)) {
104-
$this->updateAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_TEXT, $field, $value);
105+
$this->updateAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_TEXT, $field, $value, $configuration->isUseOnlyEmpty());
105106
} else {
106107
$this->insertAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_TEXT, $field, $value);
107108
}
108109

109110
// --- Field type VARCHAR
110111
if ($this->hasAttribute($db, $product, self::ENTITY_FIELD_TYPE_VARCHAR, $field)) {
111-
$this->updateAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value);
112+
$this->updateAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value, $configuration->isUseOnlyEmpty());
112113
} else {
113114
$this->insertAttributeByQuery($db, $product, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value);
114115
}
@@ -193,9 +194,10 @@ protected function getAttributeId(AdapterInterface $db, $field)
193194
* @param string $fieldType
194195
* @param string $field
195196
* @param string $value
197+
* @param boolean $useOnlyEmpty
196198
* @throws \Zend_Db_Statement_Exception
197199
*/
198-
protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Product $product, $fieldType, $field, $value)
200+
protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Product $product, $fieldType, $field, $value, $useOnlyEmpty)
199201
{
200202
$query = 'UPDATE
201203
%1$s AS e
@@ -206,6 +208,10 @@ protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog
206208
e.store_id = :storeid AND
207209
e.entity_id = :entityid';
208210

211+
if ($useOnlyEmpty) {
212+
$query .= ' AND (e.value = "" OR e.value IS NULL)';
213+
}
214+
209215
$queryText = sprintf(
210216
$query,
211217
$db->getTableName('catalog_product_entity_' . $fieldType)

0 commit comments

Comments
 (0)