Cyburg Posted September 23, 2016 Share Posted September 23, 2016 Can anyone tell me how to add products to the cart programmatically from my custom controller file please (they are existing products in the database)? My custom controller file extends FrontController. I have seen a few posts on the forum about programmatically adding products to the cart, but I am having difficulty getting anything to work. I would be most grateful if anyone can provide some guidance. Cheers,Paul Link to comment Share on other sites More sharing options...
Cyburg Posted September 26, 2016 Author Share Posted September 26, 2016 Bit more information for you all in case anyone can help......... I have ajax-cart.js included in the head of my custom page, and I have a postProcess function in the controller file for that page which receives form data from my custom page. The form data contains product id's (for products already in the database), along with quantities. What I am looking for (without much success) is a solution for programatically adding those products to the shopping cart. We have the ajax cart enabled on the store, and I have tried adding these products to the cart using calls to: ajaxCart.add(...) and also: $Cart = $this->context->cart; $Cart->updateQty(...) Neither method seems to work for me. I am sure I am missing something really simple here, can anyone help please.....? Link to comment Share on other sites More sharing options...
Cyburg Posted September 26, 2016 Author Share Posted September 26, 2016 If I try this from my controller file: $Cart = $this->context->cart; print_r($Cart); I can see the cart object is accessible: Cart Object ( [id] => 12 [id_shop_group] => 1 [id_shop] => 1 [id_address_delivery] => 0 [id_address_invoice] => 0 [id_currency] => 3 [id_customer] => 0 [id_guest] => 131 [id_lang] => 1 [recyclable] => 0 [gift] => 0 [gift_message] => [mobile_theme] => 0 [date_add] => 2016-09-26 10:32:35 [secure_key] => [id_carrier] => 0 [date_upd] => 2016-09-26 16:31:04 [checkedTos] => [pictures] => [textFields] => [delivery_option] => [allow_seperated_package] => 0 [_products:protected] => [_taxCalculationMethod:protected] => 1 [webserviceParameters:protected] => Array ( [fields] => Array ( [id_address_delivery] => Array ( [xlink_resource] => addresses ) [id_address_invoice] => Array ( [xlink_resource] => addresses ) [id_currency] => Array ( [xlink_resource] => currencies ) [id_customer] => Array ( [xlink_resource] => customers ) [id_guest] => Array ( [xlink_resource] => guests ) [id_lang] => Array ( [xlink_resource] => languages ) ) [associations] => Array ( [cart_rows] => Array ( [resource] => cart_row [virtual_entity] => 1 [fields] => Array ( [id_product] => Array ( [required] => 1 [xlink_resource] => products ) [id_product_attribute] => Array ( [required] => 1 [xlink_resource] => combinations ) [id_address_delivery] => Array ( [required] => 1 [xlink_resource] => addresses ) [quantity] => Array ( [required] => 1 ) ) ) ) ) [id_shop_list] => [get_shop_from_context:protected] => 1 [table:protected] => cart [identifier:protected] => id_cart [fieldsRequired:protected] => Array ( [0] => id_currency [1] => id_lang ) [fieldsSize:protected] => Array ( [secure_key] => 32 ) [fieldsValidate:protected] => Array ( [id_shop_group] => isUnsignedId [id_shop] => isUnsignedId [id_address_delivery] => isUnsignedId [id_address_invoice] => isUnsignedId [id_carrier] => isUnsignedId [id_currency] => isUnsignedId [id_customer] => isUnsignedId [id_guest] => isUnsignedId [id_lang] => isUnsignedId [recyclable] => isBool [gift] => isBool [gift_message] => isMessage [mobile_theme] => isBool [allow_seperated_package] => isBool [date_add] => isDate [date_upd] => isDate ) [fieldsRequiredLang:protected] => Array ( ) [fieldsSizeLang:protected] => Array ( ) [fieldsValidateLang:protected] => Array ( ) [tables:protected] => Array ( ) [image_dir:protected] => [image_format:protected] => jpg [def:protected] => Array ( [table] => cart [primary] => id_cart [fields] => Array ( [id_shop_group] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_shop] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_address_delivery] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_address_invoice] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_carrier] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_currency] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_customer] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_guest] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_lang] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [recyclable] => Array ( [type] => 2 [validate] => isBool ) [gift] => Array ( [type] => 2 [validate] => isBool ) [gift_message] => Array ( [type] => 3 [validate] => isMessage ) [mobile_theme] => Array ( [type] => 2 [validate] => isBool ) [delivery_option] => Array ( [type] => 3 ) [secure_key] => Array ( [type] => 3 [size] => 32 ) [allow_seperated_package] => Array ( [type] => 2 [validate] => isBool ) [date_add] => Array ( [type] => 5 [validate] => isDate ) [date_upd] => Array ( [type] => 5 [validate] => isDate ) ) [classname] => Cart ) [update_fields:protected] => [force_id] => ) But no matter what I do, I cannot seem to be able to update the cart with the products I want to push into it........ Link to comment Share on other sites More sharing options...
Cyburg Posted September 27, 2016 Author Share Posted September 27, 2016 $Cart = $this->context->cart; $Cart->updateQty(10, 3, null, false); Thanks for all the help guys....... 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now