Magento - tracking who to give back to - magento

We have a Magento multi-site that give a percentage back to a non-profit, and what we would like to do is to allow customers to select which non-profit or a group within that non-profit to receive the percentage.
Trying to keep it simple we thought allowing customers to enter a discount code named something like "GIVE BACK to {non-profit name}" (but no actual dollar amount subtracted from the purchase or maybe just a penny, I don't think you can have a 0 to a discount code), then internally we know to give the percentage back to that non-profit.
It just seems a little bit of an odd way of doing this, it would be better to have a drop down of the none-profits at the end, but we are not sure how to create that.
Does anyone have any suggestions, on an easy way of doing this?
Thanks in advance!

You could add an attribute to the quote/order model and then populate that with an appropriate value, populated at the cart level? It's the kind of think we've done for affiliate modules we've built in the past.
You'll need a custom controller to grab the value when the customer moves from the cart to the checkout, which means you'll also need to make the Cart -> Checkout step a form submission instead of a straight link.
Then, at the end of the month, you'll need to run a report on the collection with something along the lines of:
// I've added a * in the SELECT because I'm not sure of the attribute names off the top of my head :)
$collection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('*')
// Make sure the orders are in the correct date range
->addAttributeToFilter(...)
// Make sure the orders are in a valid state, e.g. processing, pending, complete, etc..
->addAttributeToFilter(...)
$donation_total = array();
foreach ($collection as $order) {
// You'll have to investigate the attribute values for these
$charity = $order->getData('charity_attribute_code');
$order_total = $order->getData('order_total_attribute_code');
if (!isset($donation_total[$charity])) {
$donation_total[$charity] = 0;
}
$donation_total[$charity] += $order_total;
}
print_r($donation_total);
You could make this more efficient with proper SUM()ing in the query.

Related

Magento - Get attribute options value and quantity

Hello and good day to all the members of this great community. I'm still new in PHP and especially in Magento.
I'm not posting, waiting for answers, and leaving without replying back. This is a learning process. I hope to get a great support from all of you.
I have a product. I did create custom option for the product, that is an attribute named "a_size". The attribute has value of S, M and L. Each of the value has quantity.
In the single product view, I would like to call all the available size. That is the size (S, M, or L) that has quantity more than 0. I just want to show the available size, not how much the size left.
Can anybody guide me? I'm using Magento 1.7.x and as far for this 2 weeks, I did try pretty many of suggested answers from the community thru the search function.
The replies will be much appreciated. Thank you.
There are a few things to try.
Firstly check that when you set up your new attribute in the Magento Admin (Catalog->Attributes->Manage Attribute) that in the Frontend Properties box you have set Visible on Product View Page on Front-end to yes.
To get size values I use this code:
$cabac_sizeAttribute = $_product->getAttributeText("a_size");
but I have other code for getting attribute values that goes like this:
$_product_helper = Mage::helper('catalog/output');
$temp = $_product_helper->productAttribute($_product, $_product->getASize(), 'a_size');
I think it is related to the type of attribute: text, dropdown, multiselect etc so try both and see how you get on. But really the function productAttribute() is just applying formatting. You can read the function in the file app/core/Mage/Catalog/Helper/Output.php
Also, I wonder, if you have set up a configurable product and you are on the product view page then you will be viewing the configurable product. That product won't have an a_size value: you are trying to access the a_size attribute of the simple products that make up the configurable product, yes? Everything I wrote above is (I think) correct but to get the attribute of the simple products that are part of a configured product you should study the code in the function getJsonConfig() of the file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
And in particular to these lines:
//file: file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
//class: Mage_Catalog_Block_Product_View_Type_Configurable
//function: getJsonConfig()
foreach ($this->getAllowProducts() as $product) {
$productId = $product->getId();
foreach ($this->getAllowAttributes() as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
Being careful about variable naming: $product is local here, I suggest changing it, and about $this - but if you are in a .phtml of the product view for configurables then I think your $this is already Mage_Catalog_Block_Product_View_Type_Configurable
Welcome to Magento coding. You are doing well; it is a long but rewarding path. (hints: local.xml is your vital friend and so is Alan Storm if you haven't come across his content yet.)
[Additionally, (welcome to Magento) I think you are trying to say eg S and L are out of stock and M is in stock but actually the function getAllowProducts() will disallow a product with zero stock and exclude it from the returned object. You will need to use
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
(taken from function getAllowProducts() in file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php)
and then, if needed, check that each product is allowed to be shown eg status=ENABLED, and then check its stock level...
]
Malachy.
If you want to get the values of your drop down attribute use the following code
$_product->getASize();
and initially load the product object

Magento Event Observer Change Cart Base Subtotal

I've been trying to get this right for the past couple of days. I've read so many posts I am sure I am close (or at least close at some point) but I just can't seem to get this. I am using the even observer checkout_cart_save_after Here is what I am doing inside of the checkout_cart_save_after
$session = Mage::getSingleton('checkout/session');
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->setBaseSubtotal(0);
$quote->save();
All I am trying to do is get the Subtotal to equal 0 ... From what I read I want to set the "BaseSubtotal" because of currency differences. Eventually what I will do with this once I can get it working is dynamically change the price so it's not always going to be 0. But baby steps here lol I just want to try and change the price to specific value first.
You forgot to do
$quote->setSubtotal(0); // needs to be there ;)
$quote->setBaseSubtotal(0);
An object and a Base objects are two different things and both need to be set.

Magento - Calculated Product Attribute (property)

I would like to add a calculated attribute (property) to Products. It's value is to be calculated using a PHP function eg:
function CalculateCustomAttribute() {
...
//Do some calculations based on other Product attributes, date, etc
...
return $calculatedValue; // type float
}
This calculated attribute needs to be:
displayed in the Product page,
filterable through the "Layered Navigation", and
sortable in the "Product Listing".
Could this be done? And how?
What you want to do might be possible, but I am not sure that the approach you have described would be doable, I think it is too simplistic to work with the very complex Magento platform.
I had a similar project where the actual price of the products was constantly changing based on a few inputs and I was able to solve the problem fairly well, but it was definitely more complicated thank what you seem to be hoping for. I am not sure this scenario is helpful to you or not, but here it goes...
The basic idea was that I created new product attributes (eav attributes). These served as the inputs to determine what the price really should be. Note that in my case, these attributes were being updated fairly regularly by an outside process.
Then I created an observer on the "catalog_product_save_before" event that would simply do something like this:
//some calculations to get the $newPrice
$product->setPrice($newPrice);
So basically that will make it so that the price field will always be current whenever you save a product in the administrative screens.
Then also, since several of the attributes that were used as inputs were constantly changing (updated by an outside process), so we also had to add a magento cron job to run every so often, and it would recalculate the price for all the affected products with something like this...
//some calculations to get the $newPrice
$product->addAttributeUpdate("price", $newPrice, Mage::app()->getStore()->getStoreId());
So it all boils down to the fact that you should have the attribute saved in the db. And of course you need to find the specific spots of where to update that derived attribute. Maybe your requirements will vary slightly from what I have described, but it might get you on the right path at least.

Magento: increment_id empty after first order

I need the increment id from an order to create a folder on my server for moving some order specific files there, after a customer has successfully ordered something. This is what I've got so far:
$chkoutSess = Mage::getSingleton('checkout/session');
$lastOrderId = $chkoutSess->getLastOrderId();
$order = Mage::getModel('sales/order');
$order->load($lastOrderId);
$incid = $order->getData("increment_id");
I guess this can be done much easier but it works for me. The problem I have with this is that the $order is totally empty when I order something "the first time". After this it always works. So I think I can only get this data out of a session object when there's an actual session...this of course is senseless because there should always be a "lastOrderId" when a customer orders something. Is there any other way to simply get the last order and it's incremented out of the database? I tried several things but the objects where always empty..
The function I use is executed right after the user successfully hits the order submit button (event observer "sales_order_place_after"). Anyone can help me with this?
Try $chkoutSess->getRealOrderId(); and not $chkoutSess->getLastOrderId();

Display Percentage or Amount of discount for catalog price rule in Magento

I'm struggling to figure out how to display the percentage or the amount of discount that is applied to a product in Magento via the Catalog Price Rules.
For example: I want the price to be displayed in the front-end as follows: [old-price] [special-price] [discount info] where [old-price] has a css strike through.
The [old-price] and [special-price] is available by default through the tax helper. I've tried using the CatalogRule model, but I have no way to load it with a product id as the load function expects an entity id and from what I can tell, there aren't any other useful methods to load by product ID. I've var dumped (as well as using get_class_methods) just about everything that I found in the price.phtml file (apart from $this of course), but nothing helps.
I could just use a simple calculation to work out the discount percentage or amount, but I have no way of knowing whether the catalog rule is based on a percentage, or fixed amount.
I hope this all makes sense?Thanks for the help.Rémy
I do agree Magento makes this kind of thing a bit too hard. I wanted to display the description of the coupon code next to the entered coupon code. I suspect my code will be able to help you on your way. I put this code at the top in the template checkout/cart/coupon.phtml:
<?php
$c = Mage::getResourceModel('salesrule/rule_collection');
$c->addBindParam('coupon_code', $this->getCouponCode());
$c->getSelect()->where("coupon_code is null or coupon_code='' or coupon_code=:coupon_code");
foreach ($c->getItems() as $item) {
$coupon_description = $item->getDescription();
}
?>
So you can see $coupon_description now holds the description of the Shopping Cart Price Rule as long as the user specified a coupon code. You can add more properties from the coupon this way.

Resources