How to import products with variations in Shopware 6 - shopware

I'm trying to import products from an XML with variations.
The import for the products works so far but it doesn't create the variations.
Here is my code (simplified):
/**
* #return int
* #throws \Exception
*/
public function execute()
{
// avoid reaching memory limit
ini_set('memory_limit', '-1');
// set tax id
$this->setTaxId();
if (empty($this->taxId)) {
return 1;
}
// read products from import xml file
$importProducts = $this->loadProducts();
$csvBatch = array_chunk($importProducts, self::BATCH);
$productNumbers = [];
foreach ($csvBatch as $products) {
$productNumbers[] = $this->processImportProducts($products, false);
}
$this->deleteProducts(array_merge(...$productNumbers));
return 0;
}
/**
* #param $productsData
* #param $progressBar
* #return array
*/
private function processImportProducts($productsData, $progressBar)
{
$products = [];
$productNumbers = [];
foreach ($productsData as $product) {
$products[$product['SKU']['#cdata']] = $this->importProducts($product, $progressBar);
$productNumbers[] = $product['SKU']['#cdata'];
}
// upsert product
try {
$this->cleanProductProperties($products, $this->context);
$this->productRepository->upsert(array_values($products), $this->context);
} catch (WriteException $exception) {
$this->logger->info(' ');
$this->logger->info('<error>Products could not be imported. Message: '. $exception->getMessage() .'</error>');
}
unset($products);
return $productNumbers;
}
/**
* #param $product
* #param $progressBar
* #return array
*/
private function importProducts($product, $progressBar)
{
...
$productData = [
'id' => $productId,
'productNumber' => $productNumber,
'price' => [
[
'currencyId' => Defaults::CURRENCY,
'net' => !empty($product['net']) ? $product['net'] : 0,
'gross' => !empty($product['net']) ? $product['net'] : 0,
'linked' => true
]
],
'stock' => 99999,
'unit' => [
'id' => '3fff95a8077b4f5ba3d1d2a41cb53fab'
],
'unitId' => '3fff95a8077b4f5ba3d1d2a41cb53fab',
'taxId' => $this->taxId,
'name' => $productNames,
'description' => $productDescriptions
];
if(isset($product['Variations'])) {
$variationIds = $product['Variations']['#cdata'] ?? '';
$productData['variation'] = [$this->getProductVariationIds($variationIds)];
}
return $productData;
}
/**
* Get product variation ids
*
* #param string $productVariations
* #return string
*/
private function getProductVariationIds($productVariations)
{
$productVariationIds = explode(',', $productVariations);
// get product variationIds in form of a string list
$ids = $this->productRepository->search(
(new Criteria())->addFilter(new EqualsAnyFilter('productNumber', $productVariationIds)),
$this->context
)->getIds();
return implode(',', $ids);
}
It loads correctly the ids but nothing happen. Also no error.
Anyone an idea how to import variations as well?

The variation field is not meant to be persisted or to create variants of a product. It has the Runtime flag, meaning it's not an actual database column but processed during runtime.
You have to create/update variants just like you create the parent product. Additionally you have to set the parentId and the options. The latter being associations to property_group_option, which you'll have to create first.
So in addition to your existing payload when creating parent products, you'll have to add this data to the variants:
$productData = [
// ...
'parentId' => '...'
'options' => [
['id' => '...'],
['id' => '...'],
['id' => '...'],
// ...
],
];
Finally you'll have to create the product_configurator_setting records. That's one record for each option used across all variants. Also the productId for the records has to be the one of the parent product.
$repository = $this->container->get('product_configurator_setting.repository');
$configuratorSettings = [];
foreach ($options as $option) {
$configuratorSetting = [
'optionId' => $option['id'],
'productId' => $parentId,
];
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('productId', $parentId));
$criteria->addFilter(new EqualsFilter('optionId', $option['id']));
$id = $repository->searchIds($criteria, $context)->firstId();
// if the configurator setting already exists, update or skip
if ($id) {
$configuratorSetting['id'] = $id;
}
$configuratorSettings[] = $configuratorSetting;
}
$repository->upsert(configuratorSettings, $context);

Just as an addition to make things easier. When creating a product with variants you can just update the configuratorSettings of the parent/father/main-product (whatever you call it).
Then Shopware6 will go and create the variant products automatically. Also the uuids of the children are created automatically. So if need to keep track of these you have to query them after the creation process.
But for a fast creation this might be much faster, if you have a lot of variants the only "variation" are the options. So no special images or texts.

Related

Friends of Cake search: hasmany associations

I seem to recall far better documentation the last time I used it. I need to be able to search across multiple tables, finding Locations which have ShippingAddresses that match search criteria. Here are my Location model showing the association between the Addresses table and Locations table:
class LocationsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('locations');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Search.Search');
// Stores:
$this->belongsTo('Stores', [
'foreignKey' => 'store_id',
]);
// Shipping addresses:
$this->hasMany('ShippingAddresses', [
'foreignKey' => 'location_id',
'className' => 'Addresses',
'dependent' => true,
])->setConditions(['type' => 'shipping']);
// Billing addresses
$this->hasMany('BillingAddresses', [
'foreignKey' => 'location_id',
'className' => 'Addresses',
'dependent' => true,
])->setConditions(['type' => 'billing']);
// Deliverables
$this->belongsToMany('Deliverables', [
'through' => 'LocationsDeliverables',
]);
}
/**
* Default validation rules.
*
* #param \Cake\Validation\Validator $validator Validator instance.
* #return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->scalar('identifier')
->maxLength('identifier', 255)
->allowEmptyString('identifier');
$validator
->scalar('description')
->allowEmptyString('description');
return $validator;
}
/**
* Returns a summarized set of important data.
*
* #param \Cake\ORM\Query $query The current query
* #param array $options An array of options.
* #return \Cake\ORM\Query
*/
public function findSummary(Query $query, array $options): Query
{
// do things.
return $query
->where(['Locations.id' => $options['location_id']])
->contain(
'Deliverables',
function (Query $q) {
return $q
->select(['id', 'parent_id', 'name', 'description'])
->where(['LocationsDeliverables.default_quantity >' => 0]);
}
)
->contain(['ShippingAddresses', 'Stores']);
}
}
Here is the search function call in context in the Locations controller:
/**
* Index method
*
* #return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$this->Authorization->skipAuthorization();
$locations = $this->paginate($this->Locations, [
'contain' => ['Stores', 'ShippingAddresses'],
'finder' => [
'search' => ['search' => $this->request->getQueryParams()],
],
]);
$this->set(compact('locations'));
}
According to the information provided in this comment, you should just be able to add the additional ShippingAddress columns to the search collection in it's dot-noted version. But of course, hasMany associations are queried by CakePHP as a separate operation, so doing so fails:
class LocationsCollection extends FilterCollection
{
/**
* #return void
*/
public function initialize(): void
{
$this->like('name')
->value('identifier')
->value('store_id')
->add('q', 'Search.Like', [
'before' => true,
'after' => true,
'fieldMode' => 'OR',
'comparison' => 'LIKE',
'wildcardAny' => '*',
'wildcardOne' => '?',
'fields' => ['name', 'description', 'Stores.store_name', 'ShippingAddresses.street_1'],
]);
}
}
Using this search, I get the error message "Column not found: 1054 Unknown column 'ShippingAddresses.street_1' in 'where clause'". That makes sense, because that's not how hasMany associations work. But then, how DO I query this data?
Thanks for your help!

In EasyAdmin 3 configureActions method how do I get the current entity?

I wish to add an action to another to the index action with a predefined filter.
To build the filter I need the get current entity in the configureActions method.
public function configureActions(Actions $actions): Actions
{
parent::configureActions($actions);
$adminUrlGenerator = $this->get(AdminUrlGenerator::class);
$url = $adminUrlGenerator
->setController(SiteCrudController::class)
->setAction(Action::INDEX)
->set('filters', [
'agent' => [
'comparison' => '=',
'value' => 2194, // How to get current entity here??
]
])
->generateUrl()
;
$viewRelatesSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice')
->linkToUrl($url)
;
$actions->add(Action::DETAIL, $viewRelatesSites);
$actions->add(Action::EDIT, $viewRelatesSites);
return $actions;
}
}
How can I get the entity here?
To get the current entity the AdminContext is needed.
The best place to get this with the entity set is in a BeforeCrudActionEvent.
final class AgentCrudActionEventListen implements EventSubscriberInterface
{
private AdminUrlGenerator $adminUrlGenerator;
private AdminContextProvider $adminContextProvider;
public function __construct(AdminUrlGenerator $adminUrlGenerator, AdminContextProvider $adminContextProvider)
{
$this->adminUrlGenerator = $adminUrlGenerator;
$this->adminContextProvider = $adminContextProvider;
}
public static function getSubscribedEvents(): array
{
return [
BeforeCrudActionEvent::class => 'onBeforeCrudActionEvent',
];
}
public function onBeforeCrudActionEvent(BeforeCrudActionEvent $event): void
{
$crud = $event->getAdminContext()->getCrud();
if ($crud->getControllerFqcn() !== AgentCrudController::class) {
return;
}
$entity = $this->adminContextProvider->getContext()->getEntity();
if (!$entity) {
return;
}
$url = $this->adminUrlGenerator
->setController(SiteCrudController::class)
->setAction(Action::INDEX)
->set('filters', [
'agent' => [
'comparison' => '=',
'value' => $entity->getPrimaryKeyValue(),
]
])
->generateUrl()
;
$viewRelatedSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice')
->linkToUrl($url)
;
$actions = $crud->getActionsConfig();
$actions->appendAction(Action::DETAIL, $viewRelatedSites->getAsDto());
$actions->appendAction(Action::EDIT, $viewRelatedSites->getAsDto());
}
}
After a day looking for the current entity into my AbstractCrudController, I found this :
$currentEntity = $this->getContext()->getEntity()->getInstance();
Since EasyAdmin 3 events are discouraged per the docs:
Starting from EasyAdmin 3.0 everything is defined with PHP. That's why it's easier to customize backend behavior overloading PHP classes and methods and calling to your own services. However, the events still remain in case you want to use them.
public function configureActions(Actions $actions): Actions
{
// Create your action
$viewRelatedSites = Action::new('viewRelatedSites', 'Sites', 'fa fa-file-invoice');
//set the link using a string or a callable (function like its being used here)
$viewRelatedSites->linkToUrl(function($entity) {
$adminUrlGenerator = $this->get(AdminUrlGenerator::class);
$url = $adminUrlGenerator
->setController(SiteCrudController::class)
->setAction(Action::INDEX)
->set('filters', [
'agent' => [
'comparison' => '=',
'value' => $entity->getId()
]
])
->generateUrl();
return $url;
});
$actions->add(Crud::PAGE_INDEX, $viewRelatedSites);
return $actions;
}

GuzzleHttp Parallel Progress For Async Client in Azure and Flysystem

I would like to get the actual block progress and not the Progress of all the transfers. Currently i don't know how to detect the blockId of each individual transfer. The information on the progress callback im currently retrieving is pointless.
Here's the progress function, contained within ServiceRestProxy.php
Original Function https://github.com/Azure/azure-storage-php/blob/master/azure-storage-common/src/Common/Internal/ServiceRestProxy.php#L99
/**
* Create a Guzzle client for future usage.
*
* #param array $options Optional parameters for the client.
*
* #return Client
*/
private static function createClient(array $options)
{
$verify = true;
//Disable SSL if proxy has been set, and set the proxy in the client.
$proxy = getenv('HTTP_PROXY');
// For testing with Fiddler
// $proxy = 'localhost:8888';
// $verify = false;
if (!empty($proxy)) {
$options['proxy'] = $proxy;
}
if (!empty($options['verify'])) {
$verify = $options['verify'];
}
$downloadTotal = 0;
return (new \GuzzleHttp\Client(
array_merge(
$options,
array(
"defaults" => array(
"allow_redirects" => true,
"exceptions" => true,
"decode_content" => true,
),
'cookies' => true,
'verify' => $verify,
'progress' => function (
$downloadTotal,
$downloadedBytes,
$uploadTotal,
$uploadedBytes
){
// i need to detect which block the progress is for.
echo ("progress: download: {$downloadedBytes}/{$downloadTotal}, upload: {$uploadedBytes}/{$uploadTotal}");
}
)
)
));
}
I got a solution to get each block progress.
I needed to use the Async Function for this. Updated version.
/**
* Send the requests concurrently. Number of concurrency can be modified
* by inserting a new key/value pair with the key 'number_of_concurrency'
* into the $requestOptions of $serviceOptions. Return only the promise.
*
* #param callable $generator the generator function to generate
* request upon fulfillment
* #param int $statusCode The expected status code for each of the
* request generated by generator.
* #param ServiceOptions $options The service options for the concurrent
* requests.
*
* #return \GuzzleHttp\Promise\Promise|\GuzzleHttp\Promise\PromiseInterface
*/
protected function sendConcurrentAsync(
callable $generator,
$statusCode,
ServiceOptions $options
) {
$client = $this->client;
$middlewareStack = $this->createMiddlewareStack($options);
$progress = [];
$sendAsync = function ($request, $options) use ($client, $progress) {
if ($request->getMethod() == 'HEAD') {
$options['decode_content'] = false;
}
$options["progress"] = function(
$downloadTotal,
$downloadedBytes,
$uploadTotal,
$uploadedBytes) use($request, $progress){
// extract blockid from url
$url = $request->getUri()->getQuery();
parse_str($url, $array);
// this array can be written to file or session etc
$progress[$array["blockid"]] = ["download_total" => $downloadTotal, "downloaded_bytes" => $downloadedBytes, "upload_total" => $uploadTotal, "uploaded_bytes" => $uploadedBytes];
};
return $client->sendAsync($request, $options);
};
$handler = $middlewareStack->apply($sendAsync);
$requestOptions = $this->generateRequestOptions($options, $handler);
$promises = \call_user_func(
function () use (
$generator,
$handler,
$requestOptions
) {
while (is_callable($generator) && ($request = $generator())) {
yield \call_user_func($handler, $request, $requestOptions);
}
}
);
$eachPromise = new EachPromise($promises, [
'concurrency' => $options->getNumberOfConcurrency(),
'fulfilled' => function ($response, $index) use ($statusCode) {
//the promise is fulfilled, evaluate the response
self::throwIfError(
$response,
$statusCode
);
},
'rejected' => function ($reason, $index) {
//Still rejected even if the retry logic has been applied.
//Throwing exception.
throw $reason;
}
]);
return $eachPromise->promise();
}

Acceptable to save results from Spotify search API?

Reading the TOS for the Spotify web API, developers are not allowed to aggregate data from the API in creation of databases. I don't know if what I'm trying to accomplish counts as "aggregation."
I have a website that allows users to make suggestions for songs to be played at a wedding. I let them put in song name, artist, and album names so that the DJ can readily find the music. All this is user supplied. The songs are then approved by the bride/groom and voted on by other guests to generate a playlist for the DJ will know what music will be popular at the event.
What I want to provide is a way for the user to use that information and have the ability to search through the top few search results on Spotify, select the right track, and associate the Spotify track with their suggestion. This let's other guests hear the song they are suggesting if they're not familiar with it and allows the administrators to allow or disallow the song depending on the tastes of the bride/groom.
In order to avoid API calls exceeding the rate limit, I'd like to be able to store the Spotify URI returned from the search results with the user supplied song information so that I can generate a play button on the site for the songs suggested.
Does this count as aggregation, or would this be allowed under the current TOS for the web search API?
What you're doing sounds just fine.
The TOS section you're asking about is to prevent people making automated tools that scrape the Spotify catalogue without user interaction. If you're writing a "normal" application and caching data from the Spotify APIs as a result of a user actually doing something like searching, browsing, etc etc you have no problems.
Source: I work at Spotify.
I use this one:
<?php
namespace App\Services;
use DB;
use Exception;
use App\Genre;
use App\Album;
use App\Artist;
use Illuminate\Support\Str;
class ArtistSaver {
/**
* Save artist to database and return it.
*
* #param array $data
* #return Artist
*/
public function save($data)
{
$artist = Artist::whereName($data['mainInfo']['name'])->first();
if ( ! $artist) {
$artist = Artist::create($data['mainInfo']);
} else {
$artist->fill($data['mainInfo'])->save();
}
$this->saveAlbums($data, $artist);
if (isset($data['albums'])) {
$this->saveTracks($data['albums'], $artist);
}
if (isset($data['similar'])) {
$this->saveSimilar($data['similar'], $artist);
}
if (isset($data['genres']) && ! empty($data['genres'])) {
$this->saveGenres($data['genres'], $artist);
}
return $artist;
}
/**
* Save and attach artist genres.
*
* #param array $genres
* #param Artist $artist
*/
public function saveGenres($genres, $artist) {
$existing = Genre::whereIn('name', $genres)->get();
$ids = [];
foreach($genres as $genre) {
$dbGenre = $existing->filter(function($item) use($genre) { return $item->name === $genre; })->first();
//genre doesn't exist in db yet, so we need to insert it
if ( ! $dbGenre) {
try {
$dbGenre = Genre::create(['name' => $genre]);
} catch(Exception $e) {
continue;
}
}
$ids[] = $dbGenre->id;
}
//attach genres to artist
$artist->genres()->sync($ids, false);
}
/**
* Save artists similar artists to database.
*
* #param $similar
* #param $artist
* #return void
*/
public function saveSimilar($similar, $artist)
{
$names = array_map(function($item) { return $item['name']; }, $similar);
//insert similar artists that don't exist in db yet
$this->saveOrUpdate($similar, array_flatten($similar), 'artists');
//get ids in database for artist we just inserted
$ids = Artist::whereIn('name', $names)->lists('id');
//attach ids to given artist
$artist->similar()->sync($ids);
}
/**
* Save artist albums to database.
*
* #param array $data
* #param Artist|null $artist
* $param int|null
* #return void
*/
public function saveAlbums($data, $artist = null, $albumId = null)
{
if (isset($data['albums']) && count($data['albums'])) {
$b = $this->prepareAlbumBindings($data['albums'], $artist, $albumId);
$this->saveOrUpdate($b['values'], $b['bindings'], 'albums');
}
}
/**
* Save albums tracks to database.
*
* #param array $albums
* #param Artist|null $artist
* #param Album|null $trackAlbum
* #return void
*/
public function saveTracks($albums, $artist, $tracksAlbum = null)
{
if ( ! $albums || ! count($albums)) return;
$tracks = [];
foreach($albums as $album) {
if ( ! isset($album['tracks']) || empty($album['tracks'])) continue;
if ($tracksAlbum) {
$id = $tracksAlbum['id'];
} else {
$id = $this->getIdFromAlbumsArray($album['name'], $artist['albums']);
}
foreach($album['tracks'] as $track) {
$track['album_id'] = $id;
$tracks[] = $track;
}
}
if ( ! empty($tracks)) {
$this->saveOrUpdate($tracks, array_flatten($tracks), 'tracks');
}
}
private function getIdFromAlbumsArray($name, $albums) {
$id = false;
foreach($albums as $album) {
if ($name === $album['name']) {
$id = $album['id']; break;
}
}
if ( ! $id) {
foreach($albums as $album) {
if (Str::slug($name) == Str::slug($album['name'])) {
$id = $album['id']; break;
}
}
}
return $id;
}
/**
* Unset tracks key from album arrays and flatten them into single array.
*
* #param array $albums
* #param Artist|null $artist
* #param int|null $albumId
* #return array
*/
private function prepareAlbumBindings($albums, $artist = null, $albumId = null)
{
$flat = [];
foreach($albums as $k => $album) {
if (isset($albums[$k]['tracks'])) unset($albums[$k]['tracks']);
if ( ! isset($albums[$k]['artist_id']) || ! $albums[$k]['artist_id']) {
$albums[$k]['artist_id'] = $artist ? $artist->id : 0;
}
//can't insert null into auto incrementing id because
//mysql will increment the id instead of keeping the old one
if ($albumId) {
$albums[$k]['id'] = $albumId;
}
foreach($albums[$k] as $name => $data) {
if ($name !== 'tracks') {
$flat[] = $data;
}
}
}
return ['values' => $albums, 'bindings' => $flat];
}
/**
* Compiles insert on duplicate update query for multiple inserts.
*
* #param array $values
* #param array $bindings
* #param string $table
*
* #return void
*/
public function saveOrUpdate(array $values, $bindings, $table)
{
if (empty($values)) return;
$first = head($values);
//count how many inserts we need to make
$amount = count($values);
//count in how many columns we're inserting
$columns = array_fill(0, count($first), '?');
$columns = '(' . implode(', ', $columns) . ') ';
//make placeholders for the amount of inserts we're doing
$placeholders = array_fill(0, $amount, $columns);
$placeholders = implode(',', $placeholders);
$updates = [];
//construct update part of the query if we're trying to insert duplicates
foreach ($first as $column => $value) {
$updates[] = "$column = COALESCE(values($column), $column)";
}
$prefixed = DB::getTablePrefix() ? DB::getTablePrefix().$table : $table;
$query = "INSERT INTO {$prefixed} " . '(' . implode(',' , array_keys($first)) . ')' . ' VALUES ' . $placeholders .
'ON DUPLICATE KEY UPDATE ' . implode(', ', $updates);
DB::statement($query, $bindings);
}
}

drupal 7 custom content hook_theme output

I have a custom module created in Drupal 7 and I want it to display some HTML content. Here is how I have did.
But it is not working, what I do wrong?
<?php
/**
* Implements hook_block_info().
*/
function submenus_block_info() {
$blocks = array();
$blocks['info'] = array(
'info' => t('The submenu zone')
);
return $blocks;
}
/**
* Implements hook_block_view().
*
*/
function submenus_block_view($delta = '') {
$block = array();
$users = "edf";
$title = "sdfsd";
$block['subject'] = t('Submenu');
$block['content'] = theme('submenus_output', array('users' => $users, 'title' => $title));
return $block;
}
/**
* Implement hook_theme()
*/
function submenus_theme() {
return array(
'submenus_output' => array(
'variables' => array('users' => NULL, 'title' => NULL),
),
);
}
/**
* Display output
*/
function theme_submenus_output($somearray) {
$content = '<div>TEST</div>';
return $content;
}
?>
I checked, there is nothing wrong with that code: the new block is available in the list of blocks, and if you assign it to a region, the block is called and the code from the custom theme function is displayed.
So you could try these things:
in Administration > Configuration > Development > Performance, clear the caches
in Administration > Structure > Blocks, make sure the block is assigned to a region that exists (such as "Content") and if it is, click the "Configure" link to see if there is a filter that prevents it from being displayed.

Resources