Update product visibility to search only programmaticallly. $product->setVisibility(3) does not work, break the scriptMagento 2: How to generate Product URL Key when creating Product from Rest API?Magento 2: Access Denied Error generated in report while the browser page is loadingMagento installation on live server is throwing errorNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Magento Setup on AWS server getting ErrorUser already has more than 'max_user_connectionsWebsite down: main.CRITICAL: SQLSTATE[28000] [1045] Access denied for user '(removed)'@'localhost' (using password: YES)Problem logging into Backend of Magento 2Getting error while adding category Magento2Checkout Cart page shows 503 errorMagento2.2.5 I am getting Integrity constraint error while placing order?

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes

Is it true that "only photographers care about noise"?

Can Dive Down protect a creature against Pacifism?

How can I find out about the game world without meta-influencing it?

How to represent jealousy in a cute way?

Does every chapter have to "blow the reader away" so to speak?

Customize Smartdiagram

Manager wants to hire me; HR does not. How to proceed?

Is all-caps blackletter no longer taboo?

Why did the AvroCar fail to fly above 3 feet?

Why is it bad to use your whole foot in rock climbing

Why is C++ template use not recommended in space/radiated environment?

Approach sick days in feedback meeting

ISP is not hashing the password I log in with online. Should I take any action?

Why does there seem to be an extreme lack of public trashcans in Taiwan?

Purpose of cylindrical attachments on Power Transmission towers

What game uses dice with compass point arrows, forbidden signs, explosions, arrows and targeting reticles?

Commencez à vous connecter -- I don't understand the phrasing of this

Placement of positioning lights on A320 winglets

Can an open source licence be revoked if it violates employer's IP?

What's a opened solder bridge signifies?

Are athletes' college degrees discounted by employers and graduate school admissions?



Update product visibility to search only programmaticallly. $product->setVisibility(3) does not work, break the script


Magento 2: How to generate Product URL Key when creating Product from Rest API?Magento 2: Access Denied Error generated in report while the browser page is loadingMagento installation on live server is throwing errorNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Magento Setup on AWS server getting ErrorUser already has more than 'max_user_connectionsWebsite down: main.CRITICAL: SQLSTATE[28000] [1045] Access denied for user '(removed)'@'localhost' (using password: YES)Problem logging into Backend of Magento 2Getting error while adding category Magento2Checkout Cart page shows 503 errorMagento2.2.5 I am getting Integrity constraint error while placing order?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have this script to update product attributes, everything else run fine until i added this line below of code to update visibility to search only.
I know i can update it through catalog product grid, but would like to know why this doesn't work.



->setVisibility(3)


or



->setVisibility(search_only)


or



->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_IN_SEARCH)


more details:



 /*check url key*/

/*holder for new url key in case it's a duplicate*/
//$name = $product->getName();
//$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
//$url = strtolower($url) . 'discontinued';
$currentURL = $product->getUrlKey();
$urlKey = $currentURL;
$storeId = 1;
//echo $storeManager->getStore()->getStoreId() . '<br />'; //here returned 1
$isUrlDuplicate;
$urlKey .= '.html';
$connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
$tablename = $connection->getTableName('url_rewrite');
$sql = $connection->select()->from(
['url_rewrite' => $connection->getTableName('url_rewrite')], ['request_path', 'store_id']
)->joinLeft(
['cpe' => $connection->getTableName('catalog_product_entity')], "cpe.entity_id = url_rewrite.entity_id"
)->where('request_path IN (?)', $urlKey)
->where('store_id IN (?)', $storeId)
->where('cpe.sku not in (?)', $sku);
$urlKeyDuplicates = $connection->fetchAssoc($sql);
if (!empty($urlKeyDuplicates))
// return false;
$isUrlDuplicate = 0;
else
//return true;
$isUrlDuplicate = 1;
//if is duplicate, add eol to url
$newURL = $currentURL . '-eol';
$product->setStoreId(1)->setUrlKey($newURL);
echo 'setting new url key to ' . $newURL .'<br />';

echo 'original url key is '.$currentURL . ' is duplicate? ' . ( $isUrlDuplicate > 0 ? 'yes' : 'no') .'<br />';

//$price = trim($data['price']);
echo 'Getting product SKU: '.$sku.', with EOL Status: '.$product->getIsEol().'<br />';
echo 'Updating product SKU: '.$sku.', with EOL Status: '.$status.'('.$option_id.')'.'<br />'; // .' and Price:'.$price.'<br />';
$product->setIsEol($option_id) // use option_id here
->setStoreId(1) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
->setCustomStockStatus($status_id)
->setMetaDescription('Discontinued')
->setVisibility(3) //this line break the script //VISIBILITY_IN_SEARCH VISIBILITY_IN_CATALOG VISIBILITY_BOTH
//->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED)
->save();


error_log



[07-Jun-2019 00:12:42 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'digital-to-analog-audio-converter.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID' in /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/mystore/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(93): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): MagentoFrameworkDBStatementPdoMysql->_execute(Array)
#3 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `u...', Array)
#5 /home/mystore/public_h in /home/mystore/public_html/vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php on line 220









share|improve this question






























    0















    I have this script to update product attributes, everything else run fine until i added this line below of code to update visibility to search only.
    I know i can update it through catalog product grid, but would like to know why this doesn't work.



    ->setVisibility(3)


    or



    ->setVisibility(search_only)


    or



    ->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_IN_SEARCH)


    more details:



     /*check url key*/

    /*holder for new url key in case it's a duplicate*/
    //$name = $product->getName();
    //$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
    //$url = strtolower($url) . 'discontinued';
    $currentURL = $product->getUrlKey();
    $urlKey = $currentURL;
    $storeId = 1;
    //echo $storeManager->getStore()->getStoreId() . '<br />'; //here returned 1
    $isUrlDuplicate;
    $urlKey .= '.html';
    $connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
    $tablename = $connection->getTableName('url_rewrite');
    $sql = $connection->select()->from(
    ['url_rewrite' => $connection->getTableName('url_rewrite')], ['request_path', 'store_id']
    )->joinLeft(
    ['cpe' => $connection->getTableName('catalog_product_entity')], "cpe.entity_id = url_rewrite.entity_id"
    )->where('request_path IN (?)', $urlKey)
    ->where('store_id IN (?)', $storeId)
    ->where('cpe.sku not in (?)', $sku);
    $urlKeyDuplicates = $connection->fetchAssoc($sql);
    if (!empty($urlKeyDuplicates))
    // return false;
    $isUrlDuplicate = 0;
    else
    //return true;
    $isUrlDuplicate = 1;
    //if is duplicate, add eol to url
    $newURL = $currentURL . '-eol';
    $product->setStoreId(1)->setUrlKey($newURL);
    echo 'setting new url key to ' . $newURL .'<br />';

    echo 'original url key is '.$currentURL . ' is duplicate? ' . ( $isUrlDuplicate > 0 ? 'yes' : 'no') .'<br />';

    //$price = trim($data['price']);
    echo 'Getting product SKU: '.$sku.', with EOL Status: '.$product->getIsEol().'<br />';
    echo 'Updating product SKU: '.$sku.', with EOL Status: '.$status.'('.$option_id.')'.'<br />'; // .' and Price:'.$price.'<br />';
    $product->setIsEol($option_id) // use option_id here
    ->setStoreId(1) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
    ->setCustomStockStatus($status_id)
    ->setMetaDescription('Discontinued')
    ->setVisibility(3) //this line break the script //VISIBILITY_IN_SEARCH VISIBILITY_IN_CATALOG VISIBILITY_BOTH
    //->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED)
    ->save();


    error_log



    [07-Jun-2019 00:12:42 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'digital-to-analog-audio-converter.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID' in /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
    Stack trace:
    #0 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
    #1 /home/mystore/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(93): Zend_Db_Statement_Pdo->_execute(Array)
    #2 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): MagentoFrameworkDBStatementPdoMysql->_execute(Array)
    #3 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
    #4 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `u...', Array)
    #5 /home/mystore/public_h in /home/mystore/public_html/vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php on line 220









    share|improve this question


























      0












      0








      0








      I have this script to update product attributes, everything else run fine until i added this line below of code to update visibility to search only.
      I know i can update it through catalog product grid, but would like to know why this doesn't work.



      ->setVisibility(3)


      or



      ->setVisibility(search_only)


      or



      ->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_IN_SEARCH)


      more details:



       /*check url key*/

      /*holder for new url key in case it's a duplicate*/
      //$name = $product->getName();
      //$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
      //$url = strtolower($url) . 'discontinued';
      $currentURL = $product->getUrlKey();
      $urlKey = $currentURL;
      $storeId = 1;
      //echo $storeManager->getStore()->getStoreId() . '<br />'; //here returned 1
      $isUrlDuplicate;
      $urlKey .= '.html';
      $connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
      $tablename = $connection->getTableName('url_rewrite');
      $sql = $connection->select()->from(
      ['url_rewrite' => $connection->getTableName('url_rewrite')], ['request_path', 'store_id']
      )->joinLeft(
      ['cpe' => $connection->getTableName('catalog_product_entity')], "cpe.entity_id = url_rewrite.entity_id"
      )->where('request_path IN (?)', $urlKey)
      ->where('store_id IN (?)', $storeId)
      ->where('cpe.sku not in (?)', $sku);
      $urlKeyDuplicates = $connection->fetchAssoc($sql);
      if (!empty($urlKeyDuplicates))
      // return false;
      $isUrlDuplicate = 0;
      else
      //return true;
      $isUrlDuplicate = 1;
      //if is duplicate, add eol to url
      $newURL = $currentURL . '-eol';
      $product->setStoreId(1)->setUrlKey($newURL);
      echo 'setting new url key to ' . $newURL .'<br />';

      echo 'original url key is '.$currentURL . ' is duplicate? ' . ( $isUrlDuplicate > 0 ? 'yes' : 'no') .'<br />';

      //$price = trim($data['price']);
      echo 'Getting product SKU: '.$sku.', with EOL Status: '.$product->getIsEol().'<br />';
      echo 'Updating product SKU: '.$sku.', with EOL Status: '.$status.'('.$option_id.')'.'<br />'; // .' and Price:'.$price.'<br />';
      $product->setIsEol($option_id) // use option_id here
      ->setStoreId(1) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
      ->setCustomStockStatus($status_id)
      ->setMetaDescription('Discontinued')
      ->setVisibility(3) //this line break the script //VISIBILITY_IN_SEARCH VISIBILITY_IN_CATALOG VISIBILITY_BOTH
      //->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED)
      ->save();


      error_log



      [07-Jun-2019 00:12:42 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'digital-to-analog-audio-converter.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID' in /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
      Stack trace:
      #0 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
      #1 /home/mystore/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(93): Zend_Db_Statement_Pdo->_execute(Array)
      #2 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): MagentoFrameworkDBStatementPdoMysql->_execute(Array)
      #3 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
      #4 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `u...', Array)
      #5 /home/mystore/public_h in /home/mystore/public_html/vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php on line 220









      share|improve this question
















      I have this script to update product attributes, everything else run fine until i added this line below of code to update visibility to search only.
      I know i can update it through catalog product grid, but would like to know why this doesn't work.



      ->setVisibility(3)


      or



      ->setVisibility(search_only)


      or



      ->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_IN_SEARCH)


      more details:



       /*check url key*/

      /*holder for new url key in case it's a duplicate*/
      //$name = $product->getName();
      //$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
      //$url = strtolower($url) . 'discontinued';
      $currentURL = $product->getUrlKey();
      $urlKey = $currentURL;
      $storeId = 1;
      //echo $storeManager->getStore()->getStoreId() . '<br />'; //here returned 1
      $isUrlDuplicate;
      $urlKey .= '.html';
      $connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
      $tablename = $connection->getTableName('url_rewrite');
      $sql = $connection->select()->from(
      ['url_rewrite' => $connection->getTableName('url_rewrite')], ['request_path', 'store_id']
      )->joinLeft(
      ['cpe' => $connection->getTableName('catalog_product_entity')], "cpe.entity_id = url_rewrite.entity_id"
      )->where('request_path IN (?)', $urlKey)
      ->where('store_id IN (?)', $storeId)
      ->where('cpe.sku not in (?)', $sku);
      $urlKeyDuplicates = $connection->fetchAssoc($sql);
      if (!empty($urlKeyDuplicates))
      // return false;
      $isUrlDuplicate = 0;
      else
      //return true;
      $isUrlDuplicate = 1;
      //if is duplicate, add eol to url
      $newURL = $currentURL . '-eol';
      $product->setStoreId(1)->setUrlKey($newURL);
      echo 'setting new url key to ' . $newURL .'<br />';

      echo 'original url key is '.$currentURL . ' is duplicate? ' . ( $isUrlDuplicate > 0 ? 'yes' : 'no') .'<br />';

      //$price = trim($data['price']);
      echo 'Getting product SKU: '.$sku.', with EOL Status: '.$product->getIsEol().'<br />';
      echo 'Updating product SKU: '.$sku.', with EOL Status: '.$status.'('.$option_id.')'.'<br />'; // .' and Price:'.$price.'<br />';
      $product->setIsEol($option_id) // use option_id here
      ->setStoreId(1) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
      ->setCustomStockStatus($status_id)
      ->setMetaDescription('Discontinued')
      ->setVisibility(3) //this line break the script //VISIBILITY_IN_SEARCH VISIBILITY_IN_CATALOG VISIBILITY_BOTH
      //->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED)
      ->save();


      error_log



      [07-Jun-2019 00:12:42 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'digital-to-analog-audio-converter.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID' in /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
      Stack trace:
      #0 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
      #1 /home/mystore/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(93): Zend_Db_Statement_Pdo->_execute(Array)
      #2 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): MagentoFrameworkDBStatementPdoMysql->_execute(Array)
      #3 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
      #4 /home/mystore/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `u...', Array)
      #5 /home/mystore/public_h in /home/mystore/public_html/vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php on line 220






      magento2 product-attribute programmatically visibility






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 7 at 21:43







      Kris Wen

















      asked Jun 7 at 0:00









      Kris WenKris Wen

      3509




      3509




















          1 Answer
          1






          active

          oldest

          votes


















          1














          The problem is not the ->setVisibility(3) call itself but the creation of an url rewrite entry for that product which is associated with the changing of the visibility. Most likely the product was 'Not Visible Individually' before and had no entry in url_rewrite table.



          If you change the url_key for that product to something unique, the saving operation would work.



          To check if a value used for url_key is unique you can use a method like posted in this answer https://magento.stackexchange.com/a/181898/76597



          I would suggest to implement such a check in your code.






          share|improve this answer























          • Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

            – Kris Wen
            Jun 7 at 18:59











          • I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

            – HelgeB
            Jun 7 at 19:45











          • i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

            – Kris Wen
            Jun 7 at 21:39











          • this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

            – Kris Wen
            Jun 7 at 21:52











          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277540%2fupdate-product-visibility-to-search-only-programmaticallly-product-setvisibil%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









          1














          The problem is not the ->setVisibility(3) call itself but the creation of an url rewrite entry for that product which is associated with the changing of the visibility. Most likely the product was 'Not Visible Individually' before and had no entry in url_rewrite table.



          If you change the url_key for that product to something unique, the saving operation would work.



          To check if a value used for url_key is unique you can use a method like posted in this answer https://magento.stackexchange.com/a/181898/76597



          I would suggest to implement such a check in your code.






          share|improve this answer























          • Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

            – Kris Wen
            Jun 7 at 18:59











          • I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

            – HelgeB
            Jun 7 at 19:45











          • i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

            – Kris Wen
            Jun 7 at 21:39











          • this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

            – Kris Wen
            Jun 7 at 21:52















          1














          The problem is not the ->setVisibility(3) call itself but the creation of an url rewrite entry for that product which is associated with the changing of the visibility. Most likely the product was 'Not Visible Individually' before and had no entry in url_rewrite table.



          If you change the url_key for that product to something unique, the saving operation would work.



          To check if a value used for url_key is unique you can use a method like posted in this answer https://magento.stackexchange.com/a/181898/76597



          I would suggest to implement such a check in your code.






          share|improve this answer























          • Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

            – Kris Wen
            Jun 7 at 18:59











          • I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

            – HelgeB
            Jun 7 at 19:45











          • i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

            – Kris Wen
            Jun 7 at 21:39











          • this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

            – Kris Wen
            Jun 7 at 21:52













          1












          1








          1







          The problem is not the ->setVisibility(3) call itself but the creation of an url rewrite entry for that product which is associated with the changing of the visibility. Most likely the product was 'Not Visible Individually' before and had no entry in url_rewrite table.



          If you change the url_key for that product to something unique, the saving operation would work.



          To check if a value used for url_key is unique you can use a method like posted in this answer https://magento.stackexchange.com/a/181898/76597



          I would suggest to implement such a check in your code.






          share|improve this answer













          The problem is not the ->setVisibility(3) call itself but the creation of an url rewrite entry for that product which is associated with the changing of the visibility. Most likely the product was 'Not Visible Individually' before and had no entry in url_rewrite table.



          If you change the url_key for that product to something unique, the saving operation would work.



          To check if a value used for url_key is unique you can use a method like posted in this answer https://magento.stackexchange.com/a/181898/76597



          I would suggest to implement such a check in your code.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 7 at 7:35









          HelgeBHelgeB

          3,5832423




          3,5832423












          • Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

            – Kris Wen
            Jun 7 at 18:59











          • I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

            – HelgeB
            Jun 7 at 19:45











          • i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

            – Kris Wen
            Jun 7 at 21:39











          • this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

            – Kris Wen
            Jun 7 at 21:52

















          • Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

            – Kris Wen
            Jun 7 at 18:59











          • I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

            – HelgeB
            Jun 7 at 19:45











          • i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

            – Kris Wen
            Jun 7 at 21:39











          • this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

            – Kris Wen
            Jun 7 at 21:52
















          Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

          – Kris Wen
          Jun 7 at 18:59





          Hi, the original visibility is set to catalog/search, and status is set to disabled. I found another enabled product with the same url key. I added the duplicate check in my script, but when seems like ->setUrlkey( $cuurentURL . 'eol') does not save. can you check my updated code? thanks

          – Kris Wen
          Jun 7 at 18:59













          I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

          – HelgeB
          Jun 7 at 19:45





          I'm not sure about that, but your conflict is in store 1 and you check for existing rewrites in store 0. Do you enter the else path in the duplicate check?

          – HelgeB
          Jun 7 at 19:45













          i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

          – Kris Wen
          Jun 7 at 21:39





          i'm a little confusion about the store ids ( since i only have one store). when i getStoreID(), it returned 1. i updated the scripts. Yes, i did enter the into the else path.

          – Kris Wen
          Jun 7 at 21:39













          this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

          – Kris Wen
          Jun 7 at 21:52





          this seems to be the issue i am having with magento 2.2.6, i applied the code from here and seems to able to save the products now. github.com/magento/magento2/issues/7298#issuecomment-463532802

          – Kris Wen
          Jun 7 at 21:52

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277540%2fupdate-product-visibility-to-search-only-programmaticallly-product-setvisibil%23new-answer', 'question_page');

          );

          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







          Popular posts from this blog

          Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

          Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

          Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림