I need to change the value of all the products in the following fashion store.
Current value = 1,000
Coefficient = 0.85
Current value / coefficient = new value
in the example would be
1000 / 0.85 = 1176.47
How to change that in all areas of Magento (cart, checkout, admin, etc.) to the new value?
There are three options, Either you can do this with magento product promotions form back-end by providing promotions or by using magento core modules.
If you have lets say 10K of products you can do this by just import/export options in magento admin dashboard. so after exporting the products csv you can add or make calculation in csv.
Or
just using simple sql queries on magento db you can get your result.
magneto db table which store product price-
catalog_product_entity_decimal
catalog_product_entity_group_price
catalog_product_entity_tier_price
Or
Now finally with the magento core modules. you can use this code ..
$product = Mage::getModel('catalog/product');
$product->load($productId);
$product->setSpecialPrice($price);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->save();
Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, array('qty' => $quantity));
$cart->save();
Assuming this is just a one-off task, then I'd suggest something like the following:
$products = Mage::getModel('catalog/product')->getCollection();
$products->setPageSize(100);
$pages = $products->getLastPageNumber();
$currentPage = 1;
$batchNumber = 0;
do {
$products->setCurPage($currentPage);
$products->load();
foreach($products as $product) {
$origPrice = $product->getPrice();
$newPrice = $origPrice / 0.875;
$product->setPrice($newPrice)->save();
}
$products->clear();
$currentPage++;
} while ($currentPage <= $pages);
Related
Hi for a Magento website i need to load a custom attribute in the shopping cart.
i use getmodel function to load the items. but i have no clue how i load the attribute. maybe i haven't configure the attribute right. i have enabled yes for enable on product listing. de attribute code is 'staffel_percentage'. it is just a regular string
also when i change the price per product it doesn't change the subtotal maybe this is because we already change the price for the product on the rest of the website?
maybe it is in the event? i use this event: controller_action_layout_render_before.
this is the code in observer i use for it
$cart = Mage::getModel('checkout/cart')->getQuote(); // Gets the collection of checkout
foreach ($cart->getAllItems() as $item) { // adds all items to $item and does the following code for each item
if ($item->getParentItem()) { // If items is part of a parent
$item = $item->getParentItem(); // Get parentItem;
}
//echo 'ID: '.$item->getProductId().'<br />';
$percentage = 80;// init first percentage
$quantity = $item->getQty(); //quantity
//$staffelstring = loadattribute//loads attribute
//gives the right percentage per quantity
//$staffelarray = explode(';', ^);
//foreach($staffelarray as $staffels){
//$stafel = explode(':', $staffels);
//if($quantity >= $stafel[0]){
//$percentage = $Stafel[1];
//}
//}
$currency = Mage::app()->getStore()->getCurrentCurrencyRate(); // Currencyrate that is used currently
$specialPrice = (($this->CalculatePrice($item) * $currency)* $percentage) / 100; //New prices we want to give the products
if ($specialPrice > 0) { // Check if price isnt lower then 0
$item->setCustomPrice($specialPrice); //Custom Price will have our new price
$item->setOriginalCustomPrice($specialPrice); //Original Custom price will have our new price, if there was a custom price before
$item->getProduct()->setIsSuperMode(true); //set custom prices against the quote item
}
}
You need to load the product right after your first if statement
$product = Mage::getModel('catalog/product')->load($item->getId()); // may be $item->getProductId() instead here
Then on the next line after that you can add some logging statements that will appear in var/log/system.log
Mage::log($product->getData()); // this will output all of the product data and attributes. You will see your custom attribute value here if it is set on this product.
If there is a value set for your product for your custom attribute then you can get it like this
$value = $product->getData('staffel_percentage');
As for the prices changing, not sure how you have the prices set up. You need to set a positive or negative number to add or subtract from the price in the fields found in the parent product configuration page in Catalog > Products > Your product > Associated Products.
See this image for what the section looks like.
I am currently working on the development of a management module Cart on Magento 1.9
I'm stuck on adding a product to my cart (I tried many different things) and that's why I solicits your help.
My module extends the rest API of magento and I have already managed update to my cart (quantity of products) but now I'll wish to add a new product via the POST method.
Of course I'm logged as customer. I defined the creation privileges for this role. (I can do the update without problems)
Here is my code :
protected function _create(array $data){
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
$cart = Mage::getModel('checkout/cart');
$cart->init();
$productCollection = Mage::getModel('catalog/product')->load(4);
// Add product to cart
$cart->addProduct($productCollection,
array(
'product_id' => $productCollection->getId(),
'qty' => '1'
)
);
// Save cart
$cart->save();
}
In this simple example, I try to add the product id 4 in quantity 1.
My problem is that I have no error in the log, and everything seems to be past. But when I get my cart, there is no product to add...
in return I have a code 200 OK
Do you have any suggestions to help me?
Thanks a lot for your help
regards
I finally found the solution after traveling all internet;)
In fact when you want to reach the cart before checkout, magento uses the definition "Quote" ... Not easy to understand for a beginner on Magento....
So in order to facilitate research of those who are like me had troubles, here is my code to add a new product in the cart (before checkout) :
//$data['entity_id'] = The id of the product you want to add to the cart
//$data['qty'] = The quantity you want to specify
protected function _create(array $data)
{
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
// Get Customer's ID
$customerID = $this->getApiUser()->getUserId();
// Load quote by Customer
$quote = Mage::getModel('sales/quote')
->loadByCustomer($customerID);
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($data['entity_id']);
// Add Product to Quote
$quote->addProduct($product,$data['qty']);
try {
// Calculate the new Cart total and Save Quote
$quote->collectTotals()->save();
} catch (Mage_Core_Exception $e) {
$this->_error($e->getMessage(),Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
}
}
I hope this can help someone
Regards
Carniflex
I am currently building a module for an ecommerce website (Lemonstand). My shop sells both normal books and ebooks, and charge different rates of for shipping based on the subtotal of all items in the cart ($7 shipping cost for subtotal of less than $20, $14 shipping cost for between $20 and $50..etc). Currently the website just uses the subtotal of all items in the cart to determine which rate should be applied. The problem is, I want only the subtotal of normal books to be used for calculating shipping, because obviously ebooks don't need shipping.
Lemonstand platform has some built_in functions that I'm using to help with this. update_shipping_quote function is called just before shipping cost is calculated. It will be used to change the subtotal of cart items so that shipping cost can be calculated using the subtotal of non-ebooks instead.
Here is the API documentation for the function: https://v1.lemonstand.com/api/event/shop:onupdateshippingquote/
Here is the bit of code that's giving me trouble. I want to know if the value I get at the end ($non_ebook_subtotal) actually contains the value it's supposed to.
If anyone can come up with a better method for doing what I'm trying to do, please share.
//$params is an array containing things like individual item price
//Here I get the cart items and put them into var
public function
update_shipping_quote($shipping_option, $params) {
$cart_items = $params['cart_items'];
//find all products that are ebooks
foreach ($cart_items-> items as $item)
{
$product = $item->product;
if($product->product_type->code == 'ebook') {
$isEbook = true;
}
}
//add price of all ebooks into $ebook_subtotal
foreach ($cart_items as $item) {
$product = $item -> product;
if ($isEbook == true) {
$ebook_subtotal = $ebook_subtotal + total_price($product);
}
}
//Calculating the subtotal of only the non-ebook products
$non_ebook_subtotal = $params['total_price'] - $ebook_subtotal;
//This returns the non_ebook_subtotal to be used for calculating shipping cost
return array('total_price' => $non_ebook_subtotal);
}
Thanks
// get all variables needed
$totalprice = $params['total_price'];
$items = $params['cart_items'];
foreach ($items AS $item) {
// if is ebook
if ($item->product->product_type->code == 'ebook') {
// minus price of the item from total
$totalprice -= total_price($item->product);
}
}
return $totalprice;
I want to get the remain quantity of a given product. I'm going to create a report in admin panel which highlights the remaining quantity and re-order level quantity against each product. I was able to get the re-order level quantity by inventory details. And I want to get remain quantity for a given product. Please any suggestions?
Try
<?php
$num= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo "Remaining products: ".$num;
?>
If you are looking for the remaining quantity for products in the cart, you need to take into account configurable vs simple products:
<?php
$quote = Mage::getModel('checkout/cart')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
$cart_product = $item->getProduct();
if ($option = $item->getOptionByCode('simple_product')) {
$cart_simple_product = $option->getProduct();
} else {
$cart_simple_product = $this->getProduct();
}
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($cart_simple_product);
$max_available_quantity = (int)$stock->getQty();
}
?>
I'm setting up a tracking code for an affiliate programs. Now we give different commissions. One for Food related products and one for Non-Food related products. These are also the tax classes we have (Food, Non Food).
I need to display the sub total for food products and a subtotal for non food products.
I use the following code but that doesn't work:
<?php
//Get Order Number & Order Total
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$grandamount = number_format($order->getGrandTotal(),2);
$coupon = $order->getCouponCode();
$amountfood = number_format($order->getSubtotal('Food'), 2);
$amountnonfood = number_format($order->getSubtotal('Non_Food'), 2);
$discount = number_format(0.00 - $order->getDiscountAmount(), 2);
?>
If I use $amountfood = number_format($order->getSubtotal(), 2); it does work for the subtotal including both the food and non food values.
Could someone please help me with that.
Thanks,
Daniel
I don't think this information is available directly: magento stores the subtotal and taxes in a global way, only the total, no detailed information.
What you could do, is fetch the ordered products, for each one get his tax class and store in an array the sale value.
Something like this:
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$subtotals = array();
foreach ($items as $_item) {
if (array_key_exists($subtotals[$_item->getTaxClassId()])) {
$subtotals[$_item->getTaxClassId()] += $_item->getRowTotal();
} else {
$subtotals[$_item->getTaxClassId()] = $_item->getRowTotal();
}
}
not sure if the "if" is needed though.
hope that helps