Magento : Add Product to cart with custom price

2019-09-07 04:56发布

问题:

First I am developing an extension for custom price and I have an input on product page, this is an image describes what I've done :

when a customer enters the price he wants and clicks add to cart, the product must be added with that price added by him.

I know that can be coded in the controller but I don't know how?

this is the controller empty class :

<?php

class WebDirect_CustomPrice_savePriceController extends Mage_Core_Controller_Front_Action{
    //put your code here
}

anyone knows how that add to cart button works(code)

回答1:

You need to call final_price observer for it. Need to follow bellow steps:

1 add Observer in etc/config.xml

<events>
  <catalog_product_get_final_price>
    <observers>
      <xyz_catalog_price_observer>
        <type>singleton</type>
        <class>Xyz_Catalog_Model_Price_Observer</class>
        <method>apply_customprice</method>
      </xyz_catalog_price_observer>
    </observers>
  </catalog_product_get_final_price>     
</events>
  1. Add method in you model apply_customprice()

     public function apply_customprice($observer)
     {
         $event = $observer->getEvent();
         $product = $event->getProduct();
     // ADDD LOGIC HERE to get price added by customer
        $product->setFinalPrice($specialPrice); // set the product final price
        return $this;
     }
    

Click on below like where you can find how to set custom price when product added in cart.

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method



回答2:

As a starting point you can start at:

class Mage_Checkout_CartController extends Mage_Core_Controller_Front_Action
{

 /**
 * Add product to shopping cart action
 *
 * @return Mage_Core_Controller_Varien_Action
 * @throws Exception
 */
public function addAction()
{

Make sure you overwrite the add to cart route to point to yours (the new one that overwrites the Core route described above).

Also getting the price from a user's input will affect the checkout process as well, specifically the quote and everything that results from it (the cart, the order and so on).

Also, regarding onepage checkout be careful as the BE logic and the opcheckout.js are very tight together.

Chears



标签: magento