This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
|
||||
namespace App\Helper;
|
||||
|
||||
use App\Entity\Products;
|
||||
use App\Repository\ProductsRepository;
|
||||
|
||||
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use Vin\ShopwareSdk\Client\AdminAuthenticator;
|
||||
use Vin\ShopwareSdk\Client\GrantType\ClientCredentialsGrantType;
|
||||
use Vin\ShopwareSdk\Data\Context;
|
||||
use Vin\ShopwareSdk\Data\Criteria;
|
||||
use Vin\ShopwareSdk\Data\Entity\Order\OrderDefinition;
|
||||
use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
|
||||
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
|
||||
use Vin\ShopwareSdk\Factory\RepositoryFactory;
|
||||
|
||||
@@ -34,8 +37,12 @@ class Shopware
|
||||
try {
|
||||
$grantType = new ClientCredentialsGrantType($_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']);
|
||||
$adminClient = new AdminAuthenticator($grantType, $_ENV['SHOPWARE_API_URL']);
|
||||
|
||||
//dump([$grantType, $adminClient,$_ENV['SHOPWARE_API_ID'], $_ENV['SHOPWARE_API_KEY']]);
|
||||
|
||||
return $adminClient->fetchAccessToken();
|
||||
} catch (\Exception $e) {
|
||||
dump($e->getMessage());
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -70,30 +77,58 @@ class Shopware
|
||||
return $orders->getEntities();
|
||||
}
|
||||
|
||||
public function setProduct(Products $product){
|
||||
/**
|
||||
* @param $prod
|
||||
*/
|
||||
public function setProduct($prod): void
|
||||
{
|
||||
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
|
||||
|
||||
//prüfen ob Shopware Produktid schon bekannt ist
|
||||
if($product->getProductId() == null){
|
||||
if($prod->getShopwareId() == null){
|
||||
$this->logger->error('Shopware Produkt ID ist nicht bekannt');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
|
||||
$productRepository->update($product, $context);
|
||||
}
|
||||
|
||||
public function pushStock()
|
||||
{
|
||||
$productRepository = new ProductsRepository();
|
||||
$products = $productRepository->findAll();
|
||||
dump($prod->getShopwareId());
|
||||
|
||||
|
||||
|
||||
|
||||
foreach ($products as $product) {
|
||||
$this->setProduct($product);
|
||||
try {
|
||||
$productRepository->update([
|
||||
'id' => $prod->getShopwareId(),
|
||||
'stock' => $prod->getStock()->getInStock()
|
||||
], $context);
|
||||
} catch (\ShopwareResponseException $e) {
|
||||
$this->logger->error($e->getResponse());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getShopwareIdbyGtin($gtin)
|
||||
{
|
||||
$context = new Context($_ENV['SHOPWARE_API_URL'], $this->shopwareAuth());
|
||||
|
||||
// Create the repository for the entity
|
||||
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
|
||||
|
||||
// Create the criteria
|
||||
$criteria = new Criteria();
|
||||
$criteria->addFilter(new EqualsFilter('ean', $gtin));
|
||||
|
||||
$products = $productRepository->search($criteria, $context);
|
||||
|
||||
//dump($products->getEntities()->first());
|
||||
|
||||
if($products->count() > 0){
|
||||
$productCollection = $products->getEntities();
|
||||
return $productCollection->first();
|
||||
//$prod->setShopwareId($products->getEntities()->first()->getId());
|
||||
//return $products->getEntities()->first();
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user