Virtuemart - Need my AddtoCart Form to show Children of Extra ID Products ONLY - joomla

I am attempting to make an online store that sells DVDs, VHSs, CDs and Digital Products. The Digital Products will be broken down into singles.
I have created two (2) Product Parents: "Product" and "Product Singles"
"Product" has three (3) Children: "DVD", "CD" and "Download"
"Product Singles" has three (4) Children: "Track1", "Track2", "Track3" and "Track4"
In "Product"s Extra IDs field I input the Product ID of "Product Singles".
Now, my AddtoCart Form is listing all seven (7) Children.
I created a 2nd AdtoCart Form titled addtocart_form2.tpl.php which is an exact copy of addtocart_form.tpl.php
In the ps_product_attribute.php file, I created a duplicate function called list_attribute_list2 which controls the list that appears in the AddtoCart Form.
I had my Flypage show addtocart_form.tpl.php in the top right and blocked out the code that would add the Extra IDs. It worked.
I had my Flypage show addtocart_form.tpl.php at the bottom. It worked.
Now, I'd like to change the code on addtocart_form2.tpl.php to allow for ONLY the Extra ID Children to appear.
But this is where I'm stuck, I don't know how to change the code to ONLY show the Extra IDs Children and NOT the Parent "Product"s Children.
Any help would be appreciated.

In the shop.product_details.php file:
I created a second instance of the variable $q and caled it $q2
after $db_product->query( $q ); ran the query on the Database, $db_product was now set as the entire row where 'product_id' = $product_id
Then I inserted the following code:
$product_id2_str = $db_product->f("child_option_ids");
$product_id2_arr = explode(",",$product_id2_str);
$product_id2 = intval($product_id2_arr[0]);
$db_product2 = new ps_DB;
In other words, I fetched 'child_option_ids' as a string, then created an array. Then, I transferred the first number in the array to $product_id2.
After that, I pretty much just made copies of anything where $product_id was important.
If you need more help, I guess just comment and I'll write the rest, but this was the main problem I was having.

Related

Codeigniter route products, categories and sub-categories

I am using Codeigniter for my project.
I need to get URL structure like this:
Main product page
example.com/language-prefix/products/
Select products by category
example.com/language-prefix/products/category
Select products by category AND sub-category:
example.com/language-prefix/products/category/sub-category
Select specifict product under category AND sub-category:
example.com/language-prefix/products/category/sub-category/product-name
OR only under category
example.com/language-prefix/products/category/product-name
Question is - what would be a good solution for this? Because problem starts here:
example.com/language-prefix/products/category/what-ever
what-ever can be a product or a sub-category and how to decide - what data and view should be returned?
In my DB table structure I have many-to-many relationships between products and categories. It means, than one product can be assigned to many categories. Every category has a self refferecing foreign key (parent).
Maybe I need to get some restrictions for category adding for products? Or specifing a main category or what?
I have couple of ideas:
Keep every route for category/sub-category and product in DB for example:
/products/watches/for-men/
/products/watches/for-men/rolex-abc-whatever-product
And so on, and so on. But this, I have feeling could be very slow.
Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to
/products/category/what-ever
then first check if there exists such product, if true, then return product_view and data, if not, check if there is such sub-category and if there is, then return the product grid or return 404 if there is not.
I assume a simple solution could be just keep all categories and sub-categories after /products/ and add category-id like
example.com/language-prefix/products/1-watches
example.com/language-prefix/products/2-for-men
But I hope there is better solution for this.
Also I cant figure out this:
example.com/language-prefix/products/category/sub-category/product-name
I need to return product with name product-name AND check if it is under those two categories so URL for example:
example.com/language-prefix/products/hello-kity/bla-bla/product-name
would NOT return that product.
Any better/other solutions?
"Keep every route for category/sub-category and product in DB"
the idea above won't work well in CI and you'll probably break the way CI routing works. remember that the route points you to a controller/function. only when you get there can you start calling the db... so this idea will be weird since you gotta' call the db first to match the routes.
"Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to /products/category/what-ever"
the idea above is better, you could have a route like this $route['[a-z]{2}/products/(:any)'] = "product/index/$1"; so http://www.example.com/sg/products/gems/ruby and http://www.example.com/sg/products/gems/red/ruby will both go to the product controller's index function
segment(4) can be either "red" which is a sub-category or "ruby" which is a product. segment(3) is always going to be a category. segment(5) is either empty or a product. you must now simply decide if products or sub-categories that precedence, better still handle within your code logic that they never will collide. i.e. product and category/sub-category names cannot be duplicated.
public function test($category = NULL, $sub_category_or_product = NULL, $product = NULL)
{
echo $category.'<br />'; // segment(3) is always your category
if ($product)
{
echo $sub_category_or_product.'<br />'; // segment(4) is a sub-category
echo $product.'<br />'; // segment(5) is a product
}
else
{
echo $sub_category_or_product.'<br />'; // it's a sub-category or product
// call the db and test whether its a sub-category
// then test if its a product... or the other way around
}
}
of cuz this is just one of the many possible ways to solve it...
however if you don't have to stick with that URL structure...
i would suggest you put products/categories/sub-categories under the "/products/" namespace in the URL for "listing view". and put products under the "/p/something" namespace for the details view
www.example.com/p/ruby // product details
www.example.com/products // product listing
www.example.com/products/gems // product listing
www.example.com/products/gems/red // product listing
for example see:
http://www.beautylish.com/products
http://www.beautylish.com/p/mac-mineralize-skinfinish-natural
this way you won't have to worry about categories/sub-categories names clashing with product names
people probably want to put the cats and sub cats in the product url for seo... but if you check beautylish they got very damn good seo without choking up the url and simply putting only the product name, it's also shorter and more readable.

How do I retrieve the ID of a Grouped Product from which a Simple Product was ordered in Magento?

Let me start with some context. I'm operating in Magento Enterprise Edition 1.8.0.0 and I am relatively new to Magento Development.
I have seen questions similar to this on Stack Overflow, forums, and blogs, but the answer tends to be something like the one I came up with myself:
$parentIds = (Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($_item->getProductId());
$parentId = $parentIds[0];
It's true that this will retrieve a Parent ID, but it will not always retrieve the right one. The "right" Parent ID, in my case, is the one belonging to the Grouped Product from which a Simple Product was placed in the cart and eventually ordered.
The file I'm trying to get this logic into is:
my_template_path/email/order/items/order/default.phtml
The end goal is to retrieve the thumbnail image of the right Grouped Product in an Order Confirmation Transactional Email. Any ideas?
The information about the parent product is stored in the property 'product_options' of the order item object. You can retrieve the right parent id for every order element by executing this code:
foreach ($order->getAllItems() as $item) {
$options = $item->getProductOptions();
$parentId = $options['super_product_config']['product_id'];
}

Filter a Magento Category Collection by a Product Attribute?

I have a Magento site with an extensive catalog (10,000 products) and a fairly deep category tree.
All of the product's categories stem from a top-level "All Products" category.
All products have a drop-down attribute of "Brand."
What I am trying to do is allow a visitor to start at the top of the category tree and navigate down the "All Products" tree either with or without the brand selected. The hitch is that we need to show the next level of subcategories at each level and I don't want to show empty categories if a brand doesn't apply.
For example, if the tree looks like this and one is on the "Screwdrivers Page," we'd like both the type and length categories to be visible.
All Products
Screw Drivers
phillips
2"
3"
5"
flathead
1"
3"
5"
I was able do this without worrying about the brand filter by creating a category collection:
$_category = $this->getCurrentCategory();
$current_level = $_category->getLevel();
$collection = $_category->getCollection();
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('parent_id')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('level',array('in'=>array($current_level+1,$current_level+2)))
->setOrder('position','ASC')
->load();
Based on my research it appears that it is not possible to directly filter a category collection by a product attribute.
Assuming that this is true:
What is the best way to accomplish this? I've thought of and preliminarily tried few ideas:
Create a product collection filtered by brand and loop through it to get all of the relevant categories and build the tree (seems like this would be horribly inefficient)
Try to utilize layered navigation to output the categories (this would work if we were only viewing one level -- I'm having trouble getting a filtered "second" level.)
Abandon the filtering idea and duplicate the category Tree under a new "Browse By Brand" Category (I'd like to stay away from this so that updating products doesn't require updating two duplicate trees.)
Thanks in advance for any help. Please let me know if you need any clarification of the problem.
In case anyone has a similar issue down the road I accomplished this by using the second bullet point above.
/* Get Current Filters by loading catalog/layer_view Block */
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('catalog/layer_view');
/* Get Current Category */
$_category = $this->getCurrentCategory();
$current_level = $_category->getLevel();
/* Get Layer Filter Category Model */
$category_filter = Mage::getModel('catalog/layer_filter_category');
/* Generate the collection based on the current category */
$categories = $_category->getCollection();
$categories->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('parent_id')
->addAttributeToSelect('thumbnail')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('path', array('like'=>'%/'.$_category->getId().'/%'))
->addAttributeToFilter('level',array('in'=>array($current_level+1,$current_level+2)))
->setOrder('position','ASC')
->load();
/* Using current filters, add product counts to collection so we can hide categories with 0 products */
$category_filter->getLayer()->getProductCollection()
->addCountToCategories($categories);
After this it is a matter of looping through the collection and displaying the name, URL of the category, etc..
This may not be the absolute best way -- I certainly welcome other solutions.

Magento - get results view HTML for a collection of products

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:
Array
(
[0] => 1965
[1] => 3371
[2] => 1052
)
I can then make this into a collection:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($product_ids);
Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:
$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);
And then I've tried to load the HTML of the results as follows:
$CollectionHTML = $ProductList->_toHtml();
But $CollectionHTML is empty.
How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?
Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.
Anyway, back to your issue.
In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.
But if you want to create a block a way to do it is:
$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.
Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:
//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object
echo $block->renderView();
Displaying specific ids:
you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)
But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)

how to get the size of product to the cart page

I have size of product (XS, XL, ...).
I want add column in cart table Size, where show sizes of products.
I added SKU in this table $_item->getSku(); This works.
But $_item->getSize(); not works. Please, help me.
try adding ->addFieldToSelect('*'), it'll add every attribute associated with your products in the returning data, you can replace the * with the attribute code of the attribute your trying to display
problem is, it'll return integers that are used in either eav_attribute_option or eav_attribute_option_value (not sure why there's 2 different tables, one has values and one has sort order, it's not like Magento even links a value to 2 options), however if you use the following code
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('ATTRIBUTE_CODE')->getFirstItem();
$sizeValues = array();
// populates sizevalue array with data
foreach ($attributeOptions as $key => $value)
{
$sizeValues[$value['value']] = $value['label'];
}
you get an array of values with their index's being that of that it returned in your collection

Resources