Jump to content

How to add same product as another entry to the cart?


Recommended Posts

Hi! 

I am developing a module, where user can configure certain parameters of the product - such as color, size, etc. 
The only thing I can't get to work is the fact, that entries in ps_cart_product are being stored with product_id as a primary key. And I need to allow users to add same product twice to the cart (with different color for example) - is there a way to do it? 

I was thinking about generating id_product with something like UUID, but then I would probably break a couple of things :D

Any help would be appreciated! 

Kind regards,
Bartek

Edited by Nietypowy Informatyk (see edit history)
Link to comment
Share on other sites

Hi.
This is done by creating your own fields and values for customization on the fly.
Look at the tables in the database:

ps_customization, ps_customization_field, prs_customization_field_lang, ps_customized_data.

Also check the ./classes/Customization.php and ./classes/CustomizationField.php files.

 

Link to comment
Share on other sites

1. it is necessary to show the customer what he has selected and saved
2. you need to show JSON in the administration or in the email

In point 1. you can save the text or image, what the customer will see.
In point 2. you create your own table in which you write your JSON and id_customization.

It's hard to give advice like this when you don't know all the detailed information.

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

Ok, so let me give some more details, about whole module:

Backoffice part: 
Administrator of the shop can configure each product with following data: 
- available amounts
- parameters - different types: checkbox, color, single-pick and select
- discounts
- and some other stuff

All backoffice configuration is taking place in a Vue app, that makes REST calls to exposed endpoints. All data is stored in appropriate custom tables.

Front part: 
User can configure all parameters, amounts etc. and add to cart. This is the hardest part for me, but with some overrides and time I have managed to get it working with custom cart controller, which calculates price, and adds to the cart - so far so good. I have also managed to show information about parameters in admin view, and invoice - using following hooks:
- displayAdminOrderTabContent
- displayPDFInvoice

By "add a duplicate" I meant duplication of data in database - in my tables responsible for storing data about price, parameters etc. and the native customization tables which you have mentioned.

Hope it helps!

Link to comment
Share on other sites

Sample add customization to databse with id cart and customization price (the whole cart is not built in):

public function addMyProductCustomization($product, $customizedSumPrice, $customizedWeight)
{
    $db = Db::getInstance();
    $custimzedFieldName = 'Customization';
    $customizedValue = 'product customized';
    $languages = Language::getLanguages(true);
    $idShop = $this->context->shop->id;
    $moduleName = 'myModule';

    $idModule = $db->getValue("SELECT id_module FROM "._DB_PREFIX_."module WHERE name = '".$moduleName."'");

    if (!$customized_datas) {
        return;
    }

    $idProduct = $product->id;
    $idProductAttribute = $product->id_product_attribute;

    /* CART */
    if ($this->context->cookie->id_cart){
        $cart = new Cart($this->context->cookie->id_cart);
    } else {
        $cart = new Cart();
    }

    /* PRODUCT CAN BE ADDED TO CART HERE */
    $idCart = $cart->id;

    /* CUSTOMIZATION */
    $db->insert('customization', 
        [
            'id_product_attribute' => $idProductAttribute,
            'id_cart' => $idCart,
            'id_product' => $idProduct,
            'quantity' => $customize['quantity'],
            'in_cart' => 1
        ]
    );

    $getInsertedCustomizationId = $db->Insert_ID();

    /* CUSTOMIZATION FIELD */
    $db->insert('customization_field', 
        [
            'id_product' => $idProduct,
            'type' => '1', /* text field */
            'required' => 1,
            'is_module' => 1,
            'is_deleted' => 0
        ]
    );

    $getInsertedCustomizationFieldId = $db->Insert_ID();


    /* CUSTOMIZATION FIELD LANG */
    foreach ($languages as $lang){
        $db->insert('customization_field_lang', 
            [
                'id_customization_field' => $getInsertedCustomizationFieldId ,
                'id_lang' => $lang['id_lang'],
                'id_shop' => $idShop,
                'name' => 1,
            ]
        );
    }

    /* CUSTOMIZED DATA */
    $getMaxIndex = $db->getValue('SELECT MAX(index) FROM '._DB_PREFIX_.'customized_data') + 1;
    $db->insert('customized_data', 
        [
            'id_customization' => $getInsertedCustomizationId,
            'type' => '1', /* text field */
            'index' => $getMaxIndex,
            'value' => $customizedValue,
            'id_module' => $idModule,
            'price' => $customizedSumPrice,
            'weight' => $customizedWeight
        ]
    );
}

 

  • Like 1
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...