Jump to content

Customized product add to cart function


p3le

Recommended Posts

Hello there, I have problem with custom fuction creating cart and adding customizable product. This is code I have so far:

public function addProductToCart($data = [])
{		
	$context = \Context::getContext();
	$cart = null;
	$errors = [];
		
	if(!isset($data['id']))
		return ["status" => "error", "message" => "Missing required arguments."];	
		
	if($context -> cookie -> id_cart)
		$cart = new \Cart((int)$context -> cookie -> id_cart);
		
	if(!$cart -> id) 
	{
		$cart = new \Cart();
		$cart -> id_customer = (int)$context -> cookie -> id_customer;
		$cart -> id_guest = (int)$context -> cookie -> id_guest;
		$cart->id_address_delivery = (int)  (\Address::getFirstCustomerAddressId($cart->id_customer));
		$cart->id_address_invoice = $cart->id_address_delivery;
		$cart->id_lang = (int)($context->cookie->id_lang);
		$cart->id_currency = (int)($context->cookie->id_currency);
		$cart->id_carrier = 1;
		$cart->recyclable = 0;
		$cart->gift = 0;
		$cart->add();
		$context-> cookie -> __set('id_cart', (int)$cart -> id);
		$cart->update();
	}
									
	if($cart -> id)
	{
		$product = new \Product((int)$data['id']);
		$customization_id = false;
		
		if(!$product -> id)
			return ["status" => "error", "message" => "Cannot find data in database."];
		
	        $customFields = $product -> getCustomizationFieldIds();
			
		/* if product has custom fields */
		if(!empty($customFields))
		{
			foreach($customFields as $field)
			{
				$label = \Customization::getLabel($field['id_customization_field'], $context -> cookie -> id_lang);
				if($field['required'] == 1 && !isset($data['custom_fields'][\Tools::str2url($label)]))
				{
					$errors[] = ["status" => "error", "message" => "No custom field ". \Tools::str2url($label) ." provided."];
					continue;
				}
				
				switch($field['type'])
				{
					case \Product::CUSTOMIZE_FILE:
					$cart -> addPictureToProduct($product -> id, $field['id_customization_field'], $field['type'], $data['custom_fields'][\Tools::str2url($label)]);
					break;
					
					case \Product::CUSTOMIZE_TEXTFIELD:
					$cart -> addTextFieldToProduct($product -> id, $field['id_customization_field'], $field['type'], $data['custom_fields'][\Tools::str2url($label)]);
					break;
				}
				
			}
				
			if(count($errors) > 0)
				return $errors;
				
			/* if there was any customization get the ID */
			$customization = $cart -> getProductCustomization((int)$product -> id);
			$customization_id = is_array($customization) && isset($customization[0]['id_customization'])? (int)$customization[0]['id_customization'] : false;
		}
			
		$cart -> updateQty(1, (int)$product -> id, null, $customization_id);
		$cart -> update();	
			
		return ["status" => "success", "message" => "Product has been added"];
	} else 
	{
		return ["status" => "error", "message" => "Unable to create cart"];
	}
}

This function is created outside presta, so in begining of the file I'm importing config.inc.php and init.php. Function adds product to cart, however updateQty method doesn't change ps_customization.in_cart to 1. If I manually change it to 1, my product will display as customized with my custom data provided by script (this is perfect). Second thing when I try to remove this product from cart (in GUI cart), presta couldn't delete row from ps_customization (I don't know why) but only from ps_customization_data and I'm still able to see this product and unable to delete it unless I remove ps_customization row from db manually. Does anybody know how to fix this issues? I don't know presta too much...

Link to comment
Share on other sites

  • 4 months later...

About Prestashop 1.6. I want to do the same effect: with one click in Button save the customization product data (I only have only two textarea), add to cart the product and go to next stape, order-opc controller, who's control de order, where the user select the payments, direction address, etc. I do not want show any fancybox.

About your second question, I think, the product quantity in your backoffce, in Product Menú, is 1 or great; if you put 0, the user can remove the producto from the cart.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

I am having a different problem with 1.6.1 - The add to cart button is visable again on the product page and I only want it on the customized product page after the customer adds information to the two text fields. We had this fixed in the 1.5 version by changing code in the product-list.tpl . That modified code still exists in my product-list.tpl but the add to cart button is still visable. Please help!

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...