Как менять кнопку покупки в зависимости от состояния на складе — различия между версиями
Материал из Umicms
Mad grant (обсуждение | вклад) (Новая страница: «'''Актуально для версии 2.9.5''' == Задача == Если Вы столкнулись с данной задачей, то её решение…») |
Mad grant (обсуждение | вклад) |
||
(не показаны 2 промежуточные версии этого же участника) | |||
Строка 12: | Строка 12: | ||
<xsl:choose> | <xsl:choose> | ||
<xsl:when test="number(page/properties/group[@name = 'catalog_stores_props']/property[@name = 'common_quantity']/value) = 0"> | <xsl:when test="number(page/properties/group[@name = 'catalog_stores_props']/property[@name = 'common_quantity']/value) = 0"> | ||
− | <a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"> | + | <a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"><img src="путь до картинки с кнопкой заказать"/></a> |
</xsl:when> | </xsl:when> | ||
<otherwise> | <otherwise> | ||
− | <a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"> | + | <a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"><img src="путь до картинки с кнопкой купить"/></a> |
</otherwise> | </otherwise> | ||
</xsl:choose> | </xsl:choose> | ||
</source> | </source> | ||
− | [[category:Вопросы и уроки разработки сайтов на UMI.CMS]] | + | |
+ | А для tpl уже придется писать кастомный макрос, пример оного дан ниже. | ||
+ | |||
+ | == Код макроса == | ||
+ | |||
+ | <source lang="php"> | ||
+ | public function getBuyButton($page_id = false, $buy_path = 'buy', $order_path = 'order'){ | ||
+ | |||
+ | $current_page_id = cmsController::getInstance()->getCurrentElementId(); | ||
+ | |||
+ | $hierarchy_col = umiHierarchy::getInstance(); | ||
+ | |||
+ | if($page_id == false){ | ||
+ | |||
+ | if($current_page_id == false && defined('VIA_HTTP_SCHEME')){ | ||
+ | throw new publicException('cant get current element via HTTP SCHEME MODE'); | ||
+ | } | ||
+ | |||
+ | $page_id = $current_page_id; | ||
+ | |||
+ | $page = $hierarchy_col->getElement($page_id, true, true); | ||
+ | |||
+ | if($page->getModule() == 'catalog' && $page->getMethod() == 'object'){ | ||
+ | if(intval($page->getValue('common_quantity')) > 0){ | ||
+ | return $buy_path; | ||
+ | }else{ | ||
+ | return $order_path; | ||
+ | } | ||
+ | }else{ | ||
+ | throw new publicException('give me catalog object'); | ||
+ | } | ||
+ | }else{ | ||
+ | $page_id = intval($page_id); | ||
+ | |||
+ | if($page_id == 0){ | ||
+ | throw new publicException('wrong id given'); | ||
+ | } | ||
+ | |||
+ | $page = $hierarchy_col->getElement($page_id, true, true); | ||
+ | |||
+ | if($page == false){ | ||
+ | throw new publicException('page with id = ' . $page_id . ' not found'); | ||
+ | } | ||
+ | |||
+ | if($page->getModule() == 'catalog' && $page->getMethod() == 'object'){ | ||
+ | if(intval($page->getValue('common_quantity')) > 0){ | ||
+ | return $buy_path; | ||
+ | }else{ | ||
+ | return $order_path; | ||
+ | } | ||
+ | }else{ | ||
+ | throw new publicException('give me catalog object'); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Вызвать его в tpl можно будет как: | ||
+ | |||
+ | <source lang="html4strict"> | ||
+ | <a href="/emarket/basket/put/element/%pid%/?redirect-uri=%system getCurrentURI()%"><img src="%custom getBuyButton(%pid%, 'путь до картинки с кнопкой купить', 'путь до картинки с кнопкой заказать')%"/></a> | ||
+ | </source> | ||
+ | [[category:Вопросы и уроки разработки сайтов на UMI.CMS]][[Категория:API]] |
Текущая версия на 15:56, 28 февраля 2014
Актуально для версии 2.9.5
Задача
Если Вы столкнулись с данной задачей, то её решение будет разное для двух шаблонизаторов.
Решение
Если для xslt Вам будет достаточно сделать условия, например так:
<xsl:choose>
<xsl:when test="number(page/properties/group[@name = 'catalog_stores_props']/property[@name = 'common_quantity']/value) = 0">
<a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"><img src="путь до картинки с кнопкой заказать"/></a>
</xsl:when>
<otherwise>
<a href="/emarket/basket/put/element/{page/@id}/?redirect-uri={$request-uri}"><img src="путь до картинки с кнопкой купить"/></a>
</otherwise>
</xsl:choose>
А для tpl уже придется писать кастомный макрос, пример оного дан ниже.
Код макроса
public function getBuyButton($page_id = false, $buy_path = 'buy', $order_path = 'order'){
$current_page_id = cmsController::getInstance()->getCurrentElementId();
$hierarchy_col = umiHierarchy::getInstance();
if($page_id == false){
if($current_page_id == false && defined('VIA_HTTP_SCHEME')){
throw new publicException('cant get current element via HTTP SCHEME MODE');
}
$page_id = $current_page_id;
$page = $hierarchy_col->getElement($page_id, true, true);
if($page->getModule() == 'catalog' && $page->getMethod() == 'object'){
if(intval($page->getValue('common_quantity')) > 0){
return $buy_path;
}else{
return $order_path;
}
}else{
throw new publicException('give me catalog object');
}
}else{
$page_id = intval($page_id);
if($page_id == 0){
throw new publicException('wrong id given');
}
$page = $hierarchy_col->getElement($page_id, true, true);
if($page == false){
throw new publicException('page with id = ' . $page_id . ' not found');
}
if($page->getModule() == 'catalog' && $page->getMethod() == 'object'){
if(intval($page->getValue('common_quantity')) > 0){
return $buy_path;
}else{
return $order_path;
}
}else{
throw new publicException('give me catalog object');
}
}
}
Вызвать его в tpl можно будет как:
<a href="/emarket/basket/put/element/%pid%/?redirect-uri=%system getCurrentURI()%"><img src="%custom getBuyButton(%pid%, 'путь до картинки с кнопкой купить', 'путь до картинки с кнопкой заказать')%"/></a>