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
18 changes: 17 additions & 1 deletion Controller/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ public function execute()
{
try {

$collection = $this->prepareCollection()->addFieldToFilter('store_id', $this->scopeid);
$collection = $this->prepareCollection();

// Apply store filter if needed (can be overridden by child classes)
if ($this->shouldApplyStoreFilter()) {
$collection->addFieldToFilter('store_id', $this->scopeid);
}

$this->_eventManager->dispatch($this->eventPrefix . '_get_collection_after', [
'controller' => $this,
Expand Down Expand Up @@ -826,4 +831,15 @@ public function addFieldHandler($field, callable $handler)
{
$this->fieldHandlers[$field] = $handler;
}

/**
* Determine if store filter should be applied to collection
* Can be overridden by child classes to customize filtering behavior
*
* @return bool
*/
protected function shouldApplyStoreFilter()
{
return true; // Default behavior: apply store filter
}
}
93 changes: 93 additions & 0 deletions Controller/Order/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,99 @@ public function execute()
}
}

/**
* Prepare collection with multi-store support
*
* @return object|null
* @throws \Magento\Framework\Exception\FileSystemException
*/
protected function prepareCollection()
{
try {
$collection = $this->collectionFactory->create();
$collection->addFieldToSelect('*');

// Check if we should import orders from all stores
$importFromAllStores = $this->scopeConfig->isSetFlag(
Config::XML_PATH_PRODUCT_SYNCHRONIZATION_IMPORT_ORDERS_FROM_ALL_STORES,
$this->scope,
$this->scopeid
);

if ($this->start_date) {
$collection->setPageSize($this->limit)
->setCurPage($this->page)
->addAttributeToFilter('created_at', ['from' => $this->start_date, 'to' => $this->end_date])
->addOrder($this->orderBy, $this->order);
} else {
$collection->setPageSize($this->limit)
->setCurPage($this->page)
->addOrder($this->orderBy, $this->order);
}

// Log the collection strategy being used
if ($importFromAllStores) {
$this->clerk_logger->log('Order Collection: Importing from ALL stores', [
'scope' => $this->scope,
'scope_id' => $this->scopeid,
'import_all_stores' => true
]);
} else {
$this->clerk_logger->log('Order Collection: Importing from CURRENT store only', [
'scope' => $this->scope,
'scope_id' => $this->scopeid,
'import_all_stores' => false
]);
}

return $collection;

} catch (\Exception $e) {
$this->clerk_logger->error('Order prepareCollection ERROR', ['error' => $e->getMessage()]);
}

return null;
}

/**
* Determine if store filter should be applied to order collection
* Checks configuration to decide between single-store or multi-store import
*
* @return bool
*/
protected function shouldApplyStoreFilter()
{
try {
// Check if we should import orders from all stores
$importFromAllStores = $this->scopeConfig->isSetFlag(
Config::XML_PATH_PRODUCT_SYNCHRONIZATION_IMPORT_ORDERS_FROM_ALL_STORES,
$this->scope,
$this->scopeid
);

// If importing from all stores, don't apply store filter
if ($importFromAllStores) {
$this->clerk_logger->log('Order Store Filter: DISABLED (importing from all stores)', [
'scope' => $this->scope,
'scope_id' => $this->scopeid
]);
return false;
}

// Default behavior: apply store filter
$this->clerk_logger->log('Order Store Filter: ENABLED (importing from current store only)', [
'scope' => $this->scope,
'scope_id' => $this->scopeid
]);
return true;

} catch (\Exception $e) {
$this->clerk_logger->error('Order shouldApplyStoreFilter ERROR', ['error' => $e->getMessage()]);
// On error, default to applying store filter for safety
return true;
}
}

/**
* Parse request arguments
*/
Expand Down
1 change: 1 addition & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Config
const XML_PATH_PRODUCT_SYNCHRONIZATION_SALABLE_ONLY = 'clerk/product_synchronization/saleable_only';
const XML_PATH_PRODUCT_SYNCHRONIZATION_VISIBILITY = 'clerk/product_synchronization/visibility';
const XML_PATH_PRODUCT_SYNCHRONIZATION_DISABLE_ORDER_SYNCHRONIZATION = 'clerk/product_synchronization/disable_order_synchronization';
const XML_PATH_PRODUCT_SYNCHRONIZATION_IMPORT_ORDERS_FROM_ALL_STORES = 'clerk/product_synchronization/import_orders_from_all_stores';
const XML_PATH_PRODUCT_SYNCHRONIZATION_IMAGE_TYPE = 'clerk/product_synchronization/image_type';
const XML_PATH_PRODUCT_SYNCHRONIZATION_ENABLE_ORDER_RETURN_SYNCHRONIZATION = 'clerk/product_synchronization/return_order_synchronization';
/**
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# clerk-magento2
Magento 2 extension for Clerk.io

## Multi-Store Order Import

For multi-store setups where you need to import orders from all stores (not just the current store scope), you can enable the "Import orders from all stores" option in the Clerk.io configuration.

### Configuration

1. Go to **Stores > Configuration > Clerk > Synchronization**
2. Set **Import orders from all stores** to **Yes**
3. Save the configuration

### When to Use This Feature

This feature is particularly useful for:
- Multi-store setups with different order prefixes (e.g., B2C_, B2B_, etc.)
- Stores using marketplace integrations like M2E Pro that create orders in different stores
- Situations where you need comprehensive order history across all stores in Clerk.io

### Default Behavior

By default, the extension imports orders only from the current store scope (existing behavior). When the multi-store option is enabled, orders from all stores will be imported regardless of their store_id or order prefix.

### Logging

The extension logs which import strategy is being used to help with troubleshooting:
- "Order Store Filter: ENABLED" - importing from current store only
- "Order Store Filter: DISABLED" - importing from all stores
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<label>Disable order synchronization</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="import_orders_from_all_stores" translate="label comment" type="select" sortOrder="52" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Import orders from all stores</label>
<comment>When enabled, orders from all stores will be imported regardless of store scope. Useful for multi-store setups where you want comprehensive order history.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="image_type" translate="label comment" type="select" sortOrder="55" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Image type</label>
<comment>Select image type to send to Clerk</comment>
Expand Down