New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not change product status which product created with productRepository #5664
Comments
An addition; |
Got this issue too. Steps to reproduce: use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\Setup\InstallDataInterface;
class InstallData implements InstallDataInterface
{
protected $_productFactory;
public function __construct( ProductFactory $productFactory )
{
$this->_productFactory = $productFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$product = $this->_productFactory->create();
$product->setSku('sku');
$product->setName('Sample Product');
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setWeight(10);
$product->setVisibility(4);
$product->setPrice(100);
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 999999999
)
);
$product->save();
$setup->endSetup();
} Expected result Status is 'Product is online', and Database value of attribute = 1 Actual result Status is 'Product is offline', and Database value of attribute = 2 |
@rbostan please specify Magento version used. |
I'm using 2.1.1 now and problem still exists. |
I am having the same problem on 2.1.3 |
I found the bug. It is in class \Magento\PricePermissions\Helper\Data, method getCanAdminEditProductStatus() It does not check if there is a session or a role. In magento 1, it was done in the following way, but it was not ported to magento2. Or it can be fixed in class Magento\PricePermissions\Observer, method execute()
Magento 1, class: Enterprise_PricePermissions_Model_Observer, method: adminControllerPredispatch
|
I have the same issue here. I saw the file @kervin mentioned. That observer is triggered when a new product is created. I can confirm that even though I update a product (Not creating new one), I am not able to change its status. Expected ResultChaning the status to enabled. Actual ResultStatus is still disabled. Steps to Reproducepublic function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
) {
$this->productRepository = $productRepository;
$this->productFactory = $productFactory;
} $parentProduct = $this->productRepository->get('PARENT_SKU');
$product = $this->productFactory->create();
$product->setStoreId(0);
$product->setWebsiteIds([1,2]);
$product->setSku('UNIQUE SKU');
$product->setName("Some custom title");
$product->setAttributeSetId(1);
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$product->setVisibility($parentProduct->getVisibility());
$product->setTypeId('simple');
$product->setWeight($parentProduct->getWeight());
$product->setExtensionAttributes($parentProduct->getExtensionAttributes());
$product->setOptions($parentProduct->getOptions());
$product->setCustomAttributes($parentProduct->getCustomAttributes());
$product->setPrice($productPrice->getRegularPrice());
try {
$product = $this->productRepository->save($product);
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return false;
}
// Change the status (Updating product)
// You can confirm that $product->getId() is not NULL
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
try {
$product = $this->productRepository->save($product);
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return false;
} |
Hi there, I have tried cloning the product, but the problem still continues with the cloning. Expected Output(1) (COPY PRODUCT ID: XXXX) (COPY PRODUCT STATUS: 1) (COPY PRODUCT STORE ID: 0) Actual Output(1) (COPY PRODUCT ID: XXXX) (COPY PRODUCT STATUS: 2) (COPY PRODUCT STORE ID: 0) Steps to Reproduce$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class);
/** @var \Magento\Catalog\Model\Product\Copier $copier */
$copier = $objectManager->create(\Magento\Catalog\Model\Product\Copier::class);
$originalProduct = $productRepository->get('000000000089210800', true, 0, true);
try {
$product = $copier->copy($originalProduct);
echo "(1) (COPY PRODUCT ID: {$product->getId()}) (COPY PRODUCT STATUS: {$product->getStatus()}) (COPY PRODUCT STORE ID: {$product->getStoreId()})\n";
$product->setStatus(1);
$product->save();
echo "(2) (COPY PRODUCT ID: {$product->getId()}) (COPY PRODUCT STATUS: {$product->getStatus()}) (COPY PRODUCT STORE ID: {$product->getStoreId()})\n";
} catch (\Exception $e) {
echo $e->getMessage()."\n";
echo $e->getTraceAsString()."\n";
} |
Any update on this issue? This is still present in Magento 2.1.6. |
@denisrpriebe There is still no solution, so I did the following work-around. It is not pretty but it works. public function __construct(
\Magento\Framework\App\ResourceConnection $resourceConnection
){
$this->resourceConnection = $resourceConnection;
} /**
* Changes product status
*
* This function changes the status of given product.
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param int $status
*/
protected function changeStatus(\Magento\Catalog\Api\Data\ProductInterface $product, $status)
{
$resourceConnection = $this->resourceConnection;
$connection = $resourceConnection->getConnection();
$tableEavAttribute = $resourceConnection->getTableName('eav_attribute');
$tableProductEntityInt = $resourceConnection->getTableName('catalog_product_entity_int');
$tableEavEntityType = $resourceConnection->getTableName('eav_entity_type');
$entityTypeIdProduct = $connection->fetchOne("SELECT `entity_type_id` FROM `{$tableEavEntityType}` WHERE `entity_type_code` = 'catalog_product' LIMIT 0,1;");
$attributeIdStatus = $connection->fetchOne("SELECT `attribute_id` FROM `{$tableEavAttribute}` WHERE `attribute_code` = 'status' AND `entity_type_id` = {$entityTypeIdProduct}");
$connection->update($tableProductEntityInt, ['value' => $status], "`attribute_id` = {$attributeIdStatus} AND `row_id` = {$product->getRowId()}");
} |
@bnymn Thanks for posting that. I just integrated your code and it works. It's insane that we have to do things like this just to enable products. I'm pretty sure the core Magento engineers have absolutely no idea of SOLID principles or any concept of quality. |
Easier solution would be to login the user... then you can use the the normal way to change status
|
Why is it still not fixed. It was reported on 16 July 2016. It is really serious issue |
@rbostan, thank you for your report. |
@magento-engcom-team Thank you for verifying the issue. Based on the provided information internal tickets |
as temporary workaround you can do
with $this-product as Magento\Catalog\Model\ResourceModel\Product |
[Magento 2.2.6]
|
Hi @milindsingh. Thank you for working on this issue.
|
Have No issue on both 2.1.x 2.2.x and 2.3x
I have product available both frontend and admin |
I am working on this at #dmdcindia1 |
Please try to redeclare the preference for the PolicyInterface during the object manager initialization:
|
Hi @sergey-solo. Thank you for working on this issue.
|
Can't reproduce on 2.3-develop |
Preconditions
Steps to reproduce
$product = $objectManager->create(Product::class); $product->setTypeId(Type::TYPE_SIMPLE) ->setWebsiteIds([1]) ->setName(NAME) ->setSku(SKU) ->setPrice(10) ->setVisibility(Visibility::VISIBILITY_BOTH) ->setStatus(Status::STATUS_DISABLED) ->setCategoryIds(array(1,5,7)); try { $product = $productRepository->save($product); } catch (\Exception $e) { echo $e->getMessage(); }
Product created successfully.
In admin set 'Enable Product' true. But at product grid in admin, status seems 'Disabled'.
Expected result
Product have to be online in website
Actual result
'We can't find products matching the selection.'
And product status seen Disabled in admin grid.
The text was updated successfully, but these errors were encountered: