Adding a specific price in the cart

1.2k views Asked by At

I would like to add a specific price for certain products in the cart. Currently I achieved that in one shop, but not in the other. I was able to add a specific price to a certain product (attribute) in the cart. I have this code that does the adding part:

foreach ($prices as $product) {
    $specific_price = new SpecificPrice;
    $specific_price->id_product = $product['id_product'];
    $specific_price->id_product_attribute = $product['id_product_attribute'];
    $specific_price->id_cart = (int)$params['cart']->id;
    $specific_price->id_shop = $this->context->shop->id;
    $specific_price->id_shop_group = $this->context->shop->id_shop_group;
    $specific_price->id_country = $this->context->country->id;
    $specific_price->id_currency = $this->context->currency->id;
    $specific_price->id_group = $this->context->customer->id_default_group;
    $specific_price->id_customer = $this->context->customer->id ? $this->context->customer->id : NULL;
    $specific_price->from_quantity = 0;
    $m2 = $product['m2'] < 0 ? 1 : round($product['m2']);
    $specific_price->price = Tools::ps_round($product['price'] * $m2 / $product['quantity'], 6);
    $specific_price->reduction_type = 'amount';
    $specific_price->reduction_tax = 1;
    $specific_price->reduction = 0;
    $specific_price->from = date('Y-m-d H:i:s');
    $specific_price->to = '0000-00-00 00:00:00';

    try {
        $specific_price->add(true);
    } catch (Exception $e) {
        Logger::addLog(sprintf('[%s] %s, line %d', $this->name, $e->getMessage(), __LINE__));
            echo $e->getMessage();
        if ($e->getCode() == 0) {
            try {
                $specific_price->update(true);
            } catch (Exception $e) {
                Logger::addLog(sprintf('[%s] %s, line %d', $this->name, $e->getMessage(), __LINE__ ));
                    echo $e->getMessage();
            }
       }
    }
}

In id_shop 1 it works, but in the other shop id_shop 2 it doesn't. I'm cracking my head for hours on this. I've been clean cache everywhere possible, restart XAMPP, check http.conf for clues.

I have disabled id_customer as being required. Because I want to price to be saved for visitors/guests too.

0

There are 0 answers