Multiple select values to render static Block Content - php

I've managed to render static blocks through a custom attribute on a per product basis, which is great, however it only seems to work with drop downs, I would like to use a multi select so i could allow the administrator to select multiple static blocks in one are, rather than have multiple drop down menus.
here's the code for the drop down
<?php
$cmsstatic=$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue ($_product);
echo $this->getLayout()->createBlock('cms/block')->setBlockID($cmsstatic)->tohtml();
?>
I managed to get the value of the attribute options out for a multi select:
<?php if($_product->getResource()->getAttribute('product_featured_attribute_3')->getFrontend()->getValue($_product)): ?>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
but i'm having problems getting it to output the value as a static block. Any ideas?

as i understood you want to echo all the block content which id's are defined in product attribute.
For that you can try this:
<?php
$cmsstatic=$_product->getResource()->getAttribute('attributename')->getFrontend()->getValue($_product);
$blockids = explode(",", $cmsstatic);
foreach($blockids as $kry=>$value)
{
echo $this->getLayout()->createBlock('cms/block')->setBlockID($value)->tohtml();
}
?>

Related

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

Dynamically populating wordrpess shortcode from an array

I am trying to customize output of wordpress smart grid plugin. My aim is to add list of images into a section and output them through the plugin. The image list has to be wrapped by the plugin shortcode. Which I have done, only problem is looping. Here is my code
<div class="p_details_right">
<?php
//print_r($partner_pictures);
$pic_bucket = []; //empty array to hold list of image id to be used within the wordpress default gallery shortcode
?>
<?php
foreach ($partner_pictures as $key => $item) {
array_push($pic_bucket, $key); // populating the array which holds image id
?>
<?php
}
//print_r($pic_bucket);
?>
<?php echo do_shortcode("
[smart-grid]
[gallery ids='$pic_bucket[0],$pic_bucket[1],$pic_bucket[2]'] // <----- PROBLEM . Currently doing it statically but I need to be able to add populate the id element of the shortcode dynamically based on the $pic_bucket array.
[/smart-grid]
"); ?>
</div>
Currently outputting the gallery by statically adding array element but I need to be able to add populate the id element of the shortcode dynamically based on the $pic_bucket array. Tried to do looping but does not work. I am missing something very basic.
Sollution
convert Array to string and assign ids variable with that.
<div class="p_details_right">
<?php
//print_r($partner_pictures);
$pic_bucket = [];
?>
<?php
foreach ($partner_pictures as $key => $item) {
array_push($pic_bucket, $key);
?>
<?php
}
//print_r($pic_bucket);
$str = implode(',',$pic_bucket);
?>
<?php echo do_shortcode("
[smart-grid]
[gallery ids='$str']
[/smart-grid]
"); ?>
</div>
You can join an array of ID's together with implode(',',$array)

Filter articles by tag inside a category - Joomla and K2

I would like to make a custom module with a list of tags. When a tag is clicked the visitor would be navigated to a category page that would show articles with that tag.
I am not a joomla expert, I was thinking about a solution like a hyperlink like this that I would add to the tags inside the module:
href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places"
Is this possible? Or how could I achieve this result?
Thanks!
This is a bit more complicated than just a query string in the URL as you also need to tweak a template.
If you want to keep it as simple as possible, I'd would recommend creating a new K2 template using template overrides and editing the category template so that it would read the query string parameters and show only articles already filtered by the category and furthermore by the tag via a query string.
That's just a brief how-to, now with a lil bit more details:
1) Create a new K2 template using template overrides.
In your template, if it doesn't exist already, create a folder structure /templates/your_template/html/com_k2/templates/default. The "default" can be replaced with any name if you want to have more K2 templates, but you have to set the new template to each category you have manually.
Now take the content from "/components/com_k2/templates/default" and copy it to the new folder in your template. Now, K2 is using the templates from your /templates/your_template/html/com_k2/ folder. Feel free to google more details if you don't understand template overrides, it's pretty important thing when customizing a template.
2) Edit your category view file to accommodate the list to your query strings
The file that interests you now is in /templates/your_template/html/com_k2/templates/default/category.php. Open this file and try to understand what's important there:
Line 141
<?php foreach($this->leading as $key=>$item): ?>
Line 169
<?php foreach($this->primary as $key=>$item): ?>
Line 197
<?php foreach($this->secondary as $key=>$item): ?>
Line 226
<?php foreach($this->links as $key=>$item): ?>
This is what matters. In these four foreach loops, there are all the items. Then, you can wrap the content of each of these loops into an if-condition to check whether it has the desired tag that is specified in the URL.
To show you an example, this is the code for <div id="itemListPrimary">. You can replace this whole div in the category.php file with the following code and it will work flawlessly. I've just written and tested it.
<div id="itemListPrimary">
<?php foreach ($this->primary as $key=>$item): ?>
<?php
# Get the value of the "tag" query string
$jInput = JFactory::getApplication()->input;
$myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
//$myTag = JRequest::getVar('tag'); // for Joomla 1.5
# If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
if (empty($myTag)) {
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
# Otherwise the tag is set so we'll filter the articles by the tag
} else {
# Get an array of all the tags that the current article in the loop has
$articleTags = array();
foreach ($item->tags as $tag) {
$articleTags[] = $tag->name;
}
# Check if the article has the tag specified in the URL as a query string
if (in_array($myTag, $articleTags)) {
# Now the default content of the foreach loop comes as written in the default K2 category.php template
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
}
} ?>
<?php endforeach; ?>
</div>
3) Understand how the URLs will work
My typical category URL is:
http://mywebsite.com/category-name
To show only articles with a specified tag, use:
http://mywebsite.com/category-name?tag=your-tag
For instance, if you want to show only articles with the "Tokio City" tag, use:
http://mywebsite.com/category-name?tag=Tokio City
Done.
That's the basics of what you needs. It's all you need if you use primary articles only (no leading and secondary or links). Of course there are plenty more things you might want to take care of:
a notice if there is no article with the specified tag
no redundant code, I've written it like this for the sake of simplicity and readability
SEO - spaces and special characters in URLs
making sure no empty div will be printed
But that would be way more code and I wanted to keep it simple & readable for you. I think I gave you more than enough for a start, so go ahead and get it done, good luck :)

Magento Custom Option If Statement

I a sorry for the poor question but I have no idea where to start.
Basically I have a product template that utilises 15 custom options for Name, 15 for Tshirt size and 15 for tshirt number.
Using jquery the options are hidden on the personalisation based on the product quantity. It then puts a default * or none for the size.
The only problem is that if a customer orders say 3 tshirts in the emails and customer orders they will see a long list of options that they have not selected.
I was wondering if there was a way to exclufe the option if it equaled * or none.
SOLVED WITH THE BELOW
<?php
$optionvalue = $option['value'];
$array = array('None','*');
if(!in_array($optionvalue,$array)){
echo $option['label'];
}
?>
<?php
$optionvalue = $option['value'];
$array = array('None','*');
if(!in_array($optionvalue,$array)){
echo (isset($option['print_value']) ? $option['print_value'] : nl2br($this->escapeHtml($option['value'])));
}
?>
This is possible.
You'll have to modify the template of the email.
The base one is here :
magento\app\design\frontend\base\default\template\email\order\items\order\
This is the one for orders, don't forget about invoice, shipment and creditmemo.
<?php foreach ($this->getItemOptions() as $option): ?>
<dt><strong><em><?php echo $option['label'] ?></em></strong></dt>
<dd style="margin:0; padding:0 0 0 9px;">
<?php echo (isset($option['print_value']) ? $option['print_value'] : nl2br($this->escapeHtml($option['value']))) ?>
</dd>
<?php endforeach; ?>
just add a control on the $option['value'] or $option['print_value']
Maybe also the Pdf function to have the same behaviour on the pdfs will need modifications
Of course as it's native core function, don't modify directly the code but create your module to extends and replace it.
Solved by using an array and if statement and with dagfr's pointer to the location of the function. Have updated the PDF functions as well as emails and on screen on the carso looks pretty tidy now.
<?php
$optionvalue = $option['value'];
$array = array('None','*');
if(!in_array($optionvalue,$array)){
echo $option['label'];
}
?>
<?php
$optionvalue = $option['value'];
$array = array('None','*');
if(!in_array($optionvalue,$array)){
echo (isset($option['print_value']) ? $option['print_value'] : nl2br($this->escapeHtml($option['value'])));
}
?>

Add a unique ID to Drupal Views list groupings

I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>

Categories