Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIACart.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   Commercial License
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class EIACart
{

    public function save($obj, $row, $id_process, $validateOnly = false)
    {
        $cart = new Cart();
        $cart->id_shop = $obj->id_shop;
        $cart->id_lang = $obj->id_lang;
        $cart->id_currency = $obj->id_currency;
        $cart->id_customer = $obj->id_customer;
        $cart->id_carrier = $obj->id_carrier;
        $cart->id_address_invoice = $obj->id_address_invoice;
        $cart->id_address_delivery = $obj->id_address_delivery;
        $cart->recyclable = $obj->recyclable;
        $cart->gift = $obj->gift;
        $cart->gift_message = $obj->gift_message;
        $cart->date_add = $obj->date_add;
        $cart->date_upd = $obj->date_upd;
        if (($error = $cart->validateFields(false, true)) !== true) {
            throw new PrestaShopException("Error: $error. Row in file: $row.");
        }
        if (!$validateOnly) {
            $cart->add();
            return (int) $cart->id;
        }
        
        return 1;
    }

    public function update($obj)
    {
        $id_cart = Cart::getCartIdByOrderId($obj->id_order);
        Db::getInstance()->insert('cart_product', [
            'id_product' => (int) $obj->product_id,
            'id_product_attribute' => (int) $obj->product_attribute_id,
            'id_cart' => (int) $id_cart,
            'id_address_delivery' => (int) $this->getAddress($obj->id_order),
            'id_shop' => $obj->id_shop,
            'quantity' => (int) $obj->product_quantity,
            'date_add' => date('Y-m-d H:i:s'),
        ], false, true, Db::INSERT_IGNORE);
    }

    private function getAddress($id_order)
    {
        $sql = "
                SELECT o.id_address_delivery
                FROM " . _DB_PREFIX_ . "orders as o
                WHERE " . (int) $id_order . "
               ";
        $id_address_delivery = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
        if (isset($id_address_delivery) && $id_address_delivery) {
            return $id_address_delivery;
        }
        return 0;
    }

}