Magento 2: How to set visibility and status to product using collection?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm creating a cron to pass through all the products and set them visible and active.
This is my execute of the cron who runs well:
public function execute(){
/**
* @var $item MagentoCatalogModelProduct
* @var MagentoCatalogModelResourceModelProductCollection $collection
*/
$items = $this->productCollection->getItems();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.
What I'm doing wrong?
magento2 module attributes collection
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm creating a cron to pass through all the products and set them visible and active.
This is my execute of the cron who runs well:
public function execute(){
/**
* @var $item MagentoCatalogModelProduct
* @var MagentoCatalogModelResourceModelProductCollection $collection
*/
$items = $this->productCollection->getItems();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.
What I'm doing wrong?
magento2 module attributes collection
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm creating a cron to pass through all the products and set them visible and active.
This is my execute of the cron who runs well:
public function execute(){
/**
* @var $item MagentoCatalogModelProduct
* @var MagentoCatalogModelResourceModelProductCollection $collection
*/
$items = $this->productCollection->getItems();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.
What I'm doing wrong?
magento2 module attributes collection
I'm creating a cron to pass through all the products and set them visible and active.
This is my execute of the cron who runs well:
public function execute(){
/**
* @var $item MagentoCatalogModelProduct
* @var MagentoCatalogModelResourceModelProductCollection $collection
*/
$items = $this->productCollection->getItems();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.
What I'm doing wrong?
magento2 module attributes collection
magento2 module attributes collection
edited May 29 '17 at 3:02
Rafael Corrêa Gomes
4,75523366
4,75523366
asked May 26 '16 at 12:55
ntzzntzz
4441624
4441624
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You should replace the $item->save(); with below code:
$item->getResource()->saveAttribute($item, 'visibility');
$item->getResource()->saveAttribute($item, 'status');
add a comment |
Try to change the Collection and set the AreaCode, I've created something similar and works.
namespace NameSpaceModuleNameConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class EAVMassUpdateCommand extends Command
{
public function __construct(
MagentoFrameworkAppState $state,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
) {
$state->setAreaCode('adminhtml');
$this->_collectionFactory = $collectionFactory;
parent::__construct();
}
...
public function execute(){
$items = $this->_collectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
add a comment |
Try this way:
public function __construct(
.....
MagentoFrameworkAppActionContext $context
MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
MagentoCatalogModelResourceModelProductCollection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
add a comment |
For getting product collection with status enable and visibility:
you have to keep below code in block file.
protected $_coreRegistry = null;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility,
array $data =
)
{
$this->_coreRegistry = $registry;
$this->_productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
return $collection;
}
Above code working for visibility type for:
- VISIBILITY_IN_SEARCH
- VISIBILITY_IN_CATALOG
- VISIBILITY_BOTH
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%2f117112%2fmagento-2-how-to-set-visibility-and-status-to-product-using-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should replace the $item->save(); with below code:
$item->getResource()->saveAttribute($item, 'visibility');
$item->getResource()->saveAttribute($item, 'status');
add a comment |
You should replace the $item->save(); with below code:
$item->getResource()->saveAttribute($item, 'visibility');
$item->getResource()->saveAttribute($item, 'status');
add a comment |
You should replace the $item->save(); with below code:
$item->getResource()->saveAttribute($item, 'visibility');
$item->getResource()->saveAttribute($item, 'status');
You should replace the $item->save(); with below code:
$item->getResource()->saveAttribute($item, 'visibility');
$item->getResource()->saveAttribute($item, 'status');
edited Apr 21 '17 at 21:10
Rafael Corrêa Gomes
4,75523366
4,75523366
answered Jan 19 '17 at 9:59
Cuong NguyenCuong Nguyen
1
1
add a comment |
add a comment |
Try to change the Collection and set the AreaCode, I've created something similar and works.
namespace NameSpaceModuleNameConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class EAVMassUpdateCommand extends Command
{
public function __construct(
MagentoFrameworkAppState $state,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
) {
$state->setAreaCode('adminhtml');
$this->_collectionFactory = $collectionFactory;
parent::__construct();
}
...
public function execute(){
$items = $this->_collectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
add a comment |
Try to change the Collection and set the AreaCode, I've created something similar and works.
namespace NameSpaceModuleNameConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class EAVMassUpdateCommand extends Command
{
public function __construct(
MagentoFrameworkAppState $state,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
) {
$state->setAreaCode('adminhtml');
$this->_collectionFactory = $collectionFactory;
parent::__construct();
}
...
public function execute(){
$items = $this->_collectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
add a comment |
Try to change the Collection and set the AreaCode, I've created something similar and works.
namespace NameSpaceModuleNameConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class EAVMassUpdateCommand extends Command
{
public function __construct(
MagentoFrameworkAppState $state,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
) {
$state->setAreaCode('adminhtml');
$this->_collectionFactory = $collectionFactory;
parent::__construct();
}
...
public function execute(){
$items = $this->_collectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
Try to change the Collection and set the AreaCode, I've created something similar and works.
namespace NameSpaceModuleNameConsoleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class EAVMassUpdateCommand extends Command
{
public function __construct(
MagentoFrameworkAppState $state,
MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
) {
$state->setAreaCode('adminhtml');
$this->_collectionFactory = $collectionFactory;
parent::__construct();
}
...
public function execute(){
$items = $this->_collectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}
edited May 28 '17 at 14:14
answered May 28 '17 at 14:07
Rafael Corrêa GomesRafael Corrêa Gomes
4,75523366
4,75523366
add a comment |
add a comment |
Try this way:
public function __construct(
.....
MagentoFrameworkAppActionContext $context
MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
MagentoCatalogModelResourceModelProductCollection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
add a comment |
Try this way:
public function __construct(
.....
MagentoFrameworkAppActionContext $context
MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
MagentoCatalogModelResourceModelProductCollection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
add a comment |
Try this way:
public function __construct(
.....
MagentoFrameworkAppActionContext $context
MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
MagentoCatalogModelResourceModelProductCollection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
Try this way:
public function __construct(
.....
MagentoFrameworkAppActionContext $context
MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
MagentoCatalogModelResourceModelProductCollection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
$product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
answered May 28 '17 at 14:19
amitshreeamitshree
3,292103983
3,292103983
add a comment |
add a comment |
For getting product collection with status enable and visibility:
you have to keep below code in block file.
protected $_coreRegistry = null;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility,
array $data =
)
{
$this->_coreRegistry = $registry;
$this->_productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
return $collection;
}
Above code working for visibility type for:
- VISIBILITY_IN_SEARCH
- VISIBILITY_IN_CATALOG
- VISIBILITY_BOTH
add a comment |
For getting product collection with status enable and visibility:
you have to keep below code in block file.
protected $_coreRegistry = null;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility,
array $data =
)
{
$this->_coreRegistry = $registry;
$this->_productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
return $collection;
}
Above code working for visibility type for:
- VISIBILITY_IN_SEARCH
- VISIBILITY_IN_CATALOG
- VISIBILITY_BOTH
add a comment |
For getting product collection with status enable and visibility:
you have to keep below code in block file.
protected $_coreRegistry = null;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility,
array $data =
)
{
$this->_coreRegistry = $registry;
$this->_productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
return $collection;
}
Above code working for visibility type for:
- VISIBILITY_IN_SEARCH
- VISIBILITY_IN_CATALOG
- VISIBILITY_BOTH
For getting product collection with status enable and visibility:
you have to keep below code in block file.
protected $_coreRegistry = null;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility,
array $data =
)
{
$this->_coreRegistry = $registry;
$this->_productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
return $collection;
}
Above code working for visibility type for:
- VISIBILITY_IN_SEARCH
- VISIBILITY_IN_CATALOG
- VISIBILITY_BOTH
answered Jun 3 '18 at 14:24
user68116
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%2f117112%2fmagento-2-how-to-set-visibility-and-status-to-product-using-collection%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