2

So here's my project. Make our giftcard integration purchasable.

Basically I need to get an "Enter Custom Amount" input field to update the price of the item. What I have been told by our architect is that I should Update the MyCompany_GiftCard_Model_Product_Type_GiftCard class to extend Mage_Catalog_Model_Product_Type_Abstract

then override the _prepareProduct Method to use the value from the custom amount if defined. So. I made this method:

protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode) {
    $result = parent::_prepareProduct($buyRequest, $product, $processMode);
    $oRide = $buyRequest->getOverrideAmount();
    $stn_amt = $buyRequest->getAmount();
    $productGiftCard = $buyRequest->getGiftCard();

    if(isset($oRide) && ($oRide != '') && ($oRide != 0) && ($oRide != null)){
        $result[0]->addCustomOption('giftcard_amount', $oRide);
    }else{
        $result[0]->addCustomOption('giftcard_amount', $amount);
    }

    if (!empty($productGiftCard)) {
        return $result;
    } 
    return Mage::helper('catalog')->__('Please specify the required options of product.');
}

Then I was told to update the getFinalPrice() method to accept the new value from MyCompany_GiftCard_Model_Product_Type_GiftCard_Price

public function getFinalPrice($qty = null, $product)
{
    $finalPrice = parent::getFinalPrice($qty, $product);
    $amount = $product->getCustomOption('giftcard_amount'); // its an object or false
    if($amount) {
        $finalPrice = $qty * $amount->getValue();
    }
    // echo '<br>finalPrice: '.$finalPrice;
    $product->setFinalPrice($finalPrice);
    // echo $product->getData('final_price');
    // Mage_Sales_Model_Quote_Item_Abstract

    return max(0, $product->getData('final_price'));
}

Here's the thing. If you do: echo $product->getData('final_price')); It totally equals the price I want to be! If the custom amount is set, final_price == custom amount!

Does anyone have any ideas where I'm going wrong and why the GetFinalPrice is not the price for the item in checkout?

(I'm rather new to Magento, 3 months-ish, but our architect was quite adamant that updating the getFinalPrice() method directly update the price in the cart! He's unavailable right now, and I have a deadline.

1
  • Kudos for a well written question. You should add that your issue is that in cart the price of the item is not your custom amount (assuming I understood you correctly). You need to set that price for the quote - when the item is added to cart
    – Haim
    Nov 9, 2018 at 0:26

2 Answers 2

2

You are correct that getFinalPrice is the correct place, however I think you may be missing some calls to 'set it' correctly.

Have a look at this github file:

https://github.com/ProxiBlue/GiftPromo/blob/master/app/code/community/ProxiBlue/GiftPromo/Model/Product/Type/Gift/Simple/Price.php

This is from a GiftPromo module I wrote aeons ago. It basically does teh same thing - The product (proper product in catalog, with price) can be gifted (no new sku is used, the actual catalog product, and SKU , is gifted, BUT the price is adapted to what i set in admin for that product gifting rule)

In my code you will see I get the price for the item, from a value stored in info_buyRequest ($giftedPrice = $buyRequest->getGiftedPrice();) - I literally set the 'updated price' to the info_BuyRequest upon add-to-cart, and then set the finalPrice, based on that value, in the given routine. (this is all handled in lines 23-46, with teh value resulting in $giftedPrice

Now, if you check my code, there are a couple of things I do, that you don't (I have not used/worked with this moule in years, so I don't recall 100% what they all do)

$finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);

$product->setCalculatedFinalPrice($finalPrice);

I highly suspect that last one is what you are misisng.

I hope this helps in some way.

PS: You will find a Price.php class for all product types in the parent folder:

https://github.com/ProxiBlue/GiftPromo/tree/master/app/code/community/ProxiBlue/GiftPromo/Model/Product/Type/Gift

1
  • This is a pretty great answer. Thanks! I actually figured out the issue. There was an observer that changing the price right before it updated the cart price! Somehow I missed it. Again, I am new to Magento, so I don't always pick up on everything. It seems that the case where adding a customOption was already anticipated and had a 'code' already set up in the observer. But I will indeed go through your module and have a look around. Thank you @ProxiBlue +1 I will also post a more explicit explanation shortly with code examples.
    – dustbuster
    Nov 9, 2018 at 14:35
2

I was getting frustrated, so I just did a general search of the code for ['amount'] and this is what I found inside of app/code/local/Company/Giftcard/Model/Observer.php:

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    // set custom price for the quote item
    $item = $observer->getQuoteItem();
    $product = $item->getProduct();
    if ($product->isGiftCard()) {
        if ($customPrice = $product->getData('custom_option')) {
            $item->setCustomPrice($customPrice);
            $item->setOriginalCustomPrice($customPrice);
        }

        // save reorder data
        $action = Mage::app()->getFrontController()->getAction();

        if (!$action) {
            return;
        }

        if ($customWishListPrice = $item->getOptionByCode('giftcard_amount')) {
            $item->setCustomPrice($customWishListPrice->getValue());
            $item->setOriginalCustomPrice($customWishListPrice->getValue());
        }
        if ($action->getFullActionName() == 'sales_order_reorder') {
            $item = $observer->getQuoteItem();
            $options = $item->getBuyRequest()->getGiftCard();
            parse_str($options, $optionsArray);
            if (count($optionsArray)) {
                $item->setPrice($optionsArray['amount']);
                unset($optionsArray['amount']);
                $additionalOptions = array();
                foreach ($optionsArray as $optionCode => $value) {
                    $additionalOptions[] = array(
                        'label' => $optionCode,
                        'value' => $value,
                    );
                }
                $item->addOption(array(
                    'code' => 'additional_options',
                    'value' => serialize($additionalOptions)
                ));
            }
        }
    }
}

The part that was causing me the headache was

if ($customPrice = $product->getData('custom_option')) {
    $item->setCustomPrice($customPrice);
    $item->setOriginalCustomPrice($customPrice);
}

So I changed it to

if ($customPrice = $product->getData('giftcard_amount')) {
    $item->setCustomPrice($customPrice);
    $item->setOriginalCustomPrice($customPrice);
}

and it worked! There were also some other supporting observer methods that I had to change the custom_amount to giftcard_amount. But this example is enough to support my point. This module was already coded to accept a custom_amount for the giftcard, but I didn't know it!

I thought I did everything right but somehow 'magic gremlins' were getting ahold of my request and swapping around the amounts and submitting them to the cart. But it was just an observer I hadn't noticed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.