Skip to content

Instantly share code, notes, and snippets.

@magefast
Created December 13, 2022 10:38
Show Gist options
  • Save magefast/6bbc7f7d18566cc1370e0ab3e9805f44 to your computer and use it in GitHub Desktop.
Save magefast/6bbc7f7d18566cc1370e0ab3e9805f44 to your computer and use it in GitHub Desktop.

Revisions

  1. magefast created this gist Dec 13, 2022.
    33 changes: 33 additions & 0 deletions products-from-category-id.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require_once __DIR__ . '/../app/bootstrap.php';

    use Magento\Framework\App\Bootstrap;

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();

    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('adminhtml');

    $collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $collection->create();
    $collection->addAttributeToSelect('entity_id')->addAttributeToSelect(['sku', 'name']);
    $collection->addCategoriesFilter(['in' => [280]]);
    $collection->addAttributeToSelect('category_ids')->addCategoryIds();

    $arrayMedia = [];
    foreach ($collection as $product) {
    if ($product->getStatus() == 1) {
    var_dump($product->getCategoryIds());
    var_dump($product->getName());
    var_dump($product->getSku());

    }
    }
    unset($collection);

    die('--------');