Magento2 custom price render on product page - magento2

How to format the product view page price differently from the one from category page ( and possible others ) ?
If I change in my child theme the:
app/design/frontend/VENDOR/my_theme/Magento_Catalog/templates/product/price/final_price.phtml
The price is being changed on both pages (category and product);
I have tried several approaches but it seems that this price rendering mechanism is complicated as hell in Magento2.
Thanks!

<?php
try {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get(‘Magento\Framework\App\Action\Context’)->getRequest();
$currentPageXML = $request->getFullActionName();
echo $currentPageXML; //catalog_product_view is product detail page controller action
} catch (Exception $e) {}
?>
The above code will help you get the current area of the environment being accessed, and then you can apply a logic
if ($currentPageXML=="catalog_product_view") {
// render my custom need
} else {
// keep the actual code intact.
}
You are facing a problem because same phtml file is being used by both the catalog and product page, so a change in this file will be reflected everywhere.
I hope this will help you.

Related

Views from the module won't display (CodeIgniter HMVC)

I'm actually working in an admin panel, and I want to manage all my sliders inside it,
so I created all the functions etc...
In the view file where I want to call the view from the module by running
<?php
echo Modules::run('sliders/_slider_display);
?>
But, The the sliders won't display, I will show you how I did it.
function _slider_display() {
$current_url = current_url();
$query_ads = $this->get_where_custom('target_url', $current_url);
$num_rows_ads = $query_ads->num_rows();
if($num_rows_ads>0){
$this->load->module('slides');
foreach($query_ads->result() as $row)
{
$parent_id= $row->id;
}
$data['query_slides']= $this->slides->get_where_custom('parent_id', $parent_id);
$this->load->view('sliders', $data);
}
}
as I mentioned, I tried to call the view in the homepage, but did not worked, I don't know what is the problem.
Thank you !

How can I render preview of FE plugin diplayed in Page module

I have developed TYPO3 (6.2) extensions with some FE plugins.
I need to change informations about plugin, which is displayed in the backend on page view.
Now only Title and name of plugin is displayed ...
I have used flexforms for configure the plugin and I would like to show some of configuration on the plugin "placeholder" in the backend.
I remember, I read some documentation how to do it a few years ago, but I can't find it anymore...
Does anyone know the right way to do it?
If I understood well you are asking for ContentElement preview. You need to use cms/layout/class.tx_cms_layout.php hook for this, here's quite nice gist
just two additions:
don't use t3lib_extMgm class it's removed since 7.x you can register this hook just with:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$_EXTKEY]
= 'EXT:your_ext/Classes/Hooks/PageLayoutView.php:\Vendor\YourExt\Hooks\PageLayoutView';
Depending on how did you register the plugin (didn't mention) you can also need to check the $row['list_type'] as your $row['CType'] may be just generic list.
Sample class with value from FlexForm field
<?php
namespace Vendor\YourExt\Hooks;
class PageLayoutView implements \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface {
public function preProcess(\TYPO3\CMS\Backend\View\PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row) {
if ($row['CType'] == 'list' && $row['list_type'] == 'yourext_yourplugin') {
$drawItem = false;
$linkStart = '<a href="#" onclick="window.location.href=\'../../../alt_doc.php?returnUrl=%2Ftypo3%2Fsysext%2Fcms%2Flayout%2Fdb_layout.php%3Fid%3D' . $row['pid'] . '&edit[tt_content][' . $row['uid'] . ']=edit\'; return false;" title="Edit">';
$linkEnd = '</a>';
$headerContent =
$linkStart .
"<strong>Selected slides</strong>" .
$linkEnd;
$ffXml = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row['pi_flexform']);
$itemContent =
$linkStart .
$ffXml['data']['sDEF']['lDEF']['settings.myFlexField']['vDEF'] .
$linkEnd;
}
}
}

Symfony 2 Forms - add new item to Collection server-side, depending on button clicked

I have a form which contains a Collection of an unspecified number of subforms. I want to have functionality allowing the user to add a new, blank item to the Collection for them to fill in. The Symfony docs tell us how to do this using Javascript clientside to add new blank form controls, which are then submitted and persisted as normal, but I'd like to do it serverside in the controller, without Javascript.
The problem I'm encountering is to do with the way Symfony Forms work. I have an "Add" button added to my main form, and I intend to detect whether it is that button which has been clicked, so that I can add the blank item to the Collection and re-render the form. But to detect the click I need to call $this->createForm and at that point the form is fixed with the original set of items, it's too late to add an extra one.
//Symfony Action
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
$form->handleRequest($request);
//Ideally I'd somehow do this test earlier, but I need $form to do it...
if ($form->get('add')->isClicked() )
{
//TOO LATE!
$person->getSelections()->add(new Selection() );
}
if ($form->isValid())
{
if ($form->get('save')->isClicked() )
{
//Persist
}
}
//Render page etc
Things I've thought about:
Putting the Add button in a completely different form on the same page, which submits to a different Action which can then do some preparatory work before forwarding to the main Action above
Inspecting submitted HTTP data directly to note that Add has been clicked (shame not to use the standard Symfony method)
Give up and use Javascript as suggested (it might work in this example, but I'd like to have the option of carrying out server-side activity (without AJAX...) as part of adding the new blank item)
How can I best achieve this in a proper Symfony way?
EDIT Just seen this: https://github.com/symfony/symfony/issues/5231, which is essentially a feature request to allow what I'm after. One suggestion a commenter makes is to add a blank item to the Collection and then remove it if it's not needed - I don't know how one would do that, but it sounds promising.
ANOTHER EDIT It occurs to me that, because I need two different aspects of the $form I'm creating, I could maybe just make the $form, use it to handle the request, detect the button click, and then throw that $form away, before altering my model and creating another $form. I don't know if that would somehow fall foul of some rules about handling the submission twice.
I'm not 100% but I think you could do the following...
//Symfony Action with (Request $request, ...)
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->get('add')->isClicked()) {
// Add thing
} elseif ($form->isValid()) {
// or
// } elseif ($form->get('save')->isClicked() && $form->isValid()) {
// Persist and what not
}
}
//Render page etc
I haven't tested it so I don't know whether it will trigger the form errors (or if it will actually work) so if it does (or it doesn't) I apologise.
What I did in the end was have my Add button hit a separate Action, which then delegates to the main action with a flag to say "add a new Selection", as below:
public function selectionsAddAction(Request $request, $id)
{
return $this->selectionsAction($request, $id, true);
}
public function selectionsAction(Request $request, $id, $addNew = false)
{
$person = $this->getPerson($id);
//Also use "add mode" if we just deleted the last one!
if (!$person->getSelections()->count())
{
$addNew = true;
}
//$addNew is set by a separate action, hit by a different form with the Add button in
if ($addNew)
{
$person->getSelections()->add(new Selection() );
}
//We now have the right number of items, and can build the form!
$form = $this->createForm(new SelectionsType($lookups), $person);
//...
}

Joomla: Plugins that modify Categories

I am trying to make a plugin for Joomla that mimics all the changes you do on content categories in a menu item. So adding, deleting and editing the name of a category in a specific article will also make the same changes on a menu item.
A content plugin has events such as onBeforeContentSave and onAfterDisplayContent that allow you to process that data. How do I do the same thing for categories?
Unfortunately, there isn't an onCategorySave event. The best approach I can think of would be to create a system plugin and check the task and option request variables for values of save and com_categories. Your plugin would look something like this:
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');
class plgSystemCategorysave extends JPlugin
{
function onAfterInitialise()
{
if (!JFactory::getApplication()->isAdmin()) {
return; // Dont run in frontend
}
$option = JRequest::getCmd('option', '');
$task = JRequest::getCmd('task', '');
if ($option == 'com_categories' && $task == 'save') {
// your processing code here
}
}
}

Way to define others layout() parts such layout()->content?

Ive done some search but no success.. i am trying to figure out how to define others layout() parts such layout()->content variable.. i would love to get int layout()->navigation (a custom one) which display the navigation..
Any ideas ?
Thanks.
Not sure if this is what you want, but you can create additional 'parts' of layout simply by assigning a value to your new part. ZF will take care of the rest. For example, in a bootstrap.php you could do:
public function _initNewLayoutPart() {
$view = $this->bootstrap('view')->getResource('view');
$view->layout()->newpart = 'some new part';
}
Then in your layout.phtml you could just echo the new part:
<?php echo $this->layout()->newpart; ?>
It is possible by just creating a new variable in layout, you can define it in your controller (preferably in init or postDispatch). Just like this:
public function init()
{
$this->view->layout()->motd = '<b>Message of the day.</b>';
}
Then in your actual view where you want to see the message, all you have to do is:
<?php echo $this->layout()->motd; ?>
If you want something fancier, such as rendering a whole page or sidebar, try the following:
public function init()
{
$this->view->layout()->sidebar = $this->view->action('render', 'sidebar');
}
With render being the action (including render.phtml) and sidebar being the controller.