Create handle with 2 variables - magento2.2

I would like to create a layout specified for only simple product with attribute set id of 4.
currently my code is something like this:
public function initProductLayout(ResultPage $resultPage, $product, $params = null)
{
$resultPage->addPageLayoutHandles(
['attribute_set_id' => $product->getAttributeSetId()]
);
and i have this layout catalog_product_view_attribute_set_id_4.xml
its all good in the products page but my problem is that catalog_product_type_bundle.xml is not being used. So I would like to have a layout file which is like catalog_product_type_bundle_attribute_set_id_4.xml if it is possible.

It is possible to get the layout file you are talking about. To do this you should add this code in addition to one you already have
$resultPage->addPageLayoutHandles(['type_'.$product->getTypeId().'_attribute_set_id' => $product->getAttributeSetId()]);
As a result you will get this

Related

magento: category image for some are set as array and other as string

I am having method:
public function saveCategory($observer)
{
$event = $observer->getEvent();
$category = $event->getCategory();
and from there I need to collect:
$category->getThumbnail()
but I am getting result Array!!??
I went and checked $category data and i noticed that I have thumbnail like this:
[thumbnail] => 157134.jpg
and this:
[thumbnail] => Array
(
[value] => 157134.jpg
)
why like this? and how can I get my thumbnail?
EDIT
This is custom module and this is observer that is triggered on:
catalog_category_save_commit_after
It is connected with admin part.
Is this an academic question? You don't really care do you? You can just use is_array($category->getThumbnail()) to test the type and act accordingly.
It is a hard question to answer on Stackoverflow because you are not running core Magento code, are you? I can't find a reference to catalog_category_save_commit_after. What version of Magento are you using?
But this is an interesting question. I think the type of the thumbnail may be determined by the way the thumbnail information is POSTed to the Magento controller and the way that the magic getters and setters work.
It is difficult for me to investigate because I don't have access to your third party module code.
What I can do is analyse Magento core 1.7.0.0 and if you capture the POST of the save category button in the Magento admin, the category image name in the form POST is
Content-Disposition: form-data; name="general[image][value]"
your-image.png
And if you add the special inspection code that I use for analysing adminHTML into the controllerAction:
file:app/code/core/Mage/Adminhtml/controllers/Catalog/CategoryController.php
class: Mage_Adminhtml_Catalog_CategoryController
function: saveAction()
add use this code below to expose the structure of the $data object
public function saveAction()
{
//...
if ($data = $this->getRequest()->getPost()) {
//a hack to expose the $data object in the browser
$this->_getSession()->addError( nl2br(print_r($data,true)) );
//and normal code resumes...
$category->addData($data['general']);
//...
}
and then we can see that Magento is passing the structure
//...
[image] => Array
(
[value] => your-image.png
)
//...
to the magic setter addData() and so actually your question might be why is it ever type String, we expect it might always be type Array because the POST input structure is
Content-Disposition: form-data; name="general[image][value]"
your-image.png
BUT I don't have your code. And your code is setting the key [Thumbnail]. To answer your question you should read the code of the controller that is POSTed to with the form value [thumbnail] then find where the addData($data['general']) equivalent code is and as above use the built in error message system to print_r() the data so you can see it.
The magic setter will set any data key with square brackets as an array. Maybe when you read the code it will become obvious why sometimes it is string and sometimes array.
I wonder if it is an array for a category that has a subcategory and a string for a category without a subcategory

MVC URL issues (Codeigniter)

I'm building a website using Codeigniter and I really like how in the MVC pattern URLs are used to reference controller methods. It seems very logical and intuitive however, I seem to be running in an array of issues with this very pattern!
So I am building an events website and currently I'm passing everything through one main Site controller, passing a number of parameters:
public function index($page = NULL, $city = NULL, $type_venue = NULL, $slug = NULL)
{
// if the page argument is empty show the homepage
if( ! ($page))
{
$page = 'home';
}
// create an array for passing to the views
$data = array(
'title_city' => $city,
'title_type_venue' => str_replace('-', ' ', $type_venue),
'locations' => $this->locations_model->load(),
'events' => $this->events_model->load($city, $type_venue, $slug),
'venues' => $this->venues_model->load($city, $slug)
);
// construct the page layout with the following views
$this->load->view('partials/head', $data);
$this->load->view('partials/header', $data);
$this->load->view('content/'.$page, $data);
$this->load->view('partials/footer');
}
This works fine, in that it loads content for the following URLs:
site.com/events/bristol/open-mic/city-varieties/another-incredible-event
site.com/events/bristol/open-mic/city-varieties/
site.com/events/bristol/open-mic/
site.com/events/bristol/
However if I want to pass anything else through this controller that isn't an event, i.e. register/user, I have to write a specific route for this!
Worth noting my routing is:
$route['(:any)'] = 'site/index/$1';
I could write separate controllers for each entity, i.e. events, venues, cities but each one would look largely like the above (correct?) in that each would need the parameters to get the data.
My question is - what is the best practice approach for developing long query strings like this? Is a single controller correct? It doesn't feel like it, but then multiple controllers would violate DRY, just because they all need so much similar data. Any help appreciated!
Avoid putting everything into a single controller; even further, in each controller, avoid putting everything into a single index function.
There is no need to write specific controllers for each function in Codeigniter - suggest you read that part again in the manual. Most of your routing will be done automatically for you if you follow the normal guidelines.
The more you try to use a single controller or function, the more you will have to add untestable, unmanageable, unscalable conditional code later.

CodeIgniter - Dynamic URL segments

I was wondering if someone could help me out.
Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.
As per the CI userguide the uri is built as follows
www.application.com/CLASS/METHOD/ARGUMENTS
This is fine except i need to structure that part a bit different.
In my forum i have categories and posts, so to view a category the following url is used
www.application.com/forums
This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be
www.application.com/forums/mycategory/this-is-my-first-post
I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.
If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.
Cheers,
Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction
First Post
In the controller you can easily get this
$category = $this->uri->segment(3);
$post = $this->uri->segment(4);
And now you can proceed.
If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.
Go to system/core/uri.php and add this method
function assing_segment($n,$num)
{
$this->segments[$n] = $num;
return $this->segments[$n];
}
How to use
$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');
And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
You could make a route that forwards to a lookup function.
For example in your routes.php add a line something like;
$route['product/(:any)/(:any)'] = "forums/cat_lookup/$1/$2";
This function would then do a database lookup to find the category.
...
public function cat_lookup($cat, $post) {
$catid = $this->forum_model->get_by_name($cat);
if ($catid == FALSE) {
redirect('/home');
}
$post_id = $this->post_model->get_by_name($post);
/* whatever else you want */
// then call the function you want or load the view
$this->load->view('show_post');
}
...
This method will keep the url looking as you want and handle any problems if the category does not exist.Don't forget you can store the category/posts in your database using underscores and use the uri_title() function to make them pretty,
Set in within config/routes.php
$route['song-album/(:any)/:num'] = 'Home/song_album/$id';
fetch in function with help of uri segment.
$this->uri->segment(1);

CakePHP validation not working for contact form

I am trying to do some very simple validation in my CakePHP contact form, but validation does not work eventhough I think I did everything necessary. Here's what I did:
I made a model like this:
class Office extends AppModel
{
var $name = 'Office';
var $useTable = false;
public $validate = array('onderwerp' => 'notEmpty');
}
(I also tried many other values for $validate from the CakePHP online manual)
In Config/bootstrap.php I made this rule for not letting CakePHP expect plural "Offices":
Inflector::rules('plural', array('rules' => array(),
'irregular' => array(),
'uninflected' => array('office')));
In OfficeController, I do this in my method contact():
$this->Office->set($this->request->data);
if($this->Office->validates()){
echo "code validates";
} else {
print_r($this->Office->validationErrors);
}
And in my Office/contact.ctp view, I have (amongst other code like starting and ending the form) this code:
$this->Form->input('onderwerp', array('label'=>false, 'size' => 60));
Now, even when I fill in the form, leaving empty the field 'onderwerp', it executes the code that should be executed when the code is executed.
When I print_r($this->request->data) or print_r($this->Office) I see that my onderwerp field is there and that it is empty (or filled when I do fill in something).
Now, when I add a public function validates() in my model and echo something there, it IS being displayed. So I'd say CakePHP knows where to find my model, and does execute my controller code. I also tried adding return parent::validates(); in my validates() function, but this also yielded no validation error, or any other error for that matter. My debug level is set to 2.
I guess I'm missing a needle in this haystack. Thanks for helping me finding it!
so drop all the inflector stuff.
and use the right model in your Form->create()
either
$this->Form->create(null)
or
$this->Form->create('Office');
and if you follow my advice to use a table less model with schema you will also have more power over input creation and validation.

Magento - how to add new product status

I'm trying to make new product statuses but i can't figure out how to do it and all the stuff set on the web is not consistent or simply talks about order status which i don't want to change.
What is your motivation to have new product statuses? I think it's little bit risky to change this part of app. I suggest you to add new attribute and use this one instead system product's attribute 'status', this attribute tells to system if product is enabled or disabled. I guess there is nothing between :)
Override class Mage_Catalog_Model_Product_Status to the local folder. Then open the file
\app\code\local\Mage\Catalog\Model\Product\Status.php
At the top of the file you can see the constants
const STATUS_ENABLED = 1;
const STATUS_DISABLED = 2;
Add your custom status below them, for example
const STATUS_SUSPENDED = 3;
Then edit the function getOptionArray
static public function getOptionArray()
{
return array(
self::STATUS_ENABLED => Mage::helper('catalog')->__('Enabled'),
self::STATUS_DISABLED => Mage::helper('catalog')->__('Disabled'),
self::STATUS_SUSPENDED => Mage::helper('catalog')->__('Suspended')
);
}
That's it. Don't forget to clear the cache.

Resources