mysutka / product_feed_generator
一个类,帮助为各种价格比较服务生成产品数据,适用于Atk14应用程序
Requires
- php: >=5.3.0
- atk14/drink-markdown: ~0.3
- atk14/files: ^1.0
- atk14/string4: 0.* >=0.1.1
- atk14/xmole: ^1.0
Requires (Dev)
- phpunit/phpunit: ~6
README
此包提供了一个库,帮助创建各种价格比较服务(例如Google购物、Heureka等)的XML和CSV数据。库旨在可扩展。开发者可以为自己需要的服务创建自己的生成器,也可以创建自己的读取器,例如需要更改对象选择或处理不同对象。
首先,您可以在src/robots/product_feed_generator_robot.php
中查看示例。
这些价格比较服务可用的生成器
- Heureka.cz
- Zbozi.cz
- Google Shopping
- Google Merchants
基本类
使用了两个基本类,可以扩展:Atk14EshopReader选择、读取对象(通常是产品)并将它们转换为数组。FeedGenerator处理Atk14EshopReader返回的数组。不应直接使用。应创建一个新的继承自FeedGenerator的类。
Atk14EshopReader选项
- price_with_currency - 布尔值 [默认: false]
- price_finder
- lang - 字符串 [默认: null => 应用程序默认] - 强制读取器使用指定语言的对象翻译,如果对象支持该语言
- category_path_connector - 字符串 [默认: '>']
- hostname - 字符串 [默认: ATK14_HTTP_HOST]
- image_geometry - 字符串 [默认: '800x800']
- image_watermark - 字符串 [默认: null => 不使用水印]
FeedGenerator选项
- full_feed
- logger
- output_format
- xml_item_element_name
- feed_begin
- feed_end
- fixed_values
示例
通常只使用FeedGenerator
类。
$generator = new \ProductFeedGenerator\Generator\GoogleShoppingGenerator(); $generator->exportTo("/path/to/public/web/directory/product_feeds/google_shopping.xml")
这将输出包含文本'25'的xml元素DELIVERY_DATE。如果它应该由生成器生成,则将覆盖原始值。
$generator = new \ProductFeedGenerator\Generator\HeurekaCzGenerator(null, [ "fixed_values" => [ "DELIVERY_DATE" => 25, ]);
输出
<DELIVERY_DATE>25</DELIVERY_DATE>
在需要的情况下,可以删除通常在输出中出现的元素。在此示例中,生成器默认在每个条目中输出元素EAN。通过将fixed_values选项中的EAN指定为null,该元素将从输出中消失。
$generator = new \ProductFeedGenerator\Generator\HeurekaCzGenerator(null, [ "fixed_values" => [ "EAN" => null, ]);
创建包含另一种货币(例如EUR)的价格数据的数据
$eur_price_finder = PriceFinder::GetInstance(null, Currency::FindByCode("EUR")); $generator = new \ProductFeedGenerator\Generator\HeurekaCzGenerator(null, [ "price_finder" => $eur_price_finder, ]);
自定义读取器
最常见的例子是生成有限产品集的读取器。在这种情况下,我们创建一个新的类,作为ProductFeedGenerator\Reader\Atk14EshopReader
的子类,它包含一个单独的方法getObjectIds()。这个LimitedEshopReader提供了上个月创建的产品的ID。
class LimitedEshopReader extends ProductFeedGenerator\Reader\Atk14EshopReader { function getObjectIds($options=[]) { $options += [ "offset" => 0, ]; $ids = $this->dbmole->selectIntoArray("SELECT distinct(id) FROM cards WHERE created_at>now() - interval '1 month' AND NOT deleted AND visible ORDER BY id LIMIT 100 OFFSET :offset", array( ":offset" => $options["offset"], )); return $ids; } }
如果您需要根据特定规则过滤掉一些产品,您可以通过itemToArray()
方法进行。请参阅示例。
class OtherLimitedEshopReader extends ProductFeedGenerator\Reader\Atk14EshopReader { function itemToArray($product) { if (!$product->canBeOrdered()) { return null; } } }
自定义生成器
有时我们需要向比较服务发送经过轻微修改的值,而生成器则提供它作为存储在数据库中的值。因此,让我们创建我们的自定义生成器。假设我们的标准GoogleShoppingGenerator不提供"g:availability"属性(实际上它确实提供了),但主Atk14EshopReader
类提供了"STOCKCOUNT"属性。因此,我们创建一个新的生成器类,它将STOCKCOUNT属性添加到读取器类的值中,然后将在输出中添加"g:availability"属性。
class CustomGoogleShoppingGenerator extends ProductFeedGenerator\Generator\GoogleShoppingGenerator { function getAttributesMap() { return parent::getAttributesMap() + [ "g:availability" => "STOCKCOUNT", ]; } function afterFilter($values) { $values["g:availability"] = ($values["g:availability"]) > 0 ? "in stock" : "out of stock"; return $values; } }
在父类Generator中调用了getAttributesMap()
方法。在这里,我们重写了它,并使用Reader类提供的通用STOCKCOUNT
属性添加了g:availability
属性,这只是一个我们仓库中的产品数量。然后我们添加了afterFilter()
方法,它也被父类FeedGenerator调用。它用于转换通常要发送到最终输出的值。在我们的方法中,我们修改了g:availability
属性以满足我们的需求,这意味着我们向输出发送文本“有货”或“缺货”而不是确切的数字。