Jump to content

Custom price to a product PS 1.7


qruim

Recommended Posts

On 8/12/2023 at 12:15 AM, Eutanasio said:

why don't you make a product for 1€ value, so then the customer has to purchase as much quantities as he wants the voucher value to be?

I proposed this solution, but the customer refused it (God knows why).
Now I'm trying to implement this mechanic as follows: the base price of the voucher is 50 conventional units, to this value is added the value from input

I wrote such a module, but I don't think it works, I'm quite new to Prestashop so I don't understand why it doesn't work

class CustomPriceAdjustment extends Module
{
    public function __construct()
    {
        $this->name = 'custompriceadjustment';
        $this->tab = 'pricing_promotion';
        $this->version = '1.0.0';
        $this->author = 'name';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Custom Price Adjustment');
        $this->description = $this->l('Allows customers to adjust product prices.');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    public function install()
    {
        return parent::install() &&
            $this->registerHook('displayProductAdditionalInfo') &&
            $this->registerHook('actionCartSave');
    }

    public function hookActionCartSave($params)
    {
        $this->updatePriceWithAdjustment($params['cart']);
    }

    private function updatePriceWithAdjustment($cart)
    {
        $priceAdjustment = (float) Tools::getValue('priceAdjustment', 0);

        // Loop through cart products and update their prices
        foreach ($cart->getProducts() as $product) {
            if ($priceAdjustment > 0) {
                $productPrice = $product['price'];
                $newProductPrice = $productPrice + $priceAdjustment;

                $cart->updateQty(
                    $product['quantity'],
                    $product['id_product'],
                    $product['id_product_attribute'],
                    null,
                    'up',
                    0,
                    null,
                    false,
                    $newProductPrice
                );
            }
        }
    }

}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...