I've programmaticaly allowed the customer to edit the price of a product.
the problem is when i add product with 20$ and adding again the same product with 30$ in the shopping cart page it displays the product -| qty=2 -| total price=60$
so this is not logic the total price must be 50$ and it should not set qty to 2
i know the problem is in the SKU is there a solution for this i don't wanna modify the SKU?
for more details this is my previous question
So this is doable, but unfortunately there is no way to do it without a pretty significant rewrite. I'm assuming that you have some way to identify the different between the two quote items besides price. If that's the case:
sales/quote_item: representProduct()
the rewrite configuration in your module:
<?xml version="1.0"?>
<config>
<global>
<models>
<sales>
<rewrite>
<!--Your full class name should be specified-->
<quote_item>Namespace_Module_Model_Sales_Quote_Item</quote_item>
</rewrite>
</sales>
</models>
</global>
</config>
the original function:
public function representProduct($product)
{
$itemProduct = $this->getProduct();
if (!$product || $itemProduct->getId() != $product->getId()) {
return false;
}
/**
* Check maybe product is planned to be a child of some quote item - in this case we limit search
* only within same parent item
*/
$stickWithinParent = $product->getStickWithinParent();
if ($stickWithinParent) {
if ($this->getParentItem() !== $stickWithinParent) {
return false;
}
}
// Check options
$itemOptions = $this->getOptionsByCode();
$productOptions = $product->getCustomOptions();
if (!$this->compareOptions($itemOptions, $productOptions)) {
return false;
}
if (!$this->compareOptions($productOptions, $itemOptions)) {
return false;
}
return true;
}
our function:
public function representProduct($product) {
$parentResult = parent::representProduct($product);
//if parent result is already false, we already have a different product, exit.
if ($parentResult === false) {
return $parentResult;
}
$itemProduct = $this->getProduct();
/*
* do our check for 'same product' here.
* Returns true if attribute is the same thus it is hte same product
*/
if ($product->getSomeAttribute() == $itemProduct->getSomeAttribute()) {
return true; //same product
} else {
return false; //different product
}
}
If this function returns true, the products will be combined in the cart as the same item and thus you'll just increase your qty by the new qty amount.
If this function returns false, the products will look different and will appear as different items in the cart.
Add Same product with different "SKU" and u can give different price for those products.
this is the class :
<?php
class WebDirect_CustomPrice_Model_Sales_Quote_Item extends Mage_Sales_Model_Quote_Item
{
public function representProduct($product) {
$parentResult = parent::representProduct($product);
//if parent result is already false, we already have a different product, exit.
if ($parentResult === false) {
return $parentResult;
}
$itemProduct = $this->getProduct();
/*
* do our check for 'same product' here.
* Returns true if attribute is the same thus it is hte same product
*/
if ($product->getPrice() == $itemProduct->getPrice()) {
return true; //same product
} else {
return false; //different product
}
}
}
Related
I want a rounding grand total; I have created a custom module and rewritten the core models to achieve this.
My rewrite Model code is below
1. Mage_Sales_Model_Quote_Address_Total_Grand
<?php
class Lr_Roundtotal_Model_Quote_Address_Total_Grand extends Mage_Sales_Model_Quote_Address_Total_Grand
{
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$grandTotal = $address->getGrandTotal();
$baseGrandTotal = $address->getBaseGrandTotal();
$totals = array_sum($address->getAllTotalAmounts());
$baseTotals = array_sum($address->getAllBaseTotalAmounts());
$address->setGrandTotal(round($grandTotal+$totals)); //Modificated
$address->setBaseGrandTotal(round($baseGrandTotal+$baseTotals)); //Modificated
//$address->setGrandTotal($grandTotal+$totals); --Original
//$address->setBaseGrandTotal($baseGrandTotal+$baseTotals); --Original
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$address->addTotal(array(
'code' => $this->getCode(),
'title' => Mage::helper('sales')->__('Grand Total'),
'value' => round($address->getGrandTotal()),
'netvalue' => round($address->getGrandTotal()),
'area' => 'footer',
));
return $this;
}
}
and second one is
2.Mage_Sales_Model_Order_Invoice
<?php
class Lr_Roundtotal_Model_Order_Invoice extends Mage_Sales_Model_Order_Invoice
{
public function pay()
{
if ($this->_wasPayCalled) {
return $this;
}
$this->_wasPayCalled = true;
$invoiceState = self::STATE_PAID;
if ($this->getOrder()->getPayment()->hasForcedState()) {
$invoiceState = $this->getOrder()->getPayment()->getForcedState();
}
$this->setState($invoiceState);
$this->getOrder()->getPayment()->pay($this);
$this->getOrder()->setTotalPaid(
round($this->getOrder()->getTotalPaid()+$this->getGrandTotal()) //Modificated
// $this->getOrder()->getTotalPaid()+$this->getGrandTotal() --Original
);
$this->getOrder()->setBaseTotalPaid(
round($this->getOrder()->getBaseTotalPaid()+$this->getBaseGrandTotal()) //Modificated
// $this->getOrder()->getBaseTotalPaid()+$this->getBaseGrandTotal() --Original
);
Mage::dispatchEvent('sales_order_invoice_pay', array($this->_eventObject=>$this));
return $this;
}
}
For example
Cart
sub-total : 990.00
discount : 120.70
Grand Total: 869.00(rounded)
Invoice
sub-total : 990.00
discount : 120.70
Grand Total: 869.30(not-rounded)
I want same grand total in cart and Invoice
Once you overwrite the core file in local then implement below code.
First create one function in the HELPER file like below(no need to create new module but you can put this function in any module helper file):
Namespace_module_helper_data extends Mage_Core_Helper_Abstract
{
public function getFormatedPrice($price)
{
return Mage::getModel('directory/currency')->format($price, array('display'=>Zend_Currency::NO_SYMBOL), false);
}
}
then you can just use this function where you need to round the price in any where in Magento.
You can use helper function like below:
$helper = Mage::helper('modulename'); // module name means name of the module in which you have create helper
Use function for round price like below:
$price = 120.12456;
echo $helper->getFormatedPrice($price); // you can get round price as per your store.
I have created a new product type in Magento. However, I am having difficulty adding all of its associated products to the sales_flat_quote_item table. I only want the associated products added to the table and only the main parent product visible in the cart.
I am real close to achieving this. Right now only the parent item is visible in the cart when adding it to the cart. However, only one of it's associated products are being listed in the above mentioned table.
Here is a snippet of my code:
class Namespace_Module_Model_Product_Type_Custom extends Mage_Catalog_Model_Product_Type_Abstract {
........
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
$qty = $buyRequest['qty'];
$associatedQty = $buyRequest['associated_qty'];
if($qty >= 1) {
$result = parent::_prepareProduct($buyRequest, $product, $processMode);
if (is_array($result)) {
$product = $this->getProduct($product);
foreach($buyRequest['associated'] as $associated){
if($associated){
$subProducts[] = Mage::getModel('catalog/product')->load($associated);
}
}
foreach($subProducts as $subProduct){
if($subProduct){
$product->setCartQty($qty);
$product->addCustomOption('product_qty_'.$subProduct->getId(), $associatedQty[$subProduct->getId()], $subProduct);
$product->addCustomOption('associated_product_' . $subProduct->getId(), $associatedQty[$subProduct->getId()]);
}
}
$_result = $subProduct->getTypeInstance(true)->_prepareProduct(
$buyRequest,
$subProduct,
$processMode
);
if (!isset($_result[0])) {
return Mage::helper('checkout')->__('Cannot add the item to shopping cart');
}
$_result[0]->setParentProductId($product->getId())
->addCustomOption('parent_product_id', $product->getId());
$result[] = $_result[0];
return $result;
} else {
return $this->getSpecifyOptionMessage();
}
} else {
return $this->getQtyMessage();
}
}
........
}
Right now only the associated product '53' is being added as a child product. I am still missing the other two. Basically, the foreach($subProducts as $subProduct) loop will loop 3 three times with the three associated products. I am assuming somewhere along the lines in Magento it is only using the last looped product.
Any advice or help with this would be great. Thanks in advance!
Got it figured out. I just had to shift the following into the foreach loop instead of outside of it.
$_result[0]->setParentProductId($product->getId())
->addCustomOption('parent_product_id', $product->getId());
$result[] = $_result[0];
I have a custom module which is called on the customer_save_observer_executed event and specifically when a customer updates their details in their account page (name, password, email etc) I have added a custom attribute called display_name.
When a customer submits this form i need to check if the display_name currently exists for any other customer. If it doesn't then setDisplayName(...) else do nothing/display error message.
I was hoping to do this with this snippet:
$customer_check = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('display_name')
->addAttributeToFilter('display_name',$new_name)->load();
if ( is_object($customer_check) && count($customer_check) >= 2) {
// dont allow - duplicate customer displayname
}
else {
// allow update....
}
My current code in Model -> Observer.php
class xxxxxxx_Model_Observer
{
public function xxxxxxxx(Varien_Event_Observer $observer)
{
// to stop event being fired twice
if(Mage::registry('customer_save_observer_executed')){
return $this;
}
$postData = Mage::app()->getRequest()->getPost();
$customer = $observer->getCustomer();
// if updating NOT a new customer
if($customer instanceof Mage_Customer_Model_Customer && !$customer->isObjectNew()) {
// check display name is posted
if(!empty($postData['display_name'])){
$current_name = $customer->getDisplayName();
$new_name = $postData['display_name'];
// duplicate check
$customer_check = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('display_name')
->addAttributeToFilter('display_name',$new_name)->load();
if ( is_object($customer_check) && count($customer_check) >= 2) {
// dont allow - duplicate customer displayname
}
else {
if( $postData['display_name'] !== $current_name ) {
$customer->setDisplayName($postData['display_name']);
}
}
}
}
Mage::register('customer_save_observer_executed',true);
}
}
but this just updates the display_name even if i deliberately set it to a duplicate of another customers
UPDATE
Looking into this further it looks like the module function itself is not being run or nothing inside it is taking affect, as whatever i put in it doesn't work. It is infact the default behaviour that is setting the displayname not my module. My module is activated and my config file uses the customer_save_commit_after event as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<xxx_xxxxx>
<version>0.0.1</version>
</xxx_xxxxx>
</modules>
<global>
<models>
<xxx_xxxxx>
<class>xxx_xxxxx_Model</class>
</xxx_xxxxx>
</models>
<events>
<customer_save_commit_after>
<observers>
<xxx_xxxxx>
<class>xxx_xxxxx/observer</class>
<method>xxxxxx</method>
<type>singleton</type>
</xxx_xxxxx>
</observers>
</customer_save_commit_after>
</events>
</global>
</config>
This probably is the case because all data from the form submitted is set on the customer model in Magento's Customer module (code taken from Mage_Customer_AccountController)
/**
* Forgot customer account information page
*/
public function editAction()
{
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
$block = $this->getLayout()->getBlock('customer_edit');
if ($block) {
$block->setRefererUrl($this->_getRefererUrl());
}
$data = $this->_getSession()->getCustomerFormData(true);
$customer = $this->_getSession()->getCustomer();
if (!empty($data)) {
$customer->addData($data);
}
if ($this->getRequest()->getParam('changepass') == 1) {
$customer->setChangePassword(1);
}
$this->getLayout()->getBlock('head')->setTitle($this->__('Account Information'));
$this->getLayout()->getBlock('messages')->setEscapeMessageFlag(true);
$this->renderLayout();
}
Here addData() is called on the customer model. To prevent this rename your form's input to something temporary, like <input name="display_name_tmp"/>, then in your controller do something like
public function xxxxxxxx(Varien_Event_Observer $observer)
{
// to stop event being fired twice
if(Mage::registry('customer_save_observer_executed')){
return $this;
}
$postData = Mage::app()->getRequest()->getPost();
$customer = $observer->getCustomer();
// if updating NOT a new customer
if($customer instanceof Mage_Customer_Model_Customer &&
!$customer->isObjectNew()) {
// check display name is posted
if(!empty($postData['display_name_tmp'])){
$current_name = $customer->getDisplayName();
$new_name = $postData['display_name_tmp'];
// duplicate check
$customer_check = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('display_name')
->addAttributeToFilter('display_name',$new_name)->load();
if ( is_object($customer_check) && count($customer_check) >= 2) {
// dont allow - duplicate customer displayname
}
else {
if( $postData['display_name'] !== $current_name ) {
$customer->setDisplayName($postData['display_name_tmp']);
}
}
}
}
Mage::register('customer_save_observer_executed',true);
}
Ideally we would like to prevent any items on an order from being shipped if they do not have an invoice created.
At the moment Magento (1.7.0.2) allows the creation of a shipment even if there has been no invoices created for the order. I realise this is by design for site owners who wish to ship first and invoice afterwards but our workflow requires payment first on creation of the invoice before allowing a shipment.
Could anyone please offer any suggestions code wise on what to change? I think the correct file would be /app/code/local/Mage/Sales/Model/Order/Shipment.php and the code we require to change would be below but im not sure how to check each item for an invoice individually.
Edit - Would the following code work well?
protected function _beforeSave()
{
if ((!$this->getId() || null !== $this->_items) && !count($this->getAllItems())) {
Mage::throwException(
Mage::helper('sales')->__('Cannot create an empty shipment.')
);
}
$order = $this->getOrder();
if ($order->canInvoice()) {
Mage::throwException(
Mage::helper('sales')->__('Cannot ship items without an Invoice.')
);
}
You can get some hints about the code to use by looking at this piece of code from: \app\code\core\Mage\Sales\Model\Order.php in the public function canInvoice():
public function canInvoice()
{
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_INVOICE) === false) {
return false;
}
foreach ($this->getAllItems() as $item) {
if ($item->getQtyToInvoice()>0 && !$item->getLockedDoInvoice()) {
return true;
}
}
return false;
}
I have developed a custom module to meet my project requirements using Alan Storms tutorial for creating modules in magento.
I had the requirement of changing the price attribute dynamically on frontend based on a livefeed. Everysecond the feed is updated so every time the page refreshes a new price must be displayed for each product on the site.
I have override the product module and the price modules for this purpose. The issue is with tier pricing. When tier pricing comes into place I need to calculate the tier-price based on the live price.
For this also I managed to change using the price_type class override.
Now whenever an item is added to cart the tier-pricing was not working for that I wrote event_trigger ie an Observer which updates the tier_pricing on the event "checkout_cart_save_before" and here's my code
class My_Custom_Model_Observer extends Varien_Event_Observer
{
public function __construct()
{
}
public function updateCartBasedOnLiveFeed($observer)
{
foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* #var $item Mage_Sales_Model_Quote_Item */)
{
$tierPrices = array();
$tierPrices = $item->getProduct()->getTierPrice();
$itemPrice = $item->getProduct()->getPrice();
$i=0;
foreach($tierPrices as $key => $tierPrice)
{
if(!is_numeric($key))
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrices[$key]['price'] = $updatedTierPrice;
$tierPrices[$key]['website_price'] = $updatedTierPrice;
}
else
{
if($tierPrice['price'] > 0)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrice['price'] = $updatedTierPrice;
$tierPrice['website_price'] = $updatedTierPrice;
$tierPrices[$i] = $tierPrice;
$i++;
}
}
}
$item->getProduct()->setData('tier_price',$tierPrices);
}
}
}
The above code works excellently in cart page. But when it comes to checkout page. It works for a single item and when tier-pricing comes into play it does apply cart prices.
Please help me with this.
I also tried using other events along with the above event.
Event: sales_quote_save_before
public function updateQuoteLive($observer)
{
$tierPrices = array();
$quote_item = $observer->getEvent()->getQuote;
$itemPrice = $quote_item->getProduct()->getPrice();
$tierPrices = $quote_item->getProduct()->getTierPrice();
$tierPricesSize = sizeof($tierPrices);
for($i=0;$i<$tierPricesSize;$i++)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrices[$i]['price']/100));
$tierPrices[$i]['price'] = $updatedTierPrice;
$tierPrices[$i]['website_price'] = $updatedTierPrice;
}
$quote_item->getProduct()->setData('tier_price',$tierPrices);
}
When I tried to print the getQuote() function available in Quote.php I find that the tier prices there are not the ones which I updated using the first event. So I think I need to update the price before saving the quote. Please any one help me and show the correct direction.
Please help me with this I am missing some important step. Any help is greatly appreciated.
Thanks in advance.
It might be better off "saving" the new price in to the database when you update.
Try something along the lines of:
$product = $observer->getProduct();
$procuct->setPrice($updatedPrice);
This way when it comes to checkout it will be pulling in the correct price from the database (and avoids the headache of correcting it "mid-flight"
i realized such a project like you. I have no sales_quote_save_before Observer. I only use the checkout_cart_save_before. Based on the session the price will be setted.
I realized that like this way:
public function updatePrice( $observer )
{
try {
$cart = $observer->getCart();
$items = $cart->getItems();
foreach($items as $item)
{
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
}
} catch ( Exception $e )
{
Mage::log( "checkout_cart_save_before: " . $e->getMessage() );
}
}
I calcute the tierprices on the fly and with this Observer. All prices will be set up correct in the qoute.
Maybe you should try this way.
Regards boti
At last figured out the issue and got the solution.
The problem was that in cart page or checkout page when the getTierPrice() function is called, which is present in /app/code/core/Mage/Catalog/Product.php. It takes one parameter named $qty which is by default null. This function in turn calls the function getTierPrice which is present in /app/code/core/Mage/Type/Price.php file which takes two parameters $qty and $productObject. By default $qty is null and when it is null the function returns an array of tier_prices. But when the $qty value is passed then the function returns a single for that particular quantity.
So, I wrote my own custom function which calculates the tier prices based no my requirements like
I overridden both the core files with my custom module following Alan Storm's tutorials.
I've extended Mage_Catalog_Model_Product with My_CustomModule_Model_Product class and
then
Mage_Catalog_Model_Product_Type_Price with My_CustomModule_Model_Price
And then in /app/code/local/My/Custommodule/Model/Product.php
I added my custom code like
public function getTierPrice($qty=null)
{
if($qty)
{
return $this->getPriceModel()->getCustomTierPrice($qty, $this);
}
return $this->getPriceModel()->getTierPrice($qty, $this);
}
Then in /app/code/local/My/Custommodule/Model/Price.php
<?php
public function getCustomTierPrice($qty = null, $product)
{
$allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
$prices = $product->getData('tier_price');
if (is_null($prices)) {
$attribute = $product->getResource()->getAttribute('tier_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($product);
$prices = $product->getData('tier_price');
}
}
foreach($prices as $key => $customPrices)
{
if($prices[$key]['price'] < 1)
{
$prices[$key]['price'] = abs($product->getPrice() - ($productPrice * ($customPrices['price']/100)));
$prices[$key]['website_price'] = $prices[$key]['price'];
}
}
}
which retured a customized value when $qty is passed and voila it worked.
I just posed this answer so that any one else who has similar requirement may get benefited with this.
?>