Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect category listing #820

Merged
merged 24 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
05210b1
Implement category export
dairbuirabass Feb 9, 2023
980a89f
Fix category tagging
dairbuirabass Feb 10, 2023
e8f5e37
Remove reference to the old CategoryCollection
dairbuirabass Feb 10, 2023
6460c7c
Update Category model path
dairbuirabass Feb 13, 2023
da4e05a
Update nosto/php-sdk to version 7.0.0
dairbuirabass Feb 21, 2023
4712f98
Create category save observer
dairbuirabass Mar 2, 2023
728832c
Merge remote-tracking branch 'origin/develop' into feature/collect-ca…
supercid May 29, 2023
f5a1416
Fix return type category
supercid May 29, 2023
0d99d78
Merge remote-tracking branch 'origin/develop' into feature/collect-ca…
supercid May 29, 2023
ee7b636
Update php-sdk to 7.0
supercid May 29, 2023
67d31d3
Update php-sdk to 7.0
supercid May 29, 2023
68c3299
Update changelog
supercid May 29, 2023
2a01af0
Fix type, remove getCategoryVisibleInMenu
supercid May 29, 2023
d39dec1
temp sdk-branch
supercid May 29, 2023
d84340e
Remove unused imports, pass correct id to setProductStoreId
supercid May 29, 2023
7b17150
Fix return types, add types in CategoryCollectionBuilder
supercid May 29, 2023
ba44234
Update nosto SDK to 7.1.0
supercid May 29, 2023
58dc139
Set category availabel for NostoCategory in builder
phonglynosto Jun 14, 2023
b5b6a1e
Add categoryString for NostoCategory in Builder
phonglynosto Jun 15, 2023
8de0bfa
Use the branch dev-hotfix/fix-update-category
Jun 15, 2023
d82dba6
Merge remote-tracking branch 'origin/develop' into feature/collect-ca…
phonglynosto Jun 27, 2023
82e350b
Merge remote-tracking branch 'origin/develop' into feature/collect-ca…
supercid Aug 24, 2023
40d536e
Bump version
supercid Aug 24, 2023
fe2ae58
remove unused Model/ResourceModel/Magento/Category/Collection
supercid Aug 29, 2023
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
10 changes: 4 additions & 6 deletions Block/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Nosto\Model\Category as NostoCategory;
use Nosto\Model\Category\Category as NostoCategory;
use Nosto\Tagging\Helper\Account as NostoHelperAccount;
use Nosto\Tagging\Helper\Scope as NostoHelperScope;
use Nosto\Tagging\Model\Category\Builder as NostoCategoryBuilder;
Expand Down Expand Up @@ -104,7 +104,7 @@ public function __construct(
/**
* Returns the current category as a slash delimited string
*
* @return string|null the current category as a slash delimited string
* @return NostoCategory|null the current category as a slash delimited string
*/
private function getNostoCategory()
{
Expand All @@ -120,12 +120,10 @@ private function getNostoCategory()
/**
* Returns the HTML to render categories
*
* @return NostoCategory
* @return NostoCategory|null
*/
public function getAbstractObject()
{
$category = new NostoCategory();
$category->setCategoryString($this->getNostoCategory());
return $category;
return $this->getNostoCategory();
}
}
13 changes: 0 additions & 13 deletions Block/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
* This meta-data is sent to Nosto via JavaScript when users are browsing the
* pages in the store.
*/
class Product extends View

Check warning on line 63 in Block/Product.php

View workflow job for this annotation

GitHub Actions / Mess Detect

The class Product has a coupling between objects value of 18. Consider to reduce the number of dependencies under 13.
{
use TaggingTrait {
TaggingTrait::__construct as taggingConstruct; // @codingStandardsIgnoreLine
Expand Down Expand Up @@ -142,19 +142,6 @@
);
}

/**
* Returns the Nosto category DTO.
*
* @return string|null the current category as a slash-delimited string
*/
public function getNostoCategory()
{
/** @phan-suppress-next-line PhanDeprecatedFunction */
$category = $this->_coreRegistry->registry('current_category');
$store = $this->nostoHelperScope->getStore();
return $category !== null ? $this->categoryBuilder->build($category, $store) : null;
}

/**
* Formats a price e.g. "1234.56".
*
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

### 7.3.0
* Update tagging with category listing support
* Add category export
### 7.2.6
* Convert price in cart tagging item to avoid rounding errors

Expand Down
99 changes: 99 additions & 0 deletions Controller/Export/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright (c) 2023, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Controller\Export;

use Magento\Framework\App\Action\Context;
use Magento\Store\Model\Store;
use Nosto\Model\AbstractCollection;
use Nosto\Model\Category\CategoryCollection;
use Nosto\NostoException;
use Nosto\Tagging\Helper\Account as NostoHelperAccount;
use Nosto\Tagging\Helper\Scope as NostoHelperScope;
use Nosto\Tagging\Model\Category\CollectionBuilder;

/**
* Category export controller used to export category tree to Nosto.
* This controller will be called by Nosto when a new account has been created
* from the Magento backend. The controller is public, but the information is
* encrypted with AES, and only Nosto can decrypt it.
*/
class Category extends Base
{
public const PARAM_PREVIEW = 'preview';

/** @var CollectionBuilder */
private CollectionBuilder $nostoCollectionBuilder;

/**
* @param Context $context
* @param NostoHelperScope $nostoHelperScope
* @param NostoHelperAccount $nostoHelperAccount
* @param CollectionBuilder $collectionBuilder
*/
public function __construct(
Context $context,
NostoHelperScope $nostoHelperScope,
NostoHelperAccount $nostoHelperAccount,
CollectionBuilder $collectionBuilder
) {
parent::__construct($context, $nostoHelperScope, $nostoHelperAccount);
$this->nostoCollectionBuilder = $collectionBuilder;
}

/**
* @param Store $store
* @param int $limit
* @param int $offset
* @return AbstractCollection|CategoryCollection
* @throws NostoException
*/
public function buildExportCollection(Store $store, int $limit = 100, int $offset = 0)
Copy link
Member

@supercid supercid Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
return $this->nostoCollectionBuilder->buildMany($store, $limit, $offset);
}

/**
* @param Store $store
* @param $id
* @return AbstractCollection|CategoryCollection
* @throws NostoException
*/
public function buildSingleExportCollection(Store $store, $id)
{
return $this->nostoCollectionBuilder->buildSingle($store, $id);
}
}
50 changes: 31 additions & 19 deletions Model/Category/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,46 @@
namespace Nosto\Tagging\Model\Category;

use Exception;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Nosto\Model\Category as NostoCategory;
use Nosto\Model\Category\Category as NostoCategory;
use Nosto\Tagging\Logger\Logger as NostoLogger;
use Nosto\Tagging\Model\Service\Product\Category\CategoryServiceInterface as NostoCategoryService;

class Builder
{
private NostoLogger $logger;
/** @var CategoryRepositoryInterface */
private CategoryRepositoryInterface $categoryRepository;

/** @var ManagerInterface */
private ManagerInterface $eventManager;

/** @var NostoCategoryService */
private NostoCategoryService $nostoCategoryService;

/** @var NostoLogger */
private NostoLogger $logger;

/**
* Builder constructor.
* @param NostoLogger $logger
* @param CategoryRepositoryInterface $categoryRepository
* @param ManagerInterface $eventManager
* @param NostoCategoryService $nostoCategoryService
* @param NostoLogger $logger
*/
public function __construct(
NostoLogger $logger,
CategoryRepositoryInterface $categoryRepository,
ManagerInterface $eventManager,
NostoCategoryService $nostoCategoryService
NostoCategoryService $nostoCategoryService,
NostoLogger $logger
) {
$this->logger = $logger;
$this->categoryRepository = $categoryRepository;
$this->eventManager = $eventManager;
$this->nostoCategoryService = $nostoCategoryService;
$this->logger = $logger;
}

/**
Expand All @@ -77,18 +90,16 @@ public function build(Category $category, Store $store)
try {
$nostoCategory->setId($category->getId());
$nostoCategory->setParentId($category->getParentId());
$nostoCategory->setImageUrl($category->getImageUrl());
$nostoCategory->setLevel($category->getLevel());
$nostoCategory->setTitle($this->getCategoryNameById($category->getId(), $store->getId()));
$path = $this->nostoCategoryService->getCategory($category, $store);
$nostoCategory->setPath($path);
$nostoCategory->setCategoryString($path);
$nostoCategory->setUrl($category->getUrl());
$nostoCategory->setVisibleInMenu($this->getCategoryVisibleInMenu($category));
$nostoCategory->setCategoryString(
$this->nostoCategoryService->getCategory($category, $store)
);
$nostoCategory->setName($category->getName());
$nostoCategory->setAvailable($category->getIsActive() ?? false);
supercid marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception $e) {
$this->logger->exception($e);
}
if (empty($nostoCategory)) {
if (empty($nostoCategory->getId())) {
$nostoCategory = null;
} else {
$this->eventManager->dispatch(
Expand All @@ -101,12 +112,13 @@ public function build(Category $category, Store $store)
}

/**
* @param Category $category
* @return bool
* @param int $id
* @param int $storeId
* @return string
* @throws NoSuchEntityException
*/
private function getCategoryVisibleInMenu(Category $category)
private function getCategoryNameById(int $id, int $storeId)
{
$visibleInMenu = $category->getIncludeInMenu();
return $visibleInMenu === "1";
return $this->categoryRepository->get($id, $storeId)->getName();
}
}
Loading
Loading