Magento CMS pages — getting title from URL key/identifier? - magento

Is there any way to get the title of a cms page, if you only know it's url key/identifier? For example, the about page (in the sample data) has a url key/identifier of 'about-magento-demo-store'. If that's the only information I had, how would I go about getting the page title from that? As in the faux code below:
$pageTitle = Mage::getModel('cms/page')->loadByAttribute('identifier', 'about-magento-demo-store')->getTitle();
I'd like to get a list of all CMS page titles, using just the url keys/identifiers.
I know you can get the current CMS page title using the following:
$pageTitle = Mage::getSingleton('cms/page')->getTitle();
Anyone any ideas?

OK, figured it out myself using trial and error:
$pageTitle = Mage::getModel('cms/page')->load('about-magento-demo-store', 'identifier')->getTitle();

I made this solution to get URL-Key (Identifier) of a page:
<?php $pageTitle = Mage::getSingleton('cms/page')->getIdentifier(); ?>
<?php if ($pageTitle=='home'):?>

Related

Magento condition for specific page

In Magento I'm using <?php if ($this->getIsHomePage()):?> to check if it's the main page or not but how would I check if the user is on a custompage? CMS Page Url Key game-store
To get the URL key / identifier of any CMS page in Magento, use the following bit of code.
<?php
$yourUrlKey = 'game-store';
$cmsPageUrlKey = Mage::getSingleton('cms/page')->getIdentifier();
if($yourUrlKey == $cmsPageUrlKey){
//do something here
}
?>
The above code will print your URL Key
Either
$model = Mage::getModel('cms/page')->load('game-store','identifier');
var_dump($model->getData());
var_dump($model->getPageId());
or
$model = Mage::getModel('cms/page')->getCollection()
->addFieldTofilter('identifier','game-store')
->getFirstItem();
var_dump($model->getData());
var_dump($model->getPageId());
should do it.

Magento: Category Lock/Warning or T&C

I wonder if there is a way to add a Warning/Advisory or Terms & Conditions (preferably) to a category or a landing page to warn users about adult content. On the shop I'm working on there is an "Adult Section" Category for Erotic Toys, Lingerie, cosplay, etc. and I need to warn users about such content before they access the section in case they're less than 18.
I've searched the web and only found paid extensions which I cannot afford. Does anyone know how to do this?
I'm using Magento CE 1.7.
Thanks,
EDIT:
Ok, so I've been trying to figure out how to do this and I just figured that the best way to do it would be to just create a Category Landing Page and adding a java script for T&C redirect to the page I want it to.
This is the code I would use for the Category Landing Page with blank content would be this.
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_collectionSize = $_categories->count() ?>
<div>
<?php $i=0; foreach ($_categories as $_category): ?>
<?php
$layer = Mage::getSingleton(‘catalog/layer’);
$layer->setCurrentCategory(Mage::getModel(‘catalog/category’)->load($_category->getId()));
$helper = Mage::helper(‘catalog/category’);
?>
Then I found a code snippet that would redirect the user to a page of my liking after clicking accept but I'm not sure how to implement it and/or if this is the best choice. Here's the code.
<form action="url-to-go-to" method="GET" onsubmit="return checkCheckBox(this)">
I accept: <input type="checkbox" value="0" name="agree">
<input type="submit" value="Continue">
<input type="button" value="Exit" onclick="document.location.href='BACKTOWHAT.html';">
</form>
<script type="text/javascript">
<!--
function checkCheckBox(f){
if (f.agree.checked == false )
{
alert("Please tick the box to continue");
return false;
} else
return true;
}
-->
</script>
Now I was thinking that it would be best to just create a simple CMS page and point the category on the top menu to that page and once they have agreed and clicked on continue, they could be taken to the actual category by simply pointing the 'onclick' to the URL of the actual category.
Not sure if this is the best way but it's the only way I could come up. The other way I thought of would've required me to take the "agreements" during checkout that already comes with magento and make an extension that would allow me to place it on any page and call the "Agreement ID" from the Sales/Terms and Conditions tab but I don't know how to actually do that, it was just a thought.
If anyone has a better solution, I'd be happy to hear it.
I would add some sort of JavaScript that shows a div over the top of the whole page, that will create a session cookie when you accept to watch the category. Otherwise, make a back() in the history navigation. This way you don't need another page, just JS.
On another note, could you please post the extensions you found that do this?

How can I get the CMS page id of a particular page in Magento

In magento by using this code:
$currentPageId =$this->getRequest()->getParam('page_id');
we can get the current page id.
But how can I get the page id of a particular page?
For example, I have a page with URL key about-fruit-store.
I want to get its page id. How can I get it?
Either
$model = Mage::getModel('cms/page')->load('about-fruit-store','identifier');
var_dump($model->getData());
var_dump($model->getPageId());
or
$model = Mage::getModel('cms/page')->getCollection()
->addFieldTofilter('identifier','about-fruit-store')
->getFirstItem();
var_dump($model->getData());
var_dump($model->getPageId());
should do it.

Magento: Random "Featured Product"

My client needs a a small box on CMS pages and on Category landing pages that will show thumbnail/price/short description of a random item related to that category (separate from the grid view)
Any thoughts on what would be the best way to accomplish this?
Thanks,
-Sam
go to template/catalog/product/view/ and make a new phtml file random_product.phtml with the following code
<?php
$catId = $this->getCat_id();
$cat=Mage::getModel("catalog/category")->load($catId);
$prodCollection = $cat->getProductCollection();
$pids=array();
foreach($prodCollection as $product)
{
array_push($pids,$product->getId());
}
$randProductId=array_rand($pids);
echo $randProductId;
?>
now if your category id is for example 10, make a static block and paste the following code in the contents
{{block type="catalog/product" cat_id="10" template="catalog/product/view/random_product.phtml"}}
now when you will view the static block, you will see a random product id every time you refresh.
THen,you can write your own custom html in the phtml file after loading the product.
To load your product from here you can do $product = Mage::getModel('catalog/product')->load($randProductId); then call methods such as $product->getName() etc to get the details you need to output.

Rewriting URL's in codeigniter with url_title()?

I am rewriting my website with codeigniter and have something I want to do but not sure it is possible.
I have a gallery on my site powered by the Flickr API. Here is an example of the code I use to display the Landscape pictures:
<?php foreach ($landscapes->photoset->photo as $l->photoset->photo) : ?>
<a href="<?=$l->photoset->photo->farm?>/<?=$l->photoset->photo->server?>/<?=$l->photoset->photo->id?>/<?=$l->photoset->photo->secret?>/<?=$l->photoset->photo->title?>">
<img class="f_thumb" src="<?=$l->photoset->photo->farm?>.static.flickr.com/<?=$l->photoset->photo->server?>/<?=$l->photoset->photo->id ?>_<?=$l->photoset->photo->secret ?>_s.jpg" title="<?=$l->photoset->photo->title?>" alt="<?=$l->photoset->photo->title?>" />
</a>
<?php endforeach; ?>
As you can see when a user clicks on a picture I pass over the Farm, Server, ID, Secret and Title elements using URI segments and build the page in the controller using
$data['farm'] = $this->uri->segment(3);
$data['server'] = $this->uri->segment(4);
$data['id'] = $this->uri->segment(5);
$data['secret'] = $this->uri->segment(6);
$data['title'] = $this->uri->segment(7);
Everything works and is fine, but the URL’s are a tad long. Example:
http://localhost:8888/wip/index.php/gallery/focus/3/2682/4368875046/e8f97f61d9/Old_Mill_House_in_Donegal
Is there a way to rewrite the URL so its more like:
http://localhost:8888/wip/index.php/gallery/focus/Old_Mill_House_in_Donegal
I was looking at using:
$url_title = $this->uri->segment(7);
$url_title = url_title($url_title, 'underscore', TRUE);
But I can’t seem to be able to get it to work. Any ideas?
Are the links linking internally? Or are they linking to flickr? If it's internally, I'd suppose you could pass all the requisite information via POST. If it's to flickr, I don't think there is a way you can control how they want requests to come in for a specific item.
What's the endgame/purpose of all that extra info (secret, server, etc)?

Resources