How to get the attributes selected Magento - magento

what I need is relatively simple ...
I need to get the attributes that have been selected in the product page.
Explaining better:
I have a configurable product and have two attribute, size and color. When I go to buy the product, I have the options to select the attributes, I have a button that will do some actions and need to get the attributes that are selected.

On change of the dropdown do a ajax call which would return from the selected value
This would get you all attribute option and value. Change it according to your requirement
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
Still not sure what you want to achieve

Related

Magento 2 How to pass js variable to controller and then call template with custom code?

I need dynamically change custom attribute on the configurable page when I click to the swatch color attribute. On this purpose I programmatically created custom attributes with options on the admin panel, where I can save for every product his own custom attributes.
I figure out how to call my custom attributes on simple products but I can't to do this with configurable product.
I see when I change color attribute - price is changed for every simple product([it's default thing), how to do same but with my custom attributes?
I have one idea but I don't know how this one implement. So I know how to get current product id when you clicked on the color attribute(through js code) then I need pass this id data to the controller via ajax. I need to know how exactly to do this, how to pass js data through ajax to controller and then call needed block on the category page every time when I click on the color attribute? ? When I will be know how to do that I get with this id data - custom attribute values and call this template in the needed place.
Code below is that what call custom attributes on the configurable product, but it's call all attributes for all simple products included in the configurable product. I need call these attributes for current product every time when I cliked on the color attribute. For example: if I click on the black color (which related to the product with id 123 ) I get custom block with custom attributes without reloading page(like price).color attributes
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$product_id = $product->getId();
$configProduct = $objectManager-
>create('Magento\Catalog\Model\Product')->load($product_id);
$configurable = $configProduct->getTypeId() ==
\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE;
$_children = $configProduct->getTypeInstance()-
>getUsedProducts($configProduct);
if ($configurable){
foreach ($_children as $child){
$child_badge = $child->getResource()->getAttribute('mycustom_attribute_code')->getFrontend()->getValue($child);
echo $child_badge;
}
}

How to update product attributes?

I have thousands of products which have some common default values:
page layout : No layout updates; Visibility:Not Visible Individually;
categoies: empty,
Now I want to change these three attributes once like below:
page layout : 1 column; Visibility:Catalog, Search (4); categoies: 1,
I tried to finish it with below code, but it is not working.
$collection = Mage::getResourceModel('catalog/product_collection')->load();
foreach($collection as $items)
{
$product = Mage::getModel('catalog/product')->load($items->getId());
$product->setVisibility(4);
$product->save();
echo $product->getId().'<br>';
}
Who can tell me what the problem is with my code?
I also tried to finish in database. But I do not know which table can control visibilibty, which table can control layout or category. (Using Magento 1.7)
You can do this directly from the admin area:
Catalog -> Manage Product
Check the article you want to edit and then select "Change attributes" from "Actions" drop down menu.
You can now massively change attribute to your product
Cheers

MVC3 - display price based on product selection in form

I am sure there will be articles around this but I really don't know the correct name for it and having little success finding examples like mine.
I have a quote form where you have the ability to select a product and atributes of it. There are three dropdowns and based on the combination of the three, I have a table (with associated model/controller) that has a price. I want to display the price on the page but i must update as the selections are updated.
Setting the price from the get go seemed easy but then updating it based on dropdowns selected had me go in a tail spin of '"onSelectedIndexChanged()', javascript, AJAX, partial views and I simply confused myself.
I need a simple way to display price information on a quote form as they fill out the details (three fields control price). I did look at the music store demo but its slightly different and the shopping cart element which looked handy was grabbing data in a table so again got stuck.
Help always appreciated as ever.
Thanks,
Steve.
When one of the three dropdowns changes, I'd make an AJAX call to the server to get the price as a JSON object and use it to update the price input.
Example using jQuery to add the handlers and perform the AJAX operation.
var $priceControls = $('#control1,#control2,#control2');
$priceControls.change( function() {
$.getJSON( '#Url.Action("price","product")',
$priceControls.serialize(),
function(price) {
$('#price').val(price);
});
});
public class ProductController : Controller
{
public ActionResult Price( string control1, string control2, string control3 )
{
decimal price = ...lookup price based on control values...
return Json( price, JsonRequestBehavior.AllowGet );
}
}

Price based on product custom options (width and height)

I have some products that can be custumized by the costumer. Naturally, the price must based on the costumization.
For instance, I sell rugs. The dimensions of the rugs can be, sometimes, chosen by the costumer. In order to accomplish that, I created 3 custom options in all the customized products: square_meter_price, width and height.
After clicking on 'add to cart', the customer can choose width and height. After that, on the checkout/cart url, the costumer reads on the 'Product Name' column the correct product name, width and height. The only think that is needed here is to get the correct price out of it.
I am trying to do that by overriding Mage_Catalog_Model_Product_Type_Price::getPrice() method. The problem is I can't get the width/height values input by the costumer so I can do the math.
Note:
I already tried iterating through $product->getOptions(). I was able to get the custom options width/height but I wasn't able to get the values input by the customer.
I also tried $product->getCollection()->getAttribute('width') and $product->getAttribute('width'), but I got null.
Thank you in advance.
class My_Catalog_Model_Product_Type_Price extends Mage_Catalog_Model_Product_Type_Price
{
public function getPrice(Mage_Catalog_Model_Product $product)
{
// get the width provided by the user
$width = $this->howToGet('width');
// get the height provided by the user
$height = $this->howToGet('height');
if ($width && $height) {
return $width * $height * $product->getAttribute('square_meters_price');
}
return parent::getPrice($product);
}
}
You should be able to do this by modifying the function _applyOptionsPrice in Mage_Catalog_Model_Product_Type_Price. This function loops through the options, adding the option prices one by one. Instead of adding, you just need to multiply. Try something like this:
//$finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $basePrice);
$finalPrice *= $group->getOptionPrice($quoteItemOption->getValue(), $finalPrice);

Hide a group in catalog product?

Please help me if anybody know how this work can be done.
I want to hide the website tab in catalog Product, but its functionality should exist. That is, I have made all the check boxes automatically checked,so i dont want to show this tab anybody...but at the time of adding product..check boxes values would be saved.
Not exactly sure how you would do this, but basically you need to bind an Observer in the adminhtml render sequence that calls Mage_Adminhtml_Block_Widget_Tabs::removeTab($tabId) where $tabId is the Id of the websites tab (I think it's just "websites"). The trick is to find the right event to bind your Observer to, #Joseph's list of events should get you started. I would try something like adminhtml_block_html_before.
Your observer would also set the values on the product at the same time.
Good luck,
JD
In ProductController.php
Websites
*/
if (!isset($productData['website_ids'])) {
$productData['website_ids'] = array();
}
$productData['website_ids']=$this->getStoreWebsiteId(); //newly added
//newly added
public function getStoreWebsiteId(){
$selectWebsite="SELECT * from core_website WHERE website_id!=0";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$value=$connection->fetchAll($selectWebsite);
foreach($value as $websiteDetails){
$websiteId[]=$websiteDetails['website_id'];
}
return $websiteId;
}

Resources