Add support for Shared Catalogs

In a Magento B2B (Commerce/Cloud) architecture, product availability and pricing catalogs are partitioned using Shared Catalogs, which restrict or grant visibility dynamically based on a user's assigned customer_group_id.

Magento B2B Shared Catalog visibility rules are not supported out-of-the-box by the native Tweakwise Magento export extension yet.

To bridge this operational gap, developers must implement custom catalog mappings. This article serves as the standard, recommended path to manually extend your store's export database queries and align index structures with active Magento B2B visibility mappings. By exporting these relationships as structured multi-value attributes, search results and lister grids will only output product lists that the active B2B customer has explicit permissions to view.

Export

Copy and paste this prompt into your Agentic IDE (Cursor, VSCode, Windsurf, etc.) or use it as a System Message for your AI Assistant:

Task: Extend our Magento 2 Product Export extension to support B2B Shared Catalog visibility for Tweakwise.

Reference: Please follow the data structure logic described here: https://docs.tweakwise.com/docs/customer-specific-catalog

Objective: For every product in the export, add a new attribute named allow_customer_group. This attribute must contain all Customer Group IDs that have access to that product via Magento’s B2B Shared Catalogs.

Technical Requirements:
1. Performance: Do not query repositories or load product models inside the export loop.

2. Implementation:
- Create a seperate export for the attribute. 
- Check out Model\Export.php to see how the seperate price and stock exports work, especially the product id is important.
- Create a "Visibility Map" before the export starts using a single SQL query on the shared_catalog_product_item and shared_catalog tables.

3. Tweakwise Format:
- According to the documentation link, ensure each group ID is added as a separate <attribute> node with the name allow_customer_group.
Format:
``` xml
<?xml version="1.0"?>
<items xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item>
        <id>0000000361100</id>
        <values>
            <value>99.15858</value>
        </values>
    </item>
    <item>
        <id>000000036110107</id>
        <values>
            <value>99.15858</value>
        </values>
    </item>
</items>```

Constraint: Focus only on visibility. Pricing logic is not needed for this task.

The agent should have added a new export we can configure in Tweakwise.

Configuration

  1. Import the feed, see Import a single attribute.
  2. Configure as request parameter: follow the guide to Configure the attribute as request parameter.
  3. Publish

Frontend

To dynamically serve the correct catalog context to visitors, you must inject their verified customer_group_id as an active query filter parameter in your Tweakwise search requests.

  • Create a custom NavigationRequest that adds the request parameter.
  • Register the new NavigationRequest to actually use the new request object.

If logged in, the allow_customer request parameter will be automatically added.

<?php

namespace Vendor\Module\Model\Client\Request;

use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductNavigationRequest;
use Magento\Customer\Model\Session as CustomerSession;

class CustomProductNavigationRequest extends ProductNavigationRequest
{
    /**
     * @var CustomerSession
     */
    private $customerSession;

    /**
     * CustomProductNavigationRequest constructor.
     *
     * @param CustomerSession $customerSession
     */
    public function __construct(CustomerSession $customerSession)
    {
        $this->customerSession = $customerSession;
        $this->addAllowCustomerParameter();
    }

    /**
     * Add 'allow_customer' parameter if the customer is logged in.
     *
     * @return $this
     */
    public function addAllowCustomerParameter()
    {
        if ($this->customerSession->isLoggedIn()) {
            $userId = $this->customerSession->getCustomerId();
            $this->addHiddenParameter('allow_customer', $userId);
        }

        return $this;
    }
}