-
Notifications
You must be signed in to change notification settings - Fork 2
Using feed "extension" classes
Google Product Search Feeder II (GPSF2) uses class extensions to enable site-specific additions to a product feed. When a feed is being generated, the feed's main controller loads all .php files present in the /includes/classes/gpsf directory alphabetically. An extension provides a means for a site, for instance, to identify a product's MAP price or site-specific gtin.
Each such file is expected to contain a class that extends the gpsfBase class found in /includes/classes/gpsf/gpsfBase.php and that class' name must be the same as the class' filename. For example, a file named gpsfMySite.php is expected to include the definition of a class named gpsfMySite.
This method is called once at the start of a feed's generation to gather additional database tables and/or fields as well as query clauses for the overall database query-selection for the feed. The base feed always includes the following tables and associated fields:
- Tables
- TABLE_PRODUCTS (p)
- TABLE_PRODUCTS_DESCRIPTION (pd)
- TABLE_MANUFACTURERS (m)
- TABLE_PRODUCT_TYPES (pt)
- Fields
- p.products_id, p.products_model, , p.products_image, p.products_tax_class_id, p.products_price, p.products_priced_by_attribute, p.products_type, p.master_categories_id, GREATEST(p.products_date_added, IFNULL(p.products_last_modified, 0), IFNULL(p.products_date_available, 0)) AS base_date, p.products_date_available, p.products_quantity, p.products_weight, p.product_is_always_free_shipping
- pd.products_description, pd.products_name
- m.manufacturers_name
- pt.type_handler
public function getAdditionalQueryFields(string $additional_fields_base, string $additional_tables_base): array
The method is supplied two string inputs that identify any database field or table additions currently in effect:
-
$additional_fields_base. A string containing a comma-separated list of additional database fields to be gathered by the overall feed's selection. -
$additional_tables_base. A string containing a blank-separated list of additional tables to beJOINed in gathering the overall feed's selection.
The method returns a simple array of three elements, in the following order:
return [
$additional_fields,
$additional_tables,
$additional_where_clause,
];-
$additional_fields. Contains a comma-separated string of additional database fields required by theextension. -
$additional_tables. Contains a blank-separated string of additional table-joins required by theextension. -
$additional_where_clause. Contains a string ofANDseparated clauses used to limit the products included in the feed.
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to "not include" the current product in the generated feed.
public function bypassProductInFeed(string $products_id, array $product): string
The method is supplied two inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$product. An associative array containing all the database fields selected for the current product.
The method returns a string value identifying whether ('', an empty string) or not (any other value) the product is to remain in the generated feed.
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to override the current id (the g:id attribute) for the current product.
public function getProductsFeedId(string $products_id, $id, array $product)
The method is supplied three inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$id. The value currently determined for theg:idvalue. This can be either a valid string or(bool)falseif no id-value could be determined for the product. -
$product. An associative array containing all the database fields selected for the current product.
The method returns either a string identifying the value to be used for the product's g:id value or (bool)false if no value could be determined. Returning a value of (bool)false will cause the product to not be included in the feed!
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to make modifications to the current product's description, possibly appending some additional information.
public function modifyProductsDescription(string $products_id, string $products_description, array $product): string
The method is supplied three inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$products_description. The value currently determined for the product's descriptive text (g:description). -
$product. An associative array containing all the database fields selected for the current product.
The method returns the string value of the descriptive text for the product. If the value returned is an empty string, the product will not be included in the feed!
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to include additional feed-related attributes to the product's feed information.
public function getProductsAttributes(string $products_id, array $product, array $categories_list, array $cPath, array $custom_fields): array
The method is supplied five inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$product. An associative array containing all the database fields selected for the current product. -
$categories_list. A numerically-indexed array containing the product's category and sub-category names. Element0contains the product's top-most category name. -
$cPath. A numerically-indexed array containing the product's category and sub-categorycategories_idvalues. Element0contains the product's top-mostcategories_id. -
$custom_fields. An associative array (keyed on the feedattribute's name minus the leadingg:) containing all currently recorded feedattributesfor the product.
The method returns an associative array of feed attribute (and possibly sub-attribute) values to be added to or replaced in the product's feed information. That array's keys are the feed attribute or sub-attribute names, minus the leading g:.
For example, an extension could add or replace the product's g:gtin attribute by returning
[
'gtin' => '0123456789',
]Notes:
- It is the extension's responsibility to properly format any non-string attribute type. For example, the 'adult' attribute should be set to (string)'true'/'false' values, not (bool)true/false. Any URL-type attributes returned should be have spaces (' ') converted to
%20. - Starting with v1.0.1, this method can also be used to replace the base feed's value for a product's
g:link,g:product_typeandg:brandattributes. If theg:linkattribute is supplied, it is the extension's responsibility to properly sanitize any ampersands and spaces in that link!
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to override the product's shipping rate.
public function getProductsShippingRate(string $products_id, $products_weight, $products_price, string $product_is_always_free_shipping): float
The method is supplied four inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$products_weight. Thestringorfloatvalue identifying the product's weight. -
$products_price. Thestringorfloatvalue identifying the product's price. -
$product_is_always_free_shipping. Thestringidentifier that indicates whether ('1') or not ('0') the product is always free-shipping.
The method returns an floating-point ((float)) value identifying the product's shipping rate/pricing. If the extension doesn't wish to override the 'base' feed-generated shipping price, it returns (float)-1.0. A value >= 0 returned is recorded in the product's g:shipping attribute's g:price sub-attribute.
Note: If multiple extensions are loaded, the first extension that returns a value >= 0.0 "wins".
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to override the product's price and/or sale-price.
public function getProductPricing(string $products_id, array $product, $price, $sale_price): array
The method is supplied four inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$product. An associative array containing all the database fields selected for the current product. -
$price. Thestringorfloatvalue identifying the product's currently-calculated 'base' price, as returned byzen_get_products_base_price. -
$sale_price. Thestringorfloatvalue identifying the product's currently-calculated sale-price, as returned byzen_get_products_special_price.
The method returns a numerically-indexed array containing any update to the product's $price and/or $sale_price. If no update is to be made, the extension returns the values input.
return [
$price,
$sale_price
];This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to override the product's primary-image URL, the g:image attribute for the product.
public function getProductsImageUrl(string $products_image)
The method is supplied a single input:
-
$products_image. Thestringrepresentation of the current product'sproducts_imagevalue, as defined in the product's database record.
The method returns one of three possible values:
-
(bool)false. No override is necessary, the base feed will format the product's image-link. -
null. The product's image, and thus the product, is not to be included in the feed. -
(string)$products_url. The value to be used for the product'sg:imagefeed attribute.
This method is called once for each product that is a candidate for the feed, giving site-specific customization the opportunity to override the value to be set for the product's g:title attribute, defaulting to the products::products_name.
public function getProductsTitle(string $products_id, string $products_title, array $product): string
The method is supplied three inputs:
-
$products_id. Thestringrepresentation of the current product'sproducts_idvalue. -
$products_title. Thestringcurrent value to be used as the product'sg:titlefeed attribute. -
$product. An associative array containing all the database fields selected for the current product.
The method returns a string output identifying the value to be used as the product's feed-title. Note that returning an empty string will result in the product being excluded from the feed, since each product in the feed must have a non-empty g:title!