Hi i am trying to use attribute value as a static block identifier.
i have an attribute called designer. In my tabs on product page i want to display a static block depending on the value of this attribute
echo $this->getLayout()->createBlock('cms/block')->setBlockId('**designer**')->toHtml();
so i would like designer to be substituted with the value for the specific product, so if i have a product where the attribute value is designer-1 i would like it to be
echo $this->getLayout()->createBlock('cms/block')->setBlockId('designer-1')->toHtml();
and so forth
If you are outputting this in your template (while viewing product page ): Try below:
$product = Mage::registry('current_product');
$attributeText = $product->getAttributeText('designer');
//For uniformity of block identifiers, make it all lower case and make sure you are using lower case as block name
$lowerCaseValueForBlockId = strtolower($attributeText);
$cmsBlock = Mage::getModel('cms/block')->load($lowerCaseValueForBlockId);
if ($cmsBlock->getId()) {
echo $this->getLayout()->createBlock('cms/block')->setBlockId($lowerCaseValueForBlockId)->toHtml();
//Or you can use below code
echo $cmsBlock->getContent();
}
Related
I have a dropdown select field to select a custom posttype within the woocommerce product edit screen and need to output the ID of this selected post.
This code works on my frontend but having issues to get it working with other plugins (here wplister).If i hardcode the selected post ID all works fine.
function psg_table_frontend_shortcode($atts, $content = null) {
global $psg_global;
/* START creating HTML Object */
ob_start();
?>
<p>
<?php
echo "The ID of the selected sizing Guide (cpt) inside the Product : " .$psg_global['psg-selected-sizing-guide-on-productpage'];
?>
</p>
$output = ob_get_clean();
/* END creating HTML Object */
return $output;
}
add_shortcode('psg_frontend', 'psg_table_frontend_shortcode');
Spoke to the support of wplister but they said it is probably an issue with the reduxframwork and the global variable.
Any idea?
if you are in a function, who knows if your psg_global is even in scope yet... so try this:
$psg_global= get_option( 'psg_global'); // or whatever your opt name is
$productpage = $psg_global['psg-selected-sizing-guide-on-productpage'];
echo $productpage;
also, might want to use 'psg_selected_sizing_guide_on_productpage' with underscores instead of 'psg-selected-sizing-guide-on-productpage' because that way you can do this:
extract($psg_global)
and all your vars would be in scope.
for example:
echo $psg_selected_sizing_guide_on_productpage
I'm working with a custom shortcode that generates a custom paypal button & passes specific parameters to a paypal checkout form.
I'm using the str_replace function to pass attributes defined in the shortcode to the html form.
I'd like to set a default style class & default title if not defined as an attribute in the shortcode.
My php:
function paypal_button_func($attrs){
$class=$attrs['class']; //Added button style class variable
$title=$attrs['title']; //Added button title variable
if( isset( $atts['title'])) //add button title to the form html
{
return $html=str_replace('[title]',$title,$html);
}
//This sets the default title if not defined
else {
return $html=str_replace ('[title]','SIGN UP NOW',$html);
}
And this is my html
<input type="submit" class="[class]" value="[title]" /> <!-- updated class and value for addtl shortcode parameters -->
I was successful with using the following code to pass the shortcode attributes to the html, but the problem is that if the attributes aren't defined in the shortcode it outputs the [class] and [title] as values, which is not what i want to happen.
$html=str_replace('[title]',$title,$html); //add button title assigned to the form
$html=str_replace('[class]',$class,$html); //replaces class assigned to the form
simply make sure to initialize your replacements properly, i suggest using an if/else shorthand when creating your vars:
$class = isset($attrs['class']) ? $attrs['class'] : 'default-class-name'; //Added button style class variable
$title = isset($attrs['title']) ? $attrs['title'] : 'SIGN UP NOW'; //Added button title variable
this way your html will always get a replacement, either the attrs value or the defaults.
also i noticed you have a typo checking on isset($atts['title']) where you probably want $attrs and not $atts
good luck!
I have created a custom block "blockpre" in totara which will show the results of assignments.In edit_form.php of my block blockpre i added some extra elements in mform for my block.
in edit_form.php i have created a class "block_blockpre_edit_form" which extends "block_edit_form" (standard class). I have added standard elements in mform and also 1 extra element.
The problem is that i'm unable to access the value of standard as well as extra element added.
i have tried using $mform->get_data on view page of my block but its not working on any of the block pages.
Is there some standard way to access the value of mform element ?
edit_form.php
<?php
class block_blockpre_edit_form extends block_edit_form {
protected function specific_definition($mform) {
// Section header title according to language file.
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
// A sample string variable with a default value.
$mform->addElement('text', 'config_text', get_string('blockstring', 'block_blockpre'));
$mform->setDefault('config_text', 'default value');
$mform->setType('config_text', PARAM_MULTILANG);
$mform->addElement('header', 'configheader', get_string('blocksettings1', 'block_blockpre'));
global $CFG, $OUTPUT,$DB,$USER;
$courseid = $_REQUEST['id'];
$sel="select id,name from {assign} where course=".$courseid;
$sel1= $DB->get_records_sql($sel);
foreach($sel1 as $s)
{mform element
$dis=$s->id;
$namem=$dis;
$mform->addElement('advcheckbox','assignid',$s->name,null, array('name' => $namem),$namem); //extra element
}
}
}
?>
Thanks for help in advance.
You can display form data with $mform->render();
Or use the data internally with $mform->set_data();
Duplicated attriubte set for new product line.
Attributes set show in backend and front end.
Created new attribute and added to attribute set.
New attribute show's in back end but not front end.
If I add the new attribute to the origonal set that was duplicated the new attribute will now show on the front end for the product assoiated with the duplicated attribute group.
So basicly A new attribute added to a duplicate set won't show on the front end until its added to the attribute set that was duplicated.
I've checked to make sure the attribute is visable on front end etc and tried it several times checking the settings.
The goal is to be able to duplicate a attribute set and add new attributes for different product types. Then call the folder by id and display the assoiated attributes.
I've called the attribute group by ID (spec's). This code is working.
<?php
require_once 'app/Mage.php';
Mage::app();
$attributeGroupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
$product = $this->getProduct();
foreach ($attributeGroupCollection as $attributeGroup) {
$attributeGroup->getAttributeGroupId();
$attributeSpecs = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter(41);
}
?>
Help is appreciated, thanks
Duplicating an attribute set is duplicating at a time (the moment when you click the button). If you want to add an attribute in both attribute sets, you need to create it in both, for that, you just need to create your attribute and drop it in the section you want to have it linked to (what you call "folder"). If you want to do it programmatically. You need to create an observer that will be fired after the attribute set is saved and that will add the attribute to the duplicated attribute set. I would not advise to do so because it means that you need to enter the ID of the attribute set duplicated and it can be tricky thing when it comes to pushing the development to the production. I am sure there is a workaround for what you want to achieve.
The problem was is magento creates a new id for each new group folder in the new duplicated attribute set (makes sense). I was calling by ID for the group and as a result will only show attributes in the origional folder (weird) even if the product was assoicated with the new attribute set. What I did was get the current attribute set id and then sort out the attribute groups by name so even if the attribute sets are coppied as long as the folde has the name it will display in the custom loop displaying the attributes. Here is my working code:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
$attributeSetID = $attributeSetModel->getAttributeSetID();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetID)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
if (strpos($groupName , 'Specifications') !== false) {
echo 'true';
$specificationsNum = $groupId;
};
if (strpos($groupName , 'eatures') !== false) {
echo 'true';
$prodfeaturesNum = $groupId;
};
}
//echo $specifications;
$specifications = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($specificationsNum);
$prodfeatures = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($prodfeatures);
Hi i have got the maximum elements of products using below code but it doesn't show the size attribute of my product, the size attribute is visible on front-end but i cant understand why it is not printing with this code
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId); //load the product
//$product_id = $product->getId();
//$created_at = $product->getcreated_at();
//$description = $product->getdescription();
//$short_description = $product->getshort_description();
//$sku = $product->getsku();
//$size_fit = $product->getsize_fit();
//$style_ideas = $product->getstyle_ideas();
//$name = $product->getname();
//$price = $product->getprice();
//$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
If its the size_fit attribute (i'm guessing that because its the only size attempt in your code..) use $product->getSizeFit(). For just size use $product->getSize(). When this is not returning anything, please post the attribute installer if you have one. Mufadall his answer is also correct but judging your code you are just using wrong syntax.
Basicly according to the magic get method the first letter is turned into a capital and all other letters after an underscore.
Ex.: To fetch my_sample_attribute use getMySampleAttribute().
getData('my_sample_attribute') would also be an option but you shouldn't make a habbit of doing that because in some cases, for some attributes getData('attribute') returns a different value then getAttribute()....
$product->getData($attribute_code);
will return you the actual attribute value. For attribute with type dropdown it will return option id
$product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
will return actual value
You can use this:
$product->getData('Your Attribute ID');
Go to Size attribute and check for Drop Down used in product listing if it set to No then set it to Yes, after that you can get your size attribute with other product attribute
You can use the getters, or getData();
The getter is set in magento with the magic __get() method, and you can use it in the following way:
$product->getDescription() // ( to get description attribute)
$product->getShortDescription() // (to get short_description attribute)
So basically you explode the attribute with underscores, and capitalize the words, and you will get what you need to put after "get".
Here is something very useful I use all the time
Zend_Debug::dump($product->getData());
This gets you all the data you have to work with. If you are missing data, it means it's not loaded.
Good luck!