From: JoySystem_v Date: Mon, 26 Aug 2024 08:17:29 +0000 (+0300) Subject: добавил контроллер для вывода фида X-Git-Tag: 1.6~13^2~50 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=822dbef70f1b89ec8eef74911307184145e76322;p=erp24_rep%2Fyii-erp24%2F.git добавил контроллер для вывода фида --- diff --git a/erp24/controllers/MarketplaceController.php b/erp24/controllers/MarketplaceController.php new file mode 100644 index 00000000..b217b956 --- /dev/null +++ b/erp24/controllers/MarketplaceController.php @@ -0,0 +1,30 @@ +response->format = Response::FORMAT_RAW; + Yii::$app->response->headers->add('Content-Type', 'application/xml; charset=utf-8'); + + return $xmlFeed; + } +} \ No newline at end of file diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php new file mode 100644 index 00000000..f9adf09c --- /dev/null +++ b/erp24/services/MarketplaceService.php @@ -0,0 +1,151 @@ +where(['tip' => 'products']) + ->andWhere(['not', ['components' => '']]) + ->all(); + + $result = []; + + foreach ($products as $product) { + $components = json_decode($product->components, true); + $composition = []; + + foreach ($components as $componentId => $quantity) { + $component = Products1c::findOne(['id' => $componentId]); + if ($component && $quantity > 0) { + $composition[] = [ + 'name' => $component->name, + 'quantity' => $quantity, + 'unit' => 'шт' + ]; + } + } + + $result[] = [ + 'id' => $product->id, + 'name' => $product->name, + 'picture' => self::getProductPictureUrl($product->id), + 'price' => self::getProductPrice($product->id), + 'oldprice' => self::getProductOldPrice($product->id), + 'description' => self::getProductDescription($product->id), + 'qty' => self::getProductQty($product->id), + 'weight' => self::getProductWeight($product->id), + 'minorder' => self::getProductMinOrder($product->id), + 'composition' => $composition, + 'available' => self::getProductAvailability($product->id) + ]; + } + + return $result; + } + + /** + * Статический метод для создания XML-фида на основе информации о продуктах. + * + * @param array $productsInfo + * @return string + */ + public static function createXMLFeed($productsInfo) + { + $xml = new \SimpleXMLElement(''); + $xml->addAttribute('date', date('Y-m-d\TH:i:sP')); + + $shop = $xml->addChild('shop'); + $shop->addChild('name', 'BestSeller'); + $shop->addChild('company', 'The Best inc.'); + $shop->addChild('url', 'http://best.seller.ru'); + + // Добавление валюты + $currencies = $shop->addChild('currencies'); + $currency = $currencies->addChild('currency'); + $currency->addAttribute('id', 'RUR'); + $currency->addAttribute('rate', '1'); + + // Добавление категорий + $categories = $shop->addChild('categories'); + $category = $categories->addChild('category', 'Букеты'); + $category->addAttribute('id', '1'); + + // Добавление информации о доставке + $deliveryOptions = $shop->addChild('delivery-options'); + $deliveryOption = $deliveryOptions->addChild('option'); + $deliveryOption->addAttribute('cost', '200'); + $deliveryOption->addAttribute('days', '1'); + + // Добавление офферов (продуктов) + $offers = $shop->addChild('offers'); + foreach ($productsInfo as $product) { + $offer = $offers->addChild('offer'); + $offer->addAttribute('id', $product['id']); + $offer->addChild('name', htmlspecialchars($product['name'])); + $offer->addChild('url', 'http://best.seller.ru/product_page.asp?pid=' . $product['id']); + $offer->addChild('price', $product['price']); + $offer->addChild('currencyId', 'RUR'); + $offer->addChild('categoryId', '1'); + $offer->addChild('delivery', 'true'); + + // Информация о доставке для каждого оффера + $offerDeliveryOptions = $offer->addChild('delivery-options'); + $offerDeliveryOption = $offerDeliveryOptions->addChild('option'); + $offerDeliveryOption->addAttribute('cost', '300'); + $offerDeliveryOption->addAttribute('days', '1'); + $offerDeliveryOption->addAttribute('order-before', '18'); + + $offer->addChild('description', htmlspecialchars($product['description'])); + $offer->addChild('weight', $product['weight']); + $offer->addChild('qty', $product['qty']); + $offer->addChild('minorder', $product['minorder']); + $offer->addChild('available', $product['available'] ? 'true' : 'false'); + + if ($product['oldprice']) { + $offer->addChild('oldprice', $product['oldprice']); + } + + foreach ($product['composition'] as $component) { + $consist = $offer->addChild('param', $component['quantity']); + $consist->addAttribute('name', htmlspecialchars($component['name'])); + $consist->addAttribute('unit', $component['unit']); + } + } + + return $xml->asXML(); + } + + // Приватные статические вспомогательные методы + private static function getProductPictureUrl($productId) { + return 'https://files.erp24.ru/pic.jpg'; + } + private static function getProductPrice($productId) { + $price = Prices::find() + ->where(['product_id' => $productId]) + ->one(); + return $price['price'] ?? 0; + } + private static function getProductOldPrice($productId) { return null; } + private static function getProductDescription($productId) { return 'Описание'; } + private static function getProductQty($productId) { return 100; } + private static function getProductWeight($productId) { + return null; + } + private static function getProductMinOrder($productId) { + return 1; + } + private static function getProductAvailability($productId) { + return true; + } +} diff --git a/erp24/views/marketplace/feed.php b/erp24/views/marketplace/feed.php new file mode 100644 index 00000000..1c5f0de7 --- /dev/null +++ b/erp24/views/marketplace/feed.php @@ -0,0 +1,4 @@ +