Programmatically remove block from layout - magento

I want to remove the product_options_wrapper block from the product view page according to the logedin user via frontend router controller.
I know that I can programmatically append a new block but I didn't find a remove function. :-(
Tried sth. like that
$this->getLayout()->unsetBlock('product_options_wrapper');
$this->getLayout()->getBlock('product.info')->remove('product_options_wrapper');
But nothing works.

Inorder to remove a block using its parent block use the code below
$this->getLayout()->getBlock('product.info')->unsetChild('product_options_wrapper');

The OP code should work, if it used the correct block name, which is product.info.options.wrapper, as opposed to the block alias.
$this->loadLayout();
//e.g.
if (Mage::getSingleton('customer/session')->getCustomerGroupId() == [id]){
$this->getLayout()->unsetBlock('product.info.options.wrapper');
}
$this->renderLayout();

This should work:
$blockName = 'left'; // Add yours
$update = Mage::app()->getLayout()->getUpdate();
$removeInstruction = "<remove name=\"$blockName\"/>";
$update->addUpdate($removeInstruction);
Why? Have a look in the file Mage_Core_Model_Layout in the method generateXml() the XML is parsed and where a remove is set for a block, the attribute ignore is added to the block. In the method generateBlocks() all the blocks which have that attribute are not added.

Related

Get segment from url CodeIgniter but not first

I know i can get all segments from url like this
Lets say i have this example link
www.example.com/de/products.html
Using url_helper like this:
$data['url'] = $this->uri->uri_string();
I will get value like this
de/products
But i dont need first segment de, only products, the problem is that
i dont know how many segments it will be, i only need to remove the first
Is there possible to forget first segment with url helper in CI?
Try like this...
Use the php's explode() function to make the url string as array.Then apply array's array_shift() function which always removes the first element from array.
Code is looks like as below
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
Then use the php's implode() method to get the URI without first segment.Hope it will works...
$uri=implode('/',$arr);
echo $uri;
There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put #Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.
e.g.
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
Before Edit answer.
You need to try this
$second_segment = $this->uri->segment(2);
From Codeigniter documentation -
$this->uri->segment(n);
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1. news
2. local
3. metro
4. crime_is_up
The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);
example:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>

How to fetch block's label in xml from phtml in Magento?

I have a custom block in my layout file like this:
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<label>Foo</label>
</block>
How do I get the value of label from bar.phtml ?
Please note I do not want to use setData function to set my variable and pass it.
I want to extract the value inside tags from the phtml (or anywhere else). I hope its clear.
I don't think there is a really classy Magento way of doing it since the label of a block purpose is not to be displayed as far as we speak of the frontend.
label: This element is introduced since Magento 1.4. It defines the label of the handle which is shown as a descriptive reference in some areas of the admin panel.
Source
I would really warmly recommend you to stay away from the code below. But if that is really what you want to achieve, this is a way :
First we get the layout = a big xml concatenation of the layout for that page containing the xml where the block is defined, and thus our label
$layout = $this->getLayout();
Then we get the current block name in the layout
$currentBlockNameInLayout = $this->getNameInLayout();
We can, then get the XML node representing the current block in the template.
getXpath() does returns an array, so that is why I used list() to get the first item off of this array
list($currentBlockInLayout) = $layout->getXpath("//block[#name='".$currentBlockNameInLayout."']");
We have what we want and can echo its label element
echo $currentBlockInLayout->label;
Take care though, this is an object of type Mage_Core_Model_Layout_Element so if you want to do anything else than displaying it, you would have to use the __toString() method
var_dump( $currentBlockInLayout->label->__toString() );
Full code :
$layout = $this->getLayout();
$currentBlockNameInLayout = $this->getNameInLayout();
list($currentBlockInLayout) = $layout->getXpath("//block[#name='".$currentBlockNameInLayout."']");
echo $currentBlockInLayout->label;
var_dump( $currentBlockInLayout->label->__toString() );
In your XML, use the action method setData
<block type="xxx/xxx" name="xxx" template = "bar.phtml">
<action method="setData">
<label>Foo</label>
</action>
</block>
Then in your bar.phtml file, you can retrieve it using $this->getData('label'):
<?php echo $this->getData('label') ?>

How to get a url parameter in Magento controller?

Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
I know I can split the url on "/" to get the value, but I'd prefer a single function.
This doesn't work:
$id = $this->getRequest()->getParam('id');
It only works if I use http://example.com/path/action?id=123
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
So when you call
http://example.com/path/action/id/123
The word path is your front name, action is your controller name, and id is your action method. After these three methods, you can use getParam to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
You may also use the getParams method to grab an array of parameters
$this->getRequest()->getParams()
If your url is the following structure: http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/
then use:
echo $this->getRequest()->getParam('order_id'); // output is 1795
If you want to get All Url Value or Parameter value than use below code.
var_dump($this->getRequest()->getParams());
If your url is like this: http://magentoo.blogspot.com/magentooo/userId=21
then use this to get the value of url
echo $_GET['userId'];
If you want more info about this click here.
If it's a Magento module, you can use the Varien Object getter. If it's for your own module controller, you may want to use the register method.
Source: http://www.vjtemplates.com/blog/magento/register-and-registry

Output of getPaymentHtml() in Magento

I haven't been able to find where does the output of getPaymentHtml() comes from.
Its defined as:
public function getPaymentHtml() {
return $this->getChildHtml('payment_info');
}
I couldn't find out the template for payment_info block.
Basically I want to be able to retrieve credit card type and credit card number in the progress block of checkout.
How do I find out the method names? Something like $this->getCreditCardType()
Edit: OK! I understand that Magento figures out the payment method first which has their corresponding templates which are used to render output. But in progress.phtml of checkout, var_dump( $this instanceof Mage_Payment_Block_Info_Cc ); returns false, so how do I access that in current context?
The Progress block doen't have it's own template for Payment info. Mage_Checkout_Block_Onepage_Payment_Info block uses the selected Payment Method block to output html. Look at the Mage_Checkout_Block_Onepage_Payment_Info::_toHtml() method:
protected function _toHtml()
{
$html = '';
if ($block = $this->getChild($this->_getInfoBlockName())) {
$html = $block->toHtml();
}
return $html;
}
To find the actual template and block for the specific Payment method you use, you need to perform next steps:
First - get model alias for current payment method Mage::getStoreConfig('payment/'.$yourMethod.'/model') and instantiate it using Mage::getModel(alias)
then get block type using $model->getInfoBlockType() - so you'll be able to find the actual Block by it's type
For example for ccSave payment method the info block is Mage_Payment_Block_Info_Ccsave, and template for it is app\design\frontend\base\default\template\payment\info\default.phtml. You'll be able to find all data inside those.
Good luck ;)
For the sake of completeness, exact functions to fetch CC type and last 4 digits of CC number are:
echo Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getCcType();
echo Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getCcLast4();
The block class is declared in layout update XML; see the onepage checkout and multishipping directives from checkout.xml. The actual child block which is used depends on the payment model which is being used, but there is a common template that will be used unless overridden.
Example:
See the generic CC method model Mage_Payment_ModelMethod_Cc
From that see its info block Mage_Payent_Block_Info_Cc...
...which will lead you to the "base" payment info block Mage_Payment_Block_Info which sets a default template.

Prototype: how to dynamically construct selector?

I am having a little bit of difficulty passing a variable into a selector in prototype. I would like to be able to pass a variable into the select string, so that one function can work for many of the same kind.
At the moment, this is what I would basically like to do:
function myFunct(var)
{
$(var + 'add_form').hide() //so inde the brackets would be ('#product1 #add_form') for example.
}
Be able to pass 'var' into the function that would pass it to the selector, so that I can hide a pattern that is the same for many on the page.
Any ideas for a path to follow would be greatly appreciated.
You're on the right track! Couple things:
var is a JavaScript keyword (source), don't use it to name a variable
if you're querying an element by id (such as #add_form) you don't need to add any container element as you're doing
If you're querying an element by class, you need to use the $$ function, not the $ function
You need to iterate over the wrapped set to call your method
whitespace is significant in css selectors, so make sure to include those in your selector construction to tell Prototype to search within your parent container:
function myFunct(parent) {
$$(parent + ' .add_form').invoke('hide')
}
myFunct('#someparent'); // hides .add_form inside #someparent
That should work... just rename var to something else.
function myFunct(yourVar)
{
$$('#' + yourVar + ' .add_form').each(function(s){ s.hide(); }); // yourVar being the id of the container element
}
I've put a '.' in front of add_form because you can't use multiple elements with same ID, make it a class.

Resources