Skip to content

Instantly share code, notes, and snippets.

@KEMBL
Last active May 22, 2021 07:49
Show Gist options
  • Save KEMBL/3700dad53433d4643f1a59bdfe0b1b87 to your computer and use it in GitHub Desktop.
Save KEMBL/3700dad53433d4643f1a59bdfe0b1b87 to your computer and use it in GitHub Desktop.
API method for retrieving Product and SKU features. For Webasyst Shop-script
<?php
/**
*
* API method for retrieving Product and SKU features
*
* Autor: https://github.com/KEMBL
*
* License: MIT
*
* Version: 1.0.0 alpha
*
* Tested with: Shop-Script 7.5.1
*
* Instalation:
*
* 1) Copy this file into /wa-apps/shop/plugins/kembl/lib/api
* 2) Clean shop-script cache
*
* Usage: <shopscript-site-url>/api.php/shop.product.features.getInfo?id=<shop-script-product-id>&access_token=<shop-script-access-key>
*
* Return list of objects with features, where:
* if sku_id == null - it is a product feature
* if sku_id != null - it is an sku feature.
*
*/
class shopProductFeaturesGetInfoMethod extends shopApiMethod
{
protected $method = 'GET';
public function execute()
{
$product_id = $this->get('id', true);
if (!$product_id || !is_numeric($product_id)) {
throw new waAPIException('invalid_param', 'Required parameter is missing: id', 400);
}
$sql = "SELECT
spf.id, product_id, sku_id, spf.feature_id, feature_value_id, sort, value, unit, type, code, value_base_unit, begin_base_unit, end_base_unit, begin, end
FROM shop_product_features as spf
JOIN (
SELECT id, feature_id, sort, value, code, null as unit, null as type, null as value_base_unit, null as begin_base_unit, null as end_base_unit, null as begin, null as end FROM shop_feature_values_color
UNION
SELECT id, feature_id, sort, value, null as code, unit, type, value_base_unit, null as begin_base_unit, null as end_base_unit, null as begin, null as end FROM shop_feature_values_dimension
UNION
SELECT id, feature_id, sort, value, null as code, null as unit, null as type, null as value_base_unit, null as begin_base_unit, null as end_base_unit, null as begin, null as end FROM shop_feature_values_double
UNION
SELECT id, feature_id, sort, null as value, begin, end, null as code, unit, type, null as value_base_unit, begin_base_unit, end_base_unit FROM shop_feature_values_range
UNION
SELECT id, feature_id, sort, value, null as code, null as unit, null as type, null as value_base_unit, null as begin_base_unit, null as end_base_unit, null as begin, null as end FROM shop_feature_values_text
UNION
SELECT id, feature_id, sort, value, null as code, null as unit, null as type, null as value_base_unit, null as begin_base_unit, null as end_base_unit, null as begin, null as end FROM shop_feature_values_varchar
) as sfv on (spf.feature_id=sfv.feature_id and spf.feature_value_id=sfv.id)
WHERE spf.product_id = ?";
$wam = new waModel();
$rows = $wam->query($sql, $product_id)->fetchAll();
$result = array();
foreach ($rows as $row) {
$feature = array();
foreach ($row as $k => $v) {
$feature[$k] = $v;
}
array_push($result, $feature);
}
$this->response = $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment