Price based on product custom options (width and height) - magento

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);

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;
}
}

Magento 2 calculate product price against user input

I am currently trying to implement some extra product price attribute which will take user input and calculate the price according to that. Tried with price attribute but not working. Any idea how to do it, as new in magento unable to fix that. Please note height and width are input type text, a user can add any value
To achieve this, i have created a set of attributes in admin section,
From the front end trying to calculate the price,
require([
'jquery','Magento_Catalog/js/price-utils','jquery/ui','Magento_Catalog/js/price-box'
], function($,priceUtils,priceBox){
"use strict";
var priceBoxes = $('[data-role=priceBox]');
$('.product-custom-optionval').change(function () {
var value = $(this).val();
if (value === '') {
$(this).val(0);
updatePriceOnChange(0, $(this).attr('data-attr-id'));
return;
}
disableAddToCartBtn('#product_addtocart_form');
calculatePrice($(this));
enableAddToCartBtn('#product_addtocart_form');
});
****** But after calculating the price what will my approach to update product price, please help *******
HI for this you can do something like
Create custom module
Define custom controller
whenever user puts the input in the height and width
Send those parameters to the controller along with the product id and qty
Do your calculations there on basis of your logic and return response
upadte the price in the div on basis of the response
than when user hits add to cart , listen before add to cart event and upadte the cart item price with the new price of your calculations
rest magento will do itself
hope that will help you somehwere

How to get the attributes selected 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

where is the path of opConfig.reloadPrice(); in magento

i need to customize function opConfig.reloadPrice(); in magento.
can anyone tell me where is that function locate?
This function is executed on custom attribute drop down on product details page.
As i need to change the Special price calculation , i guess need to customize this function .
This method can be found in following files:
grep 'reloadPrice:' . -rsn
./js/varien/configurable.js:271: reloadPrice: function()
./js/varien/product.js:463: reloadPrice: function()
./skin/frontend/base/default/js/bundle.js:83: reloadPrice: function()
For change custom option value, you should change in to js/varient/product.js
and in that page you found, reload:function()
that function call at the time of on change event.
Good Luck
I also had the issue of having to change the default behaviour of Magento on this one. I included jQuery in my project and I finaly figured it out by first removing the default behavior of Magento and instead on calculatePrice call my calculatePrices()-function:
$j('#select_20, #select_21').removeAttr('onchange').change(function(){
calculatePrices();
});
Now, in this calculatePrices()-function, alongside with some other logic, I included this to change the prices:
function calculatePrices()
{
var price = 0;
// some logic with custom options, not interesting for this question...
// Change the price according to the options:
$j('#select_20, #select_21').each(function(){
var selectId = this.id.replace('select_', '');
var options = opConfig.config[selectId][this.value];
if(options.type == 'fixed') {
price += options.priceValue;
} else {
// percentual change:
price += price * (options.priceValue/100);
}
});
// Use Magento Objects to set these prices:
optionsPrice.changePrice('bundle', price);
optionsPrice.reload();
}

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