3

I have used magento many times, but this is the ultimate challenge. Im working on a magento store which has over 400,000 products - each with its own variation / product options. Hundreds of products are added and removed daily on our master store (which is based on a custom shopping cart system and runs on MSSQL).

I have configured magento to grab all the categories, products, text, descriptio, prices, variations etc and create the product pages dynamically on the fly e.g http://www.offices-furniture.co.uk/pp?prod=mercury-reception-unit.html

The problem is I now need to be able to add these products to the shopping cart without them physically existing in the back end. I have added one product to the back end and plan to use this as a GENERAL template type product, so its always this product (variations of it) that get added to the cart e.g

http://www.offices-furniture.co.uk/frodo.php but I cannot for the life of me get the price to change.... grrrr..

If anyone could point me in the right direction on how to change the price via HTML or PHP on the front end and post it to the shopping cart without changing the price on the back end

Thanks in advance all…

Here is the code i’ve tried using to change the price;

    <?php
require_once ("app/Mage.php");
umask(0);

Mage::app("default");

Mage::getSingleton("core/session", array("name" => "frontend"));

// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->setCustomPrice(99);
$product->setOriginalCustomPrice(99);
$product->getProduct()->setIsSuperMode(true);
$product->setTypeId('configurable');
$product->setTaxClassId(1); //none
$product->setSku(ereg_replace("\n","","videoTest2.2"));
$product->setName(ereg_replace("\n","","videoTest2.2"));
$product->setDescription("videoTest2.2");
$product->setPrice("129.95");
$product->setShortDescription(ereg_replace("\n","","videoTest2.2"));
$cart->save();

if(isset($_POST['submit'])){

// call the Magento catalog/product model

$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($_POST['product']);
*/

////////////////////////////
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($_POST['product']);
$product->setCustomPrice(99);
$product->setOriginalCustomPrice(99);
$product->getProduct()->setIsSuperMode(true);
$product->setTypeId('configurable');
$product->setTaxClassId(1); //none
$product->setSku(ereg_replace("\n","","videoTest2.2"));
$product->setName(ereg_replace("\n","","videoTest2.2"));
$product->setDescription("videoTest2.2");
$product->setPrice("129.95");
$product->setShortDescription(ereg_replace("\n","","videoTest2.2"));
$cart->save();
/////////////////////////////////////

// start adding the product
// format: addProduct(<product id>, array(
// 'qty' => <quantity>,
// 'super_attribute' => array(<attribute id> => <option id>)
// )
// )
$cart->addProduct($product, array(
'qty' => $_POST['qty'],
'price' => 50,

'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
)
);

// save the cart
$cart->save();

// very straightforward, set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

// redirect to index.php
header("Location: frodo.php");

}else{
?> 
  • I dont think you can change price that way... take a look at http://stackoverflow.com/questions/9721583/changing-the-price-in-quote-while-adding-product-to-cart-magento – MagePal Extensions Oct 11 '12 at 22:46

2 Answers2

5

Here is a snippet which might help you out... you can definitely change the price on the fly when a product is added to the cart. I'm using this on the checkout_cart_save_before event observer, and in this example it triggers when the weight of an item is over 10 lbs.

/**
 * Set the price if the weight is over 10 lbs
 * 
 * @param Varien_Event_Observer $observer
 */
public function setPriceOnCartSaveBefore(Varien_Event_Observer $observer)
{
    $cart = $observer->getCart();
    $items = $cart->getItems();

    foreach($items as $item) {
        if ($item->getWeight() > 10) {
            $item->setCustomPrice(69.99);
            $item->setOriginalCustomPrice(69.99);
        }
    }
}
Mark Shust at M.academy
  • 6,300
  • 4
  • 32
  • 50
2

Your question is not completely clear. There are two possible ways of interpreting this (of which I'm guessing you mean the second, because the first problem has a relatively easy solution):

1) You only need the custom price in the shopping cart, but it doesn't need to last through checkout
2) You do actually need to be able to sell the product for the custom price using the Magento checkout.

Ad 1: Only change price in shopping cart

This is relatively easy. I would use JavaScript and a custom PHP script that is accessible through AJAX and can calculate the price that should be displayed. This can then be done through DOM manipulation. CSS can help you hide the price until the AJAX calculation is finished.

Another way of doing it would be to edit the price template file. Because Magento phtml files get called within the View class of the object that is currently rendering (e.g. cart or quote), you will be able to get the ProductID. You can then check if the Product that is being added is your magic custom template product and change the price accordingly.

In the base/default template you would get the item ID as such in base/default/template/checkout/cart/item/default.phtml

$product_id = $_item->getProduct()->getId();

When you figure out what combination of Weee, InclTax etc. you have used for your website (so you know where in default.pthml your price actually gets displayed), you can get an if statement in there and display the custom price somehow.

Ad 2: Keep price changed through checkout

I don't think it's possible to do this. Especially with Magento obsessing about order integrity (being that products and their info will always be available through that order, even if you delete them from the catalog).

The closest thing you'll get to this (at least as far as I can imagine) is to have the template product you set up include a custom drop-down option that enables using a variable price.

You could try to set the price of the only value for that custom drop-down option dynamically, but I doubt even that would work. The last thing you could try then is to add a value (with your custom price) to the custom option for that product every time an order is placed. That way, you keep Magento overhead to a minimum, but still satisfy Magento bureaucracy by providing Magento a way to keep a history of the physical product you have sold.

Another suggestion

There is also the possibility of using products with custom options the way they were meant to be used. If you create a basic template product with enough custom options (e.g. shirt size, color, print, fabric) and add new custom option values on the fly. This way you could also check if an option already exists and each option can have its own added price value.

Last suggestion

If you really want to go all-out, you could try writing a custom module for Magento that in turn:

  • Creates a product when it gets added to the basket.
  • Removes that product again when the order is finished or when a customer removes it from the basket.
  • Prunes the custom products periodically (e.g. through Mage/cron) provided they are not still in a stored basked for any of your customers.

This would create temporary products instead of no products at all.

I hope I have shared some thoughts that can help you move along!

eleven59
  • 137
  • 4
  • 15