If any one want to remove container (block) like product.info.main from Product Detail Page based on certain conditions or product has attribute with value assigned.
Then what is the best approach for achieving This?
Thanks
We can use Event Observer approach...
In YOUR_VENDOR\YOUR_MODULE\etc\frontend\events.xml file, need to add below code:
<event name="layout_generate_blocks_after">
<observer name="personalize-theme-pdp-customize" instance="YOUR_VENDOR\YOUR_MODULE\Observer\ApplyThemeCustomizationObserver" />
</event>
And in YOUR_VENDOR\YOUR_MODULE\Observer\ApplyThemeCustomizationObserver.php file, need to add below code:
public function execute(Observer $observer)
{
$action = $observer->getData('full_action_name');
if ($action !== 'catalog_product_view') {
return;
}
$product = $this->_registry->registry('product');
if ($product) {
$attribute = $product->getCustomAttribute('g3d_app_url_default');
if ($attribute && $attribute->getValue()) {
/** #var \Magento\Framework\View\Layout $layout */
$layout = $observer->getData('layout');
$layout->unsetElement('product.info.main');
}
}
}
Using a site wide event to remove a container/block from a specific page is overkill and not the best approach because your condition will be evaluated with every page load, adding a slight overhead to all the pages.
Removing a container/block from a specific page is best achieved by creating an after plugin for the execute method of the controller of the page where you want to remove the container/block. With this approach your condition is only executed when the intended page is loaded.
public function afterExecute(\Magento\[Module]\Controller\[ControllerName] $subject, $result)
{
if ([your condition]) {
$result->getLayout()->unsetElement('name_of_container_or_block');
}
return $result;
}
Related
I have a custom module that uses Kentico API (DocumentHelper) to update certain fields of my document and then publish but I do not want it to trigger the event handlers that are linked to my document page type. I tried adding comments to .Publish("admin_edit") hoping that I can catch it from the WorkflowEventargs parameter but the VersionComment property always return null. Is there a way to accomplish this in Kentico?
update field:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn();
document.Publish("admin_edit");
}
event handler:
public override void Init()
{
WorkflowEvents.Publish.After += Publish_After;
}
private void Publish_After(object sender, WorkflowEventArgs e)
{
if (!string.IsNullOrEmpty(e.VersionComment) &&
e.VersionComment.Contains("admin_edit"))
return;
}
You always get null for Version information, because that is related to the 'Page versioning' events, specially for 'SaveVersion'. You can find more about that on this link. If you expand 'Properties' you will see which properties are populated for the specific event. In your case, you can try something like this, to add your message for last version and then check for that comment on 'Publish_After' event, see code bellow:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn(versionComment: "admin_edit");
document.Publish();
}
and then, in event handler, take last version and check for comment like this:
if (e.PublishedDocument?.VersionHistory?.Count > 0)
{
var lastVersion = e.PublishedDocument.VersionHistory[0] as VersionHistoryInfo;
if (lastVersion.VersionComment.Equals("admin_edit"))
{
return;
}
}
NOTE: In case that you have a lot of concurrent content editors, there is a chance that your last version is not version from API (someone changed content and saved it right after your API call made change). There is a low chance for that, but still is possible. If this is something that you will use often, you must take it in consideration. This code is tested for Kentico 11.
First - disclaimer: I know I should not do it like this, but use LazyField instead and that model should not contain logic and I will modify my code accordingly, but I wanted to explore the 1-n relationship between content items and orchard in general.
I'm creating a system where user can respond to selected job offer, so I have two content types - Job, which lists all available jobs, and JobAnswer which contains my custom part and provides link to appropriate Job content item:
public class JobPart : ContentPart<JobPartRecord>
{
public ContentItem Job
{
get
{
if (Record.ContentItemRecord != null)
{
var contentItem = ContentItem.ContentManager.Get(Record.Job.Id);
return contentItem;
}
var nullItem = ContentItem.ContentManager.Query("Job").List().First();
return nullItem;
}
set { Record.Job = value.Record; }
}
}
This works, but I'm not sure how should I handle returning a null contentItem, when creating new content item, now it just returns first Job content item, which is far from ideal.
There are only four methods for chrome.contextMenus:
create
update
remove
removeAll
I am wondering how do I check whether one menu is already created?
I tried this:
try {
chrome.contextMenus.update("byname", {});
} catch (e) {
// doesn't exist
}
But it seems the error cannot be caught (but shown in the console).
Thanks for any kind of tips!
Each chrome.contextMenus.create call returns an unique identifier. Store these identifiers in an array or hash to keep track of them.
This is a direct solution to anyone having the op's problem, based on the suggestion by Rob W. The idea is to maintain your own list of existing context menu id's.
By using these wrapper functions to maintain context menu entries, also the removal and updates are being kept track of (addressing Fuzzyma's comment).
Usage works like Chrome's own methods, eg. createContextMenu({id: "something"}, onclick). It works for me.
let contextMenus = {}
// method to create context menu and keep track of its existence
function createContextMenu() {
if (arguments[0] && arguments[0].id) {
// TODO: not sure if this will work properly, is creation synchronous or asynchrounous?
// take in to account calll back and the runtime error?
chrome.contextMenus[arguments[0].id] = chrome.contextMenus.create.apply(null, arguments);
}
}
function updateContextMenu() {
if (arguments[0] && contextMenus[arguments[0]]) {
chrome.contextMenus.update.apply(mull, arguments);
}
}
function removeContextMenu() {
if (arguments[0] && contextMenus[arguments[0]]) {
chrome.contextMenus.remove.apply(null, arguments);
contextMenus[arguments[0]] = undefined;
}
}
function contextMenuExists(id) {
return !!contextMenus[id];
}
I'm currenty working on a shipping module extension that is used for an order-import script to set to every order the same shipping cost and shipping code.
Everythings works fine but the problem that is, that the shipping method is visible in frontend. I will release this extension later in magento connect, so its not pissible to edit frontend templates.
Does anyone know how to disable the carrier in frontend without disableing the module in backend or changeing the status to inactive and without editing templates? (e.g. a custom block that declines displaying)
Thanks to everyone! Mru
EDIT:
I've tried your tip like this, but it doesn't work:
<blocks>
<checkout>
<rewrite>
<onepage_shipping_method_availible>XXX_XXX_Block_Checkout_Onepage_Shipping_Method_Available</onepage_shipping_method_availible>
</rewrite>
</checkout>
</blocks>
and created this class:
class XXX_XXX_Block_Checkout_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
public function getShippingRates()
{
if (empty($this->_rates)) {
$this->getAddress()->collectShippingRates()->save();
$groups = $this->getAddress()->getGroupedAllShippingRates();
return $this->_rates = $groups;
}
return $this->_rates;
}
}
(I don't know why it is not displayed correctly...)
Thanks for your held, MRu
EDIT2:
Sorry for being so stupid.. The above posted code would work if i were not unable to write 'availalbe'...
Thanks!
You can hide shipping method from front end with observer, write this code in config.xml
<frontend>
<events>
<sales_quote_collect_totals_before>
<observers>
<frontend_shipping_rates_sales_quote_collect_totals_before>
<class>yourmodule/observer</class>
<method>hideShippingMethods</method>
</frontend_shipping_rates_sales_quote_collect_totals_before>
</observers>
</sales_quote_collect_totals_before>
</events>
Second in Observer.php use this code
public function hideShippingMethods( Varien_Event_Observer $observer )
{
if (Mage::getDesign()->getArea() === Mage_Core_Model_App_Area::AREA_FRONTEND)
{
$quote = $observer->getEvent()->getQuote();
$store = Mage::app()->getStore($quote->getStoreId());
$carriers = Mage::getStoreConfig('carriers', $store);
$hiddenMethodCode = 'freeshipping';
foreach ($carriers as $carrierCode => $carrierConfig)
{
if( $carrierCode == $hiddenMethodCode )
{
$store->setConfig("carriers/{$carrierCode}/active", '0');
}
}
}
}
You need to overload the Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates() method
In my controller, I have a before() function that calls parent::before() and then does some additional processing once the parent returns. based on a specific condition, I want to "save" the original request and pass execution to a specific action. Here is my before() function.
public function before() {
parent::before();
$this->uri = Request::Instance()->uri;
$match = ORM::factory('survey_tester')
->where('eid','=',$this->template->user->samaccountname)
->find();
if (!$match->loaded()) {
self::action_tester("add",$this->template->user);
}
}
And the action that is being called..
public function action_tester($op=null,$user=null) {
$testers = ORM::factory('survey_tester')->find_all();
$tester = array();
$this->template->title = 'Some new title';
$this->template->styles = array('assets/css/survey/survey.css' => 'screen');
$this->template->scripts = array('assets/js/survey/tester.js');
$tester['title'] = $this->template->title;
$tester['user'] = $this->template->user;
switch ($op) {
case "add":
$tester = ORM::factory('survey_tester');
$tester->name = $user->displayname;
$tester->email = $user->mail;
$tester->division = $user->division;
$tester->eid = $user->samaccountname;
if ($tester->save()) {
$this->template->content = new View('pages/survey/tester_add', $admin);
} else {
$this->template->content = new View('pages/survey/tester_error', $admin);
}
break;
default:
break;
}
}
This all seems to work fine. This is designed to prompt the user for a specific piece of information that is not provided by $user (populated by LDAP) if this is the first time they are hitting the controller for any reason.
The problem is the views are not rendering. Instead control passes back to whatever action was originally requested. This controller is called survey. If i browse to http://my.site.com/survey and login with new user info, the record gets written and i get the action_index views instead of my action_tester views.
I cannot figure out what I am doing wrong here. Any ideas will be appreciated. Thank you.
EDIT: I managed to get this working (sort-of) by using $this->request->action = 'tester'; but I'm not sure how to add/set new params for the request yet.
The issue is that you are calling your method (action_tester), but then Kohana is still going to call the original action after the before method is called, which is going to change the response content overwriting the changed made in action_tester().
You can change the action being called (after before is called) inside your before() method:
$this->request->action('action_tester');
After the before method is called, it should then call the new Action (action_tester) rather than the old one, but then you need to do something about the way you are passing your parameters then.
Or you could just redirect the request upon some condition:
if($something) {
$this->request->redirect('controller/tester');
}
This doesn't seem like a nice way to do it anyway.