Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Model/Source/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace RunAsRoot\AgenticCommerceProtocol\Model\Source;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Store\Model\Store;

class Category implements OptionSourceInterface
{
public const NONE = 0;
public const SYSTEM_CATEGORY_ID = 0;
public const ROOT_LEVEL = 0;

/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @var RequestInterface
*/
private $request;

/**
* @param CollectionFactory $collectionFactory
* @param RequestInterface $request
*/
public function __construct(
CollectionFactory $collectionFactory,
RequestInterface $request
) {
$this->collectionFactory = $collectionFactory;
$this->request = $request;
}

/**
* Options getter
*
* @return array
*/
public function toOptionArray(): array
{
$optionArray = [];
$arr = $this->toArray();
foreach ($arr as $value => $label) {
$optionArray[] = [
'value' => $value,
'label' => $label
];
}

return $optionArray;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray(): array
{
return $this->getChildren(self::SYSTEM_CATEGORY_ID, self::ROOT_LEVEL);
}

/**
* Get Children option
*
* @param mixed $parentCategoryId
* @param mixed $level
* @return array
*/
private function getChildren(int $parentCategoryId, int $level)
{
$storeId = (int) $this->request->getParam(Store::ENTITY, Store::DEFAULT_STORE_ID);
$options[self::NONE] = __('None');
$collection = $this->collectionFactory->create();
$collection->setStoreId($storeId);
$collection->addAttributeToSelect('name');
$collection->addAttributeToFilter('level', $level);
$collection->addAttributeToFilter('parent_id', $parentCategoryId);
$collection->setOrder('position', SortOrder::SORT_ASC);

foreach ($collection as $category) {
$options[$category->getId()] =
str_repeat(". ", max(0, ($category->getLevel() - 1) * 3)) . $category->getName();
if ($category->hasChildren()) {
$options = array_replace(
$options,
$this->getChildren((int) $category->getId(), $category->getLevel() + 1)
);
}
}

return $options;
}
}
28 changes: 14 additions & 14 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
<tab id="acp" translate="label" sortOrder="400">
<label>Agentic Commerce Protocol</label>
</tab>

<section id="acp" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Configuration</label>
<tab>acp</tab>
<resource>RunAsRoot_AgenticCommerceProtocol::config</resource>

<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Settings</label>

<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Module</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable/disable the ACP module</comment>
</field>

<field id="api_key" translate="label comment" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
Expand All @@ -35,7 +35,7 @@
<field id="enabled">1</field>
</depends>
</field>

<field id="test_mode" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Test Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand All @@ -54,25 +54,25 @@
</depends>
</field>
</group>

<group id="product_feed" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product Feed Settings</label>

<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Product Feed</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable product feed generation for ChatGPT discovery</comment>
</field>

<field id="categories" translate="label comment" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Include Categories</label>
<source_model>Magento\Catalog\Model\Category\Attribute\Source\Categories</source_model>
<source_model>RunAsRoot\AgenticCommerceProtocol\Model\Source\Category</source_model>
<comment>Select categories to include in product feed (leave empty for all)</comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>

<field id="max_products" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Maximum Products</label>
<comment>Maximum number of products to include in feed (default: 1000)</comment>
Expand All @@ -82,16 +82,16 @@
</depends>
</field>
</group>

<group id="webhooks" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Webhook Settings</label>

<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Webhooks</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Send order event notifications to OpenAI</comment>
</field>

<field id="endpoint_url" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Webhook Endpoint URL</label>
<comment>OpenAI webhook endpoint for order events</comment>
Expand All @@ -100,7 +100,7 @@
<field id="enabled">1</field>
</depends>
</field>

<field id="signing_secret" translate="label comment" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Webhook Signing Secret</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
Expand Down