woocommerce migrated logging from logfile to logger

This commit is contained in:
OpenXE 2024-09-14 12:44:21 +02:00
parent d494778fad
commit 0b807455ca

View File

@ -16,10 +16,10 @@
use Xentral\Components\Http\JsonResponse;
use Xentral\Modules\Onlineshop\Data\OrderStatus;
use Xentral\Modules\Onlineshop\Data\OrderStatusUpdateRequest;
use Xentral\Components\Logger\Logger;
class Shopimporter_Woocommerce extends ShopimporterBase
{
// protected $canexport = false;
public $intern = false;
@ -55,16 +55,17 @@ class Shopimporter_Woocommerce extends ShopimporterBase
*/
protected $app;
protected $dump;
/** @var Logger $logger */
public $logger;
public function __construct($app, $intern = false)
{
$this->app=$app;
$this->intern = true;
$this->logger = $app->Container->get('Logger');
}
public function ImportList()
{
$msg = $this->app->erp->base64_url_encode('<div class="info">Sie k&ouml;nnen hier die Shops einstellen</div>');
@ -72,8 +73,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
exit;
}
/**
* This function returns the number of orders which have not yet been imported
*/
@ -83,7 +82,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
// We set per_page to 100 - this could lead to a situation where there are more than
// 100 new Orders, but we still only return 100.
// Array containing additional settings, namely 'ab_nummer' (containting the next order number to get)
// and 'holeallestati' (an integer)
$tmp = $this->CatchRemoteCommand('data');
@ -97,7 +95,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
if ($number_from) {
// Number-based import is selected
// The WooCommerce API doenst allow for a proper "greater than id n" request.
// we fake this behavior by creating an array that contains 'many' (~ 1000) consecutive
// ids that are greater than $from_number and use this array with the 'include' property
@ -115,7 +112,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
'include' => implode(",",$fakeGreaterThanIds),
]);
} else {
// fetch posts by status
@ -129,7 +125,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return (!empty($pendingOrders)?count($pendingOrders):0);
}
/**
* Calling this function queries the api for pending orders and returns them
* as an array.
@ -141,7 +136,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
*/
public function ImportGetAuftrag()
{
// Array containing additional settings, namely 'ab_nummer' (containting the next order number to get)
// and 'holeallestati' (an integer)
$tmp = $this->CatchRemoteCommand('data');
@ -155,7 +149,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
if ($number_from) {
// Number-based import is selected
// The WooCommerce API doenst allow for a proper "greater than id n" request.
// we fake this behavior by creating an array that contains 'many' (~ 1000) consecutive
// ids that are greater than $from_number and use this array with the 'include' property
@ -175,7 +168,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
'orderby' => 'id'
]);
} else {
// fetch posts by status
@ -188,7 +180,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
// Return an empty array in case there are no orders to import
if ((!empty($pendingOrders)?count($pendingOrders):0) === 0) {
return null;
@ -214,10 +205,8 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return $tmp;
}
// This function searches the wcOrder for the specified WC Meta key
// and returns it if found, null otherise
public function get_wc_meta($wcOrder, $meta_key) {
@ -231,15 +220,11 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return $value;
}
// Parse the given WooCommerce order, return a Xentral array-represented order.
// Overload this method whenever additional attributes are required.
public function parseOrder($wcOrder) {
$order = array();
$order['auftragsdaten'] = $wcOrder;
@ -273,7 +258,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
}
if(!empty($wcOrder->subshop)){
$order['subshop'] = $wcOrder->subshop;
}
@ -302,7 +286,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
//
//
// Coupon Codes
//
@ -333,7 +316,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$seperateShippingAddress = !self::compareObjects(
$wcOrder->billing,
$wcOrder->shipping,
@ -360,7 +342,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$order['lieferadresse_land'] = $wcOrder->shipping->country;
}
// VAT stuff
$vatId = $this->get_wc_meta($wcOrder, "_billing_ustid");
@ -375,7 +356,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$order['zahlungsweise'] = $wcOrder->payment_method;
$order['lieferung'] = $wcOrder->shipping_lines[0]->method_id;
return $order;
}
@ -417,7 +397,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return $orderItem;
}
/**
* Sets the Order status to processing, meaning we've successfully imported
* the order into our DB. This prevents the order from beeing imported again.
@ -432,7 +411,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
]);
}
return 'ok';
}
@ -455,7 +433,13 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$this->client->post('orders/'.$data->orderId.'/notes', [
'note' => 'Tracking Code: ' . $trackingCode
]);
$this->WooCommerceLog("Tracking Code Rückmeldung für Auftrag: $data->orderId", $trackingCode);
$this->logger->info("WooCommerce Tracking Code Rückmeldung für Auftrag: ".$data->orderId,
[
'orderId' => $data->orderId,
'trackingCode' => $trackingCode
]
);
}
$updateData = [
@ -472,12 +456,16 @@ class Shopimporter_Woocommerce extends ShopimporterBase
],
];
$this->client->put('orders/'.$data->orderId, $updateData);
$this->WooCommerceLog("Statusrückmeldung 'completed' für Auftrag: $data->orderId", $this->statusCompleted );
$this->logger->info("WooCommerce Statusrückmeldung 'completed' für Auftrag: ".$data->orderId,
[
'orderId' => $data->orderId,
'status' => $this->statusCompleted
]
);
return 'ok';
}
/**
* This function syncs the current stock to the remote WooCommerce shop
* @return int
@ -524,8 +512,8 @@ class Shopimporter_Woocommerce extends ShopimporterBase
if (empty($remoteIdInformation['id'])) {
// The online shop doesnt know this article, write to log and continue with next product
$this->WooCommerceLog("Artikel $nummer wurde im Online-Shop nicht gefunden! Falsche Artikelnummer im Shop hinterlegt?");
$this->logger->error("WooCommerce Artikel $nummer wurde im Online-Shop nicht gefunden! Falsche Artikelnummer im Shop hinterlegt?");
continue;
}
@ -541,21 +529,19 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}else{
$result = $this->client->put('products/' . $remoteIdInformation['id'], $updateProductParams);
}
$this->WooCommerceLog("WooCommerce Lagerzahlenübertragung für Artikel: $nummer / $remoteIdInformation[id] - Anzahl: $lageranzahl", $result);
$this->logger->error("WooCommerce Lagerzahlenübertragung für Artikel: $nummer / $remoteIdInformation[id] - Anzahl: $lageranzahl",
[
'result' => $result
]
);
$anzahl++;
}
return $anzahl;
}
public function ImportStorniereAuftrag() {
$orderId = $this->CatchRemoteCommand('data')['auftrag'];
if (!empty($orderId)) {
$this->client->put('orders/'.$orderId, [
'status' => 'cancelled',
@ -563,22 +549,13 @@ class Shopimporter_Woocommerce extends ShopimporterBase
} else {
return 'failed';
}
return 'ok';
}
public function ImportSendList() {
$tmp = $this->catchRemoteCommand('data');
$anzahl = 0;
for($i=0;$i<(!empty($tmp)?count($tmp):0);$i++){
$artikel = $tmp[$i]['artikel'];
$nummer = $tmp[$i]['nummer'];
if(!empty($tmp[$i]['artikelnummer_fremdnummern'][0]['nummer'])){
@ -604,7 +581,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$dim_width = $tmp[$i]['breite'];
$dim_height = $tmp[$i]['hoehe'];
// Sanitize dimensions
if (self::emptyString($weight_kg))
$weight_kg = null;
@ -619,11 +595,9 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$dim_height = null;
$meta_desc = $tmp[$i]['metadescription_de'];
$meta_title = $tmp[$i]['metatitle_de'];
$pseudopreis = $tmp[$i]['pseudopreis'];//*1.19;
if($pseudopreis <= $preis)$pseudopreis = $preis;
$steuersatz = $tmp[$i]['steuersatz'];
@ -657,7 +631,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
];
// Attributes that are used for both updating an existing product as well as creating a new one
$commonProductAtts = [
'name' => $name_de,
@ -675,7 +648,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
'meta_data' => $commonMetaData,
];
if($lageranzahl===0){
$commonProductAtts['stock_status'] = 'outofstock';
$commonProductAtts['manage_stock'] = true;
@ -693,30 +665,22 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$commonProductAtts['stock_quantity'] = (int)$lageranzahl;
}
if(!is_null($product_id)) {
// Such a product already appears to exist, so we update it
$this->client->put('products/'.$product_id, array_merge([
], $commonProductAtts));
$this->WooCommerceLog("WooCommerce Artikel geändert für Artikel: $nummer / $product_id");
$this->logger->info("WooCommerce Artikel geändert für Artikel: $nummer / $product_id");
}
else{
// create a new product
$product_id = $this->client->post('products/', array_merge([
'sku' => $nummer,
], $commonProductAtts))->id;
$this->WooCommerceLog("WooCommerce neuer Artikel angelegt: $nummer");
$this->logger->info("WooCommerce neuer Artikel angelegt: $nummer");
}
// TODO: Kategoriebaum und Bilder werden noch nicht uebertragen
// if(isset($tmp[$i]['kompletter_kategorienbaum'])){
@ -729,13 +693,10 @@ class Shopimporter_Woocommerce extends ShopimporterBase
// $this->save_images($dateien, $product_id);
// }
// Update the associated product categories
$chosenCats = array();
if(isset($tmp[$i]['kategorien']) || isset($tmp[$i]['kategoriename'])){
$kategorien = $tmp[$i]['kategorien'];
if (!($kategorien) && !self::emptyString($tmp[$i]['kategoriename'])) {
$kategorien = array(
@ -745,11 +706,9 @@ class Shopimporter_Woocommerce extends ShopimporterBase
);
}
if((!empty($kategorien)?count($kategorien):0)>0){
// Retrive all WC categories via API
$allWooCommerceCategories = $this->client->get('products/categories', ['per_page' => '100']);
$searchWpCategories = [];
foreach($allWooCommerceCategories as $a){
$searchWpCategories[$a->id] = $a->name;
@ -764,13 +723,10 @@ class Shopimporter_Woocommerce extends ShopimporterBase
// If WC has a matching category. We match based on name!
if(array_search($wawi_cat_name,array_values($searchWpCategories)) !== false) {
// get id of that WC Category
$wcCatId = array_search($wawi_cat_name,$searchWpCategories);
} else {
// No matching category exists
$wcCatId = $this->client->post('products/categories', [
'name' => $wawi_cat_name,
@ -778,14 +734,12 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
if ($wcCatId) {
// update category. We first retrieve the product and append the new product category, not replace the entire category array.
$alreadyAssignedWCCats = $this->client->get('products/'.$product_id, [
'per_page' => 1,
])->categories;
// Get ids of existing categories
$existingCategoryIds = [];
foreach ($alreadyAssignedWCCats as $cat) {
@ -814,16 +768,10 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$anzahl++;
}
return $anzahl;
// return array($product_id,$anzahl,$nummer,$steuersatz, $preis);
}
/**
* Checks the connection to the WooCommerce API by trying a simple API request
*
@ -839,7 +787,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
}
/**
* This is called by class.remote.php, initializes some class variables from the DB
* @param [type] $shopid [description]
@ -876,8 +823,8 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$ImportWooCommerceApiKey,
//WooCommerce API Secret
$ImportWooCommerceApiSecret,
["query_string_auth" => true]
["query_string_auth" => true],
$this->logger
);
}
@ -919,8 +866,8 @@ class Shopimporter_Woocommerce extends ShopimporterBase
$ImportWooCommerceApiUrl,
$ImportWooCommerceApiKey,
$ImportWooCommerceApiSecret,
['query_string_auth' => true]
['query_string_auth' => true],
$this->logger
);
$auth = $this->ImportAuth();
@ -931,7 +878,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return null;
}
/**
* @return array[]
*/
@ -987,7 +933,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
* @throws WCHttpClientException
*/
private function getShopIdBySKU($sku) {
// Retrieve the product with the given sku.
// Note: We limit the result set to 1 (per_page=1), so this doesnt work
// if there are multiple products with the same sku. should not happen in practice anyway
@ -1005,7 +950,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return null;
}
private function getSKUByShopId($articleid, $variationid) {
$product = $this->client->get("products/$articleid/variations/$variationid");
@ -1023,7 +967,7 @@ class Shopimporter_Woocommerce extends ShopimporterBase
'ausblenden'=>array('abholmodus'=>array('zeitbereich')),
'archiv'=>array('ab_nummer'),
'felder'=>array(
'protokoll'=>array('typ'=>'checkbox','bezeichnung'=>'Protokollierung im Logfile:'),
// 'protokoll'=>array('typ'=>'checkbox','bezeichnung'=>'Protokollierung im Logfile:'),
'ImportWoocommerceApiKey'=>array('typ'=>'text','bezeichnung'=>'{|API Key:','size'=>60),
'ImportWoocommerceApiSecret'=>array('typ'=>'text','bezeichnung'=>'{|API Secret|}:','size'=>60),
'ImportWoocommerceApiUrl'=>array('typ'=>'text','bezeichnung'=>'{|API Url|}:','size'=>40),
@ -1034,20 +978,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
));
}
/**
* Writes data to the syslog
* @param [type] $nachricht message that will be logged
* @param string $dump php array or object, printed using print_r
*/
public function WooCommerceLog($nachricht, $dump = '')
{
if($this->protokoll){
$this->app->erp->LogFile($nachricht, print_r($dump, true));
}
}
/**
* Compares two Objects and returns true if every variable in items
* is the same in $a and $b
@ -1068,8 +998,6 @@ class Shopimporter_Woocommerce extends ShopimporterBase
return true;
}
/**
* Returns true when the string entered is empty, after stripping whitespace
* @param String $string input
@ -1081,11 +1009,8 @@ class Shopimporter_Woocommerce extends ShopimporterBase
}
class WCClient
{
/**
* WooCommerce REST API WCClient version.
*/
@ -1097,7 +1022,10 @@ class WCClient
* @var WCHttpClient
*/
public $http;
/** @var Logger $logger */
public $logger;
/**
* Initialize client.
*
@ -1108,9 +1036,10 @@ class WCClient
*
* @throws WCHttpClientException
*/
public function __construct($url, $consumerKey, $consumerSecret, $options = [])
public function __construct($url, $consumerKey, $consumerSecret, $options = [], $logger)
{
$this->http = new WCHttpClient($url, $consumerKey, $consumerSecret, $options);
$this->http = new WCHttpClient($url, $consumerKey, $consumerSecret, $options, $logger);
$this->logger = $logger;
}
/**
@ -1190,7 +1119,6 @@ class WCClient
class WCResponse
{
/**
* WCResponse code.
*
@ -1289,7 +1217,6 @@ class WCResponse
class WCOptions
{
/**
* Default WooCommerce REST API version.
*/
@ -1423,7 +1350,6 @@ class WCOptions
class WCRequest
{
/**
* WCRequest url.
*
@ -1596,7 +1522,6 @@ class WCRequest
class WCOAuth
{
/**
* OAuth signature method algorithm.
*/
@ -1906,7 +1831,6 @@ class WCHttpClientException extends \Exception
class WCHttpClient
{
/**
* cURL handle.
*
@ -1962,6 +1886,9 @@ class WCHttpClient
* @var string
*/
private $responseHeaders;
/** @var Logger $logger */
public $logger;
/**
* Initialize HTTP client.
@ -1973,7 +1900,7 @@ class WCHttpClient
*
* @throws WCHttpClientException
*/
public function __construct($url, $consumerKey, $consumerSecret, $options)
public function __construct($url, $consumerKey, $consumerSecret, $options, $logger)
{
if (!function_exists('curl_version')) {
throw new WCHttpClientException('cURL is NOT installed on this server', -1, new WCRequest(), new WCResponse());
@ -1983,6 +1910,7 @@ class WCHttpClient
$this->url = $this->buildApiUrl($url);
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->logger = $logger;
}
/**
@ -2171,7 +2099,6 @@ class WCHttpClient
*/
protected function createResponse()
{
// Set response headers.
$this->responseHeaders = '';
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($_, $headers) {
@ -2195,7 +2122,7 @@ class WCHttpClient
*/
protected function setDefaultCurlSettings()
{
$verifySsl = $this->options->verifySsl();
// $verifySsl = $this->options->verifySsl();
$timeout = $this->options->getTimeout();
$followRedirects = $this->options->getFollowRedirects();
@ -2235,6 +2162,13 @@ class WCHttpClient
$errorMessage = $errors->message;
$errorCode = $errors->code;
}
$this->logger->error('WooCommerce Error',
[
'request' => $this->request,
'response' => $this->response
]
);
throw new WCHttpClientException(
sprintf('Error: %s [%s]', $errorMessage, $errorCode),
@ -2294,8 +2228,6 @@ class WCHttpClient
public function request($endpoint, $method, $data = [], $parameters = [])
{
// Initialize cURL.
$this->ch = curl_init();
@ -2308,7 +2240,6 @@ class WCHttpClient
// Get response.
$response = $this->createResponse();
// Check for cURL errors.
if (curl_errno($this->ch)) {
throw new WCHttpClientException('cURL Error: ' . \curl_error($this->ch), 0, $request, $response);