I am using the Simple Configurable Products extension by OrganicInternet.
I have a number of Tabs in every product with related information.
I am editing OrganicInternet/SimpleConfigurableProducts/Catalog/Block/Product/View/Type/Configurable.php
The logic is as follows -- (this builds the JSON with the simple products data):
class OrganicInternet_SimpleConfigurableProducts_Catalog_Block_Product_View_Type_Configurable
extends Mage_Catalog_Block_Product_View_Type_Configurable
{
public function getJsonConfig()
{
$config = Zend_Json::decode(parent::getJsonConfig());
$childProducts = array();
....
foreach ($this->getAllowProducts() as $product) {
.....
if (Mage::getStoreConfig('SCP_options/product_page/change_attributes')) {
$childBlock = $this->getLayout()->createBlock('catalog/product_view_colors');
$childProducts[$productId]["colors"] = $childBlock->setTemplate('catalog/product/view/colors.phtml')->setProduct($product)->toHtml();
}
......
}
The block is rendering properly, but for some reason it is not using the correct Product. I suspect the setProduct($product) method is not working. (It is using the "super" product), but when I do a print_r(get_class_methods($product)), it shows the correct simple product.
Any ideas?
Related
I have setup a route like this:
Route::get('/prod/{id}/{title?}', 'Web\GetItemController#getItem')->where('id', '[0-9]+')->name('prod.item');
This is to retrieve a item based on the id the title is only a optional parameter in order to make the url look somewhat nicer.
Then in the controller I fetch the item like this:
class GetItemController extends Controller
{
public function getItem($id, $title = null)
{
$item = new product();
$data = $item->getProdInfo($id);
// Show view with data...
}
}
So I should be able to fetch the item with or without the title parameter.
So a call like this would trigger the route "https://www.x.com/prod/3/sonic-free-games"
But is there any difference in performance using slug like I do above?
/prod/{id}/{title?} vs /prod/{id}
I have areas. Each area have N curses
So, each curse belongsTo only one area.
My class
class Area extends Model
{
public function curses()
{
return $this->hasMany('App\Curse');
}
}
My Controller
public function getCursesByAreaId()
{
$areaId = Input::get('areaId'); //ID of selected Area
//area_id is a field of table CURSE. A FK to Area.
$areas = Area::with('curses')->where('area_id', $areaId)->get();
foreach($areas as $area)
{
var_dump($areas);
}
}
Curse Model
class Curseextends Model
{
protected $fillable =
[
'description',
'area_id'
];
}
Using the laravel's debugbar, I see that the query being executed is this:
select * from "areas" where "area_id" = '2'
Isn't it supose to run the relational query ? (with the joins)
Already checked if I'm receiving the right id and it's ok.
the problem is that it's bringing no data at all.
You need something like the following:
$areas = Area::whereHas('curses', function($query) use ($areaId) {
$query->where('area_id', $areaId);
})
->with('curses')
->get();
The whereHas is required only if you want to get the areas that has matching curses.
But, I think you can do it like this way if you only need the related curses because one curse only belong to a single area so, if have the area then you'll get all the curses attached to it:
$area = Area::with('curses')->find($areaId); // Single area
// Access the curses
$curses = $area->curses; // collection of curses
I think you are using relational area_id which is present in curse table, try retrieving the ID of area by id not by area_id, something like this:
$areas = Area::where('id', $areaId)->with('curses')->get();
Hope you get the solution.
Try this :
$areas = Area::whereHas('curses', function($area_query) use ($area_id) {
$area_query->where('area_id', $area_id)
})
->with('curses')
->get();
Can I have a specific category layout for selected categories? The detail page will be the same, I need to get a custom tpl file.
Thanks for suggestions
Pretty easy to do: override the category controller to set the template.
class CategoryController extends CategoryControllerCore {
// array with the selected categories
private $customCategories = array(1, 3);
public function init() {
parent::init();
if (in_array($this->category->id, $this->customCategories)) {
$this->setTemplate(_PS_THEME_DIR_.'category-custom.tpl');
}
}
}
Here you wouldn't be able to change the selected categories from the back office, but it would be easy to do with a module.
I'm having trouble with eager loading in laravel
I have these models:
class Page extends Eloquent {
public function translations() {
return $this->has_many('Pagestl', 'page_id');
}
}
and
class Pagestl extends Eloquent {
public static $table = 'pages_tl';
public function page() {
return $this->belongs_to('Page', 'id');
}
}
I want a specific page with its translation data of a specific language.
I retrieve the data like this:
$page_data = Page::with(array('translations' => function($query) {
$query->where('lang_id', '=', 'nl')->first();
}))->find($id);
De result is ok-ish. I get all the page data and the translation of 1 language, dutch (nl). But in order to get a field from the language data I have to this:
$page_data->translations[0]->attributes['content_or_whatever'];
..which i find ugly. I feel i should only have to do something like:
$page_data->translations->content;
..but that gives me an error (Trying to get property of non-object).
Am I missing something or is this just the way it is?
I don't find it necessary to do eager-loading here.
Both ways of doing it will result in 2 queries: select the Page with the given id and then find its first translation.
So, I would do:
$page = Page::find($id);
and then
$page_data = Pagestl::where_page_id_and_lang_id($page->id, 'nl')->first();
Then, assuming the content field exists in pages_tl table, we'll get it by doing:
echo $page_data->content;
Finally, your code looks alright; you don't have to have the second parameter when defining the relationships unless they are different from what Laravel expects.
I'll let you find where the problem is with your code (I couldn't figure out from the information given - could be something to do with strings.php)
I need to check if an order has already some shipment set. The only data I can use is the increment id of the order. I'm getting an instance of a model order, but I don't see a way I can get a shipment instance.
I'm using this code:
$order = Mage::getModel('sales/order')
->loadByIncrementId($order_increment_id);
But how can I get a shipment instance? I know that I can call Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment_increment_id) but how do I get the shipment increment id?
Assume that the person who wrote this might have also needed to do what you need to do. Generally, when Magento objects have a one to many relationship you can find a method to load the many on the one.
You've got a class alias sales/order.
This corresponds to Mage_Sales_Model_Order (in a stock installation).
You can find this class at app/code/core/Mage/Sales/Model/Order.php.
If you examine this class, there are 7 methods with the word "ship" in them
function canShip
function setShippingAddress
function getShippingAddress
function getShip
function getShipmentsCollection
function hasShip
function prepareShip
Of those 7, only the semantics of getShipmentsCollection indicate a method for grabbing an order's shipments. So try
foreach($order->getShipmentsCollection() as $shipment)
{
var_dump(get_class($shipment));
//var_dump($shipment->getData());
}
Or take a look at the source for getShipmentsCollection
public function getShipmentsCollection()
{
if (empty($this->_shipments)) {
if ($this->getId()) {
$this->_shipments = Mage::getResourceModel('sales/order_shipment_collection')
->setOrderFilter($this)
->load();
} else {
return false;
}
}
return $this->_shipments;
}
Just to make it complete Mage_Sales_Model_Order has public method:hasShipments()which returns number of shipments and internally uses mentioned getShipmentsCollection().