Shopware Tricks

From etlam.eu Tech Wiki
Jump to navigationJump to search

DISCLAMER: these "tricks" are provided "as is", without warranty of any kind. These may break your shop, test them thoroughly before using them. You have been warned.

Unless noted otherwise, everything applies to or has been tested on Shopware 6.6

All code snippets may be used freely under the MIT license:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Move orders to a different customer account

This query moves all orders made by a (guest) user to a different customer.

Replace <TARGET CUSTOMER ID> with the customer number of the target account. Replace <GUEST CUSTOMER ID> with the customer number of the guest account.

UPDATE `order_customer`
SET `customer_id` = (SELECT `id` FROM `customer` WHERE `customer_number` = '<TARGET CUSTOMER ID>' ORDER BY `id` DESC LIMIT 1), , `customer_number` = '<TARGET CUSTOMER ID>'
WHERE `order_customer`.`customer_id` = (SELECT `id` FROM `customer` WHERE `customer_number` = '<GUEST CUSTOMER ID>' ORDER BY `id` DESC LIMIT 1);

If you only want to move one order, this query does the trick.

Replace <TARGET CUSTOMER ID> with the customer number of the target account. Replace <ORDER ID> with the order number.

UPDATE `order_customer`
SET `customer_id` = (SELECT `id` FROM `customer` WHERE `customer_number` = '<TARGET CUSTOMER ID>' ORDER BY `id` DESC LIMIT 1), `customer_number` = '<TARGET CUSTOMER ID>'
WHERE `order_customer`.`order_id` = (SELECT `id` FROM `order` WHERE `order_number` = '10016' ORDER BY `<ORDER ID>` DESC LIMIT 1);

Check for missing product media

This query + script checks if product images are missing from the public/media folder.

Step 1: SQL Query - Replace <username> with the Database user, replace <database> with the Database name. Make sure the command is run from within the base folder of the shop.

mysql -u <username> -p -e "use <database>; select p.product_number, m.path from media m left outer join product_media pm on (m.id = pm.media_id) left outer join product p on (pm.product_id = p.id) where p.product_number is not null and m.path is not null;" > sqlresult.txt

This query will generate a list that contains the product number and the media url, separated by a tab. Step 2: Create a .sh file that contains the following:

#!/bin/bash
while read l; do
    IFS=$'\t' read -r -a array <<< "$l"
    path="./public/${array[1]}"
    path2="${path//%20/ }"
    if [ ! -f "$path" ] && [ ! -f "$path2" ]; then
        echo "$l"
    fi
done <sqlresult.txt

Mark the file as executable and run it:

chmod +x compare.sh
./compare.sh

#Optional: save result in file:
./compare.sh > compare_result.txt

This script may not behave properly on files that were uploaded with special characters or a mix of escaped space characters and normal space characters. It is important to sanity check the result by checking if the missing files are actually missing.

Regenerate all thumbnails

The following will remove all thumbnails and force them to be regenerated. This will cause them to not be visible until the process is done.

To regenerate all thumbnails, first clear the media_thumbnail table:

TRUNCATE TABLE media_thumbnail;

Clear the thumbnail folder, cache and then regenerate the thumbnails:

rm -rf public/thumbnail/*
bin/console cache:clear
bin/console media:generate-thumbnails

This process will take a while.