vitalybaev / google-merchant-feed
用于创建Google Merchant产品数据源的PHP包
v2.6.0
2022-12-23 16:17 UTC
Requires
- php: >=5.5
- sabre/xml: ^1.5 || ^2.0
README
安装
通过 Composer 安装此包。
从终端运行Composer require命令
composer require vitalybaev/google-merchant-feed
示例
use Vitalybaev\GoogleMerchant\Feed; use Vitalybaev\GoogleMerchant\Product; use Vitalybaev\GoogleMerchant\Product\Shipping; use Vitalybaev\GoogleMerchant\Product\Availability\Availability; // Create feed object $feed = new Feed("My awesome store", "https://example.com", "My awesome description"); // Put products to the feed ($products - some data from database for example) foreach ($products as $product) { $item = new Product(); // Set common product properties $item->setId($product->id); $item->setTitle($product->title); $item->setDescription($product->description); $item->setLink($product->getUrl()); $item->setImage($product->getImage()); if ($product->isAvailable()) { $item->setAvailability(Availability::IN_STOCK); } else { $item->setAvailability(Availability::OUT_OF_STOCK); } $item->setPrice("{$product->price} USD"); $item->setGoogleCategory($product->category_name); $item->setBrand($product->brand->name); $item->setGtin($product->barcode); $item->setCondition('new'); // Some additional properties $item->setColor($product->color); $item->setSize($product->size); // Shipping info $shipping = new Shipping(); $shipping->setCountry('US'); $shipping->setRegion('CA, NSW, 03'); $shipping->setPostalCode('94043'); $shipping->setLocationId('21137'); $shipping->setService('UPS Express'); $shipping->setPrice('1300 USD'); $item->setShipping($shipping); // Set a custom shipping label and weight (optional) $item->setShippingLabel('ups-ground'); $item->setShippingWeight('2.14'); // Set a custom label (optional) $item->setCustomLabel('Some Label 1', 0); $item->setCustomLabel('Some Label 2', 1); // Add this product to the feed $feed->addProduct($item); } // Here we get complete XML of the feed, that we could write to file or send directly $feedXml = $feed->build();
处理属性
Product
类提供了一些方法来管理各种属性。但它并不涵盖所有Google Merchant属性。如果不存在方法,应使用以下之一
$product->setAttribute($attributeName, $attributeValue, $isCData = false)
- 设置属性。如果存在旧属性,则替换它。$product->addAttribute($attributeName, $attributeValue, $isCData = false)
- 添加另一个属性的值。例如,数据源可以拥有多个additional_image_link
属性。
待办事项
- 涵盖所有Google Merchant数据源属性
- 更多测试
- 你的反馈?
许可证
The MIT License
Copyright (c) 2016 Vitaly Baev. vitalybaev.ru
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.