Calculate attribute value in indexer or cron?Magento2 add custom address attributeHaving trouble exporting products from Magento 2.x. Fatal error: Uncaught Error: Call to a member function getName()Magento 2: Add a product to the cart programmaticallyHow to solve Front controller reached 100 router match iterations in magento2Magento 2: I Want to add multiple product using checkboxI have created one field using product form field for my price i want save my field value at product creation time from backend magento2What is the correct way to create Mock input interface in Web-Api functional test?Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?magento 2.2 trying to save multi select value in database
Limit of an integral vs Limit of the integrand
Why does the Earth follow an elliptical trajectory rather than a parabolic one?
Is it a bad idea to replace pull-up resistors with hard pull-ups?
Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?
Why does "decimal.TryParse()" always return 0 for the input string "-1" in the below code?
Increase height of laser cut design file for enclosure
As programers say: Strive to be lazy
What is the best way for a skeleton to impersonate human without using magic?
What does i386 mean on macOS Mojave?
semanage not changing file context
"Fīliolō me auctum scito, salva Terentia"; what is "me" role in this phrase?
Why was this sacrifice sufficient?
Ex-manager wants to stay in touch, I don't want to
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
Remove everything except csv file Bash Script
Guns in space with bullets that return?
How are one-time password generators like Google Authenticator different from having two passwords?
How can this pool heater gas line be disconnected?
Make all the squares explode
Exception propagation: When to catch exceptions?
Is there a faster way to calculate Abs[z]^2 numerically?
Is Simic Ascendancy triggered by Awakening of Vitu-Ghazi?
Is there any evidence to support the claim that the United States was "suckered into WW1" by Zionists, made by Benjamin Freedman in his 1961 speech?
Ubuntu won't let me edit or delete .vimrc file
Calculate attribute value in indexer or cron?
Magento2 add custom address attributeHaving trouble exporting products from Magento 2.x. Fatal error: Uncaught Error: Call to a member function getName()Magento 2: Add a product to the cart programmaticallyHow to solve Front controller reached 100 router match iterations in magento2Magento 2: I Want to add multiple product using checkboxI have created one field using product form field for my price i want save my field value at product creation time from backend magento2What is the correct way to create Mock input interface in Web-Api functional test?Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?magento 2.2 trying to save multi select value in database
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is my observer and its updating my custom attribute every time when I save attributes on catalog_product_attribute_update_before event.
namespace MyVendorMyModuleObserver;
class TotalCalcScore implements MagentoFrameworkEventObserverInterface
{
protected $_product;
public function __construct(
MagentoCatalogModelProductFactory $product
)
$this->_product = $product;
public function execute(
MagentoFrameworkEventObserver $observer
)
$productIds = $observer->getProductIds();
foreach($productIds as $id)
$product = $this->_product->create()->load($id);
if($product->getId())
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
But what should I do if I want to move this to Model and run this like cron job or indexation? Or is it better to run this before indexation like observer. (but I need load collection in that case?)
UPDATE:
I created mview.xml and indexer.xml
and this is my Model/Review/Indexer/Review.php but still is not working corectly
class Review implements MagentoFrameworkIndexerActionInterface, MagentoFrameworkMviewActionInterface
/**
* Execute full indexation
*
* @return void
*/
public function executeFull()
// TODO: Implement executeFull() method.
/**
* Execute partial indexation by ID list
*
* @param int[] $ids
*
* @return void
*/
public function executeList(array $ids)
// TODO: Implement executeList() method.
/**
* Execute partial indexation by ID
*
* @param int $id
*
* @return void
*/
public function executeRow($id)
// TODO: Implement executeRow() method.
/**
* @var MagentoCatalogApiDataProductInterfaceFactory
*/
private $productFactory;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
private $productRepository;
/**
* MyClass constructor
*
* @param MagentoCatalogApiDataProductInterfaceFactory $productFactory
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
protected $collectionFactory;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogApiDataProductAttributeInterface $productAttribute,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
)
$this->productRepository = $productRepository;
$this->collectionFactory = $collectionFactory;
$this->collectionFactory = $productAttribute;
/**
* Execute materialization on ids entities
*
* @param int[] $ids
*
* @return void
* @api
*/
public function execute($ids)
// $post = $this->_postFactory->create();
// $collection = $post->getCollection();
// Use factory to create a new product collection
$productCollection = $this->collectionFactory->create();
// Apply filters here
$productCollection->addAttributeToSelect('*');
// Don't have to do this
$productCollection->load();
foreach($collection as $ids)
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product)
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
exit();
return $this->_pageFactory->create();
after reindex :
index has been rebuilt successfully in 00:00:00
but there is no change in atribute value
Maybe I should add this like a cron job and this above not make any sense?
magento2 cron custom-attributes indexer calculate
add a comment |
This is my observer and its updating my custom attribute every time when I save attributes on catalog_product_attribute_update_before event.
namespace MyVendorMyModuleObserver;
class TotalCalcScore implements MagentoFrameworkEventObserverInterface
{
protected $_product;
public function __construct(
MagentoCatalogModelProductFactory $product
)
$this->_product = $product;
public function execute(
MagentoFrameworkEventObserver $observer
)
$productIds = $observer->getProductIds();
foreach($productIds as $id)
$product = $this->_product->create()->load($id);
if($product->getId())
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
But what should I do if I want to move this to Model and run this like cron job or indexation? Or is it better to run this before indexation like observer. (but I need load collection in that case?)
UPDATE:
I created mview.xml and indexer.xml
and this is my Model/Review/Indexer/Review.php but still is not working corectly
class Review implements MagentoFrameworkIndexerActionInterface, MagentoFrameworkMviewActionInterface
/**
* Execute full indexation
*
* @return void
*/
public function executeFull()
// TODO: Implement executeFull() method.
/**
* Execute partial indexation by ID list
*
* @param int[] $ids
*
* @return void
*/
public function executeList(array $ids)
// TODO: Implement executeList() method.
/**
* Execute partial indexation by ID
*
* @param int $id
*
* @return void
*/
public function executeRow($id)
// TODO: Implement executeRow() method.
/**
* @var MagentoCatalogApiDataProductInterfaceFactory
*/
private $productFactory;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
private $productRepository;
/**
* MyClass constructor
*
* @param MagentoCatalogApiDataProductInterfaceFactory $productFactory
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
protected $collectionFactory;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogApiDataProductAttributeInterface $productAttribute,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
)
$this->productRepository = $productRepository;
$this->collectionFactory = $collectionFactory;
$this->collectionFactory = $productAttribute;
/**
* Execute materialization on ids entities
*
* @param int[] $ids
*
* @return void
* @api
*/
public function execute($ids)
// $post = $this->_postFactory->create();
// $collection = $post->getCollection();
// Use factory to create a new product collection
$productCollection = $this->collectionFactory->create();
// Apply filters here
$productCollection->addAttributeToSelect('*');
// Don't have to do this
$productCollection->load();
foreach($collection as $ids)
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product)
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
exit();
return $this->_pageFactory->create();
after reindex :
index has been rebuilt successfully in 00:00:00
but there is no change in atribute value
Maybe I should add this like a cron job and this above not make any sense?
magento2 cron custom-attributes indexer calculate
add a comment |
This is my observer and its updating my custom attribute every time when I save attributes on catalog_product_attribute_update_before event.
namespace MyVendorMyModuleObserver;
class TotalCalcScore implements MagentoFrameworkEventObserverInterface
{
protected $_product;
public function __construct(
MagentoCatalogModelProductFactory $product
)
$this->_product = $product;
public function execute(
MagentoFrameworkEventObserver $observer
)
$productIds = $observer->getProductIds();
foreach($productIds as $id)
$product = $this->_product->create()->load($id);
if($product->getId())
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
But what should I do if I want to move this to Model and run this like cron job or indexation? Or is it better to run this before indexation like observer. (but I need load collection in that case?)
UPDATE:
I created mview.xml and indexer.xml
and this is my Model/Review/Indexer/Review.php but still is not working corectly
class Review implements MagentoFrameworkIndexerActionInterface, MagentoFrameworkMviewActionInterface
/**
* Execute full indexation
*
* @return void
*/
public function executeFull()
// TODO: Implement executeFull() method.
/**
* Execute partial indexation by ID list
*
* @param int[] $ids
*
* @return void
*/
public function executeList(array $ids)
// TODO: Implement executeList() method.
/**
* Execute partial indexation by ID
*
* @param int $id
*
* @return void
*/
public function executeRow($id)
// TODO: Implement executeRow() method.
/**
* @var MagentoCatalogApiDataProductInterfaceFactory
*/
private $productFactory;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
private $productRepository;
/**
* MyClass constructor
*
* @param MagentoCatalogApiDataProductInterfaceFactory $productFactory
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
protected $collectionFactory;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogApiDataProductAttributeInterface $productAttribute,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
)
$this->productRepository = $productRepository;
$this->collectionFactory = $collectionFactory;
$this->collectionFactory = $productAttribute;
/**
* Execute materialization on ids entities
*
* @param int[] $ids
*
* @return void
* @api
*/
public function execute($ids)
// $post = $this->_postFactory->create();
// $collection = $post->getCollection();
// Use factory to create a new product collection
$productCollection = $this->collectionFactory->create();
// Apply filters here
$productCollection->addAttributeToSelect('*');
// Don't have to do this
$productCollection->load();
foreach($collection as $ids)
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product)
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
exit();
return $this->_pageFactory->create();
after reindex :
index has been rebuilt successfully in 00:00:00
but there is no change in atribute value
Maybe I should add this like a cron job and this above not make any sense?
magento2 cron custom-attributes indexer calculate
This is my observer and its updating my custom attribute every time when I save attributes on catalog_product_attribute_update_before event.
namespace MyVendorMyModuleObserver;
class TotalCalcScore implements MagentoFrameworkEventObserverInterface
{
protected $_product;
public function __construct(
MagentoCatalogModelProductFactory $product
)
$this->_product = $product;
public function execute(
MagentoFrameworkEventObserver $observer
)
$productIds = $observer->getProductIds();
foreach($productIds as $id)
$product = $this->_product->create()->load($id);
if($product->getId())
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
But what should I do if I want to move this to Model and run this like cron job or indexation? Or is it better to run this before indexation like observer. (but I need load collection in that case?)
UPDATE:
I created mview.xml and indexer.xml
and this is my Model/Review/Indexer/Review.php but still is not working corectly
class Review implements MagentoFrameworkIndexerActionInterface, MagentoFrameworkMviewActionInterface
/**
* Execute full indexation
*
* @return void
*/
public function executeFull()
// TODO: Implement executeFull() method.
/**
* Execute partial indexation by ID list
*
* @param int[] $ids
*
* @return void
*/
public function executeList(array $ids)
// TODO: Implement executeList() method.
/**
* Execute partial indexation by ID
*
* @param int $id
*
* @return void
*/
public function executeRow($id)
// TODO: Implement executeRow() method.
/**
* @var MagentoCatalogApiDataProductInterfaceFactory
*/
private $productFactory;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
private $productRepository;
/**
* MyClass constructor
*
* @param MagentoCatalogApiDataProductInterfaceFactory $productFactory
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
protected $collectionFactory;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogApiDataProductAttributeInterface $productAttribute,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
)
$this->productRepository = $productRepository;
$this->collectionFactory = $collectionFactory;
$this->collectionFactory = $productAttribute;
/**
* Execute materialization on ids entities
*
* @param int[] $ids
*
* @return void
* @api
*/
public function execute($ids)
// $post = $this->_postFactory->create();
// $collection = $post->getCollection();
// Use factory to create a new product collection
$productCollection = $this->collectionFactory->create();
// Apply filters here
$productCollection->addAttributeToSelect('*');
// Don't have to do this
$productCollection->load();
foreach($collection as $ids)
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product)
// attribute_first_count
$value_offirst_count = $product->getattribute_first_count();
// attribute_second_count
$value_offsecond_score = $product->getattribute_second_count();
// attribute_calculated_count
$value_calculated_score = $product->getattribute_calculated_count();
// calculate
$value_calculated_score = $value_offsecond_score + $value_offirst_count
//save calculated value
//$product->setCalculatedValue($value_calculated_score);
//save to attribute_calculated_score
$product->setAttrCalculatedScore($value_calculated_score);
$product->save();
exit();
return $this->_pageFactory->create();
after reindex :
index has been rebuilt successfully in 00:00:00
but there is no change in atribute value
Maybe I should add this like a cron job and this above not make any sense?
magento2 cron custom-attributes indexer calculate
magento2 cron custom-attributes indexer calculate
edited May 7 at 12:22
BartZalas
asked Apr 30 at 16:06
BartZalasBartZalas
629412
629412
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I finished to set this like a cron job. I'm not sure is it proper way to do this.
I moved my function from observer to controller and changed my crontab.xml to use this controller.
<job name="custom_cronjob" instance="MyVendorMyModuleControllerCalc" method="execute">
<schedule>* * * * *</schedule>
</job>
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272971%2fcalculate-attribute-value-in-indexer-or-cron%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I finished to set this like a cron job. I'm not sure is it proper way to do this.
I moved my function from observer to controller and changed my crontab.xml to use this controller.
<job name="custom_cronjob" instance="MyVendorMyModuleControllerCalc" method="execute">
<schedule>* * * * *</schedule>
</job>
add a comment |
I finished to set this like a cron job. I'm not sure is it proper way to do this.
I moved my function from observer to controller and changed my crontab.xml to use this controller.
<job name="custom_cronjob" instance="MyVendorMyModuleControllerCalc" method="execute">
<schedule>* * * * *</schedule>
</job>
add a comment |
I finished to set this like a cron job. I'm not sure is it proper way to do this.
I moved my function from observer to controller and changed my crontab.xml to use this controller.
<job name="custom_cronjob" instance="MyVendorMyModuleControllerCalc" method="execute">
<schedule>* * * * *</schedule>
</job>
I finished to set this like a cron job. I'm not sure is it proper way to do this.
I moved my function from observer to controller and changed my crontab.xml to use this controller.
<job name="custom_cronjob" instance="MyVendorMyModuleControllerCalc" method="execute">
<schedule>* * * * *</schedule>
</job>
answered May 7 at 12:21
BartZalasBartZalas
629412
629412
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272971%2fcalculate-attribute-value-in-indexer-or-cron%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown