latest changes
This commit is contained in:
@@ -18,6 +18,13 @@ class McCansHelpers
|
||||
};
|
||||
}
|
||||
|
||||
public static function usd()
|
||||
{
|
||||
return function ($template, $context, $args, $source) {
|
||||
return "$" . number_format((float) $context->get($args), 2);
|
||||
};
|
||||
}
|
||||
|
||||
public static function locationTile()
|
||||
{
|
||||
return function ($template, $context, $args, $source) {
|
||||
@@ -36,17 +43,156 @@ class McCansHelpers
|
||||
"woocommerce.cartUrl",
|
||||
"woocommerce.attribute.msrp",
|
||||
]);
|
||||
if ((float) $data["msrp"] && (float) $data["price"]) {
|
||||
if ($data["price"] != 0) {
|
||||
$difference = $data["msrp"] - $data["price"];
|
||||
if ($difference > 0) {
|
||||
$savingsPercent = round(($difference / $data["msrp"])*100);
|
||||
}
|
||||
$data["savings"] = self::calcDifference($data["price"], $data["msrp"]);
|
||||
|
||||
$data["savings"] = $savingsPercent;
|
||||
}
|
||||
}
|
||||
return site()->render("product-tile", $data);
|
||||
};
|
||||
}
|
||||
|
||||
public static function renderBlock()
|
||||
{
|
||||
return function ($template, $context, $args, $source) {
|
||||
// if a page or product is chosen...
|
||||
if ($context->get($args)["page_product"]) {
|
||||
$post = $context->get($args)["page_product"];
|
||||
|
||||
if ($post->post_type === "page") {
|
||||
return site()->render("page-tile", site()->getPost($post, [
|
||||
"title",
|
||||
"url",
|
||||
"thumbnail",
|
||||
]));
|
||||
}
|
||||
|
||||
$data = site()->getPost($post, [
|
||||
"url",
|
||||
"id",
|
||||
"title",
|
||||
"thumbnail",
|
||||
"woocommerce.price",
|
||||
"woocommerce.attribute.msrp",
|
||||
"woocommerce.cartUrl",
|
||||
"woocommerce.sku",
|
||||
]);
|
||||
$data["savings"] = self::calcDifference($data["price"], $data["msrp"]);
|
||||
|
||||
return site()->render("product-tile", $data);
|
||||
}
|
||||
|
||||
// if we got here, then hopefully the product_category is set
|
||||
if (!$context->get($args)["product_category"]) {
|
||||
return "No valid option chosen...";
|
||||
}
|
||||
|
||||
$termID = $context->get($args)["product_category"];
|
||||
$term = get_term($termID);
|
||||
return site()->render("page-tile", site()->getQueriedObject($term, ['title', 'thumbnail', 'url']));
|
||||
};
|
||||
}
|
||||
|
||||
public static function renderCallout()
|
||||
{
|
||||
return function ($template, $context, $args, $source) {
|
||||
// if a page or product is chosen...
|
||||
if ($context->get($args)["page_product"]) {
|
||||
$post = $context->get($args)["page_product"];
|
||||
|
||||
if ($post->post_type === "page") {
|
||||
return site()->render("page-callout", site()->getPost($post, [
|
||||
"title",
|
||||
"url",
|
||||
"thumbnail",
|
||||
"excerpt",
|
||||
]));
|
||||
}
|
||||
|
||||
$data = site()->getPost($post, [
|
||||
"url",
|
||||
"id",
|
||||
"title",
|
||||
"thumbnail",
|
||||
"woocommerce.price",
|
||||
"woocommerce.attribute.msrp",
|
||||
"woocommerce.cartUrl",
|
||||
"woocommerce.sku",
|
||||
]);
|
||||
$data["savings"] = self::calcDifference($data["price"], $data["msrp"]);
|
||||
|
||||
return site()->render("product-tile", $data);
|
||||
}
|
||||
|
||||
// if we got here, then hopefully the product_category is set
|
||||
if (!$context->get($args)["category"]) {
|
||||
return "No valid option chosen...";
|
||||
}
|
||||
|
||||
$termID = $context->get($args)["category"];
|
||||
$term = get_term($termID);
|
||||
return site()->render("category-callout", site()->getQueriedObject($term, [
|
||||
'title',
|
||||
'thumbnail',
|
||||
'url',
|
||||
'description',
|
||||
]));
|
||||
};
|
||||
}
|
||||
|
||||
public static function calcDifference($price, $msrp): string
|
||||
{
|
||||
if (!$price) {
|
||||
return "";
|
||||
}
|
||||
if (!$msrp) {
|
||||
return "";
|
||||
}
|
||||
|
||||
$difference = $msrp - $price;
|
||||
if ($difference === 0) {
|
||||
return "";
|
||||
}
|
||||
return round(($difference / $msrp) * 100);
|
||||
}
|
||||
|
||||
public static function searchResultBox(string $type)
|
||||
{
|
||||
return function ($template, $context, $args, $source) use ($type) {
|
||||
$post = $context->get($args);
|
||||
|
||||
if ($type === "product") {
|
||||
if ($post["post_type"] !== "product") {
|
||||
return "";
|
||||
}
|
||||
|
||||
$data = site()->getPost($post["id"], [
|
||||
"url",
|
||||
"id",
|
||||
"title",
|
||||
"thumbnail",
|
||||
"woocommerce.price",
|
||||
"woocommerce.attribute.msrp",
|
||||
"woocommerce.cartUrl",
|
||||
"woocommerce.sku",
|
||||
]);
|
||||
$data["savings"] = self::calcDifference($data["price"], $data["msrp"]);
|
||||
|
||||
return site()->render("product-tile", $data);
|
||||
}
|
||||
|
||||
if ($type === "page") {
|
||||
// var_dump($post);
|
||||
if ($post["post_type"] !== "page") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return site()->render("page-tile", site()->getPost($post["id"], [
|
||||
"url",
|
||||
"id",
|
||||
"title",
|
||||
"thumbnail",
|
||||
]));
|
||||
}
|
||||
|
||||
return site()->render("search-box", $context->get($args));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user