Skip to content

Instantly share code, notes, and snippets.

@htuscher
Created February 5, 2021 09:10
Show Gist options
  • Save htuscher/8e0d831cd0b1d94ca35d391c036f320e to your computer and use it in GitHub Desktop.
Save htuscher/8e0d831cd0b1d94ca35d391c036f320e to your computer and use it in GitHub Desktop.
Fix WooCommerce B2B Market Prices after import on variants
<?php
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'dbuser');
/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');
/** MySQL hostname */
define('DB_HOST', 'localhost');
$pdo = new PDO('mysql:host=' . DB_HOST . ';port=3306;dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
$query = 'SELECT p.ID, p.post_title FROM wpeup_posts p WHERE post_parent = 0 AND post_type = \'product\' AND id > 44336';
$stmt = $pdo->prepare($query);
$stmt->execute();
$variantStmt = $pdo->prepare('SELECT id FROM wpeup_posts WHERE post_type = ? AND post_parent = ?');
$findStmt = $pdo->prepare('SELECT meta_key, meta_value FROM wpeup_postmeta WHERE post_id = ? AND meta_key = ?');
$insertStmt = $pdo->prepare('INSERT INTO wpeup_postmeta (post_id, meta_key, meta_value) VALUES (?, ?, ?), (?, ?, ?)');
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $product) {
echo 'Checking product "' . $product['post_title'] . '"(' . $product['ID'] . ')' . PHP_EOL;
$variantStmt->execute(['product_variation', $product['ID']]);
$variantIds = $variantStmt->fetchAll(PDO::FETCH_COLUMN, 0);
if (empty($variantIds)) {
continue;
}
foreach ($variantIds as $variantId) {
$expectedProductPriceMeta = [];
$expectedProductPriceMeta['bm_guest'] = 'bm_guest_' . $variantId . '_price';
$expectedProductPriceMeta['bm_zuechter'] = 'bm_zuechter_' . $variantId . '_price';
$expectedProductPriceMeta['bm_haendler'] = 'bm_haendler_' . $variantId . '_price';
$expectedProductPriceMeta['bm_customer'] = 'bm_customer_' . $variantId . '_price';
foreach ($expectedProductPriceMeta as $group => $key) {
$findStmt->execute([$product['ID'], $key]);
if ($findStmt->fetchColumn(0)) {
unset($expectedProductPriceMeta[$group]);
}
}
if (empty($expectedProductPriceMeta)) {
continue;
}
echo ' Missing Meta for variant ' . $variantId . ': ' . implode(',', $expectedProductPriceMeta) . PHP_EOL;
foreach ($expectedProductPriceMeta as $group => $key) {
$findStmt->execute([$variantId, $group . '_price']);
$price = str_replace(',', '.', $findStmt->fetchColumn(1));
if (!$price) {
echo ' No price found for variant ' . $variantId . ' and key ' . $key . PHP_EOL;
continue;
}
$insertStmt->execute([$product['ID'], $key, $price, $product['ID'], $key . '_type', 'fix']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment