Jump to content

create override for cartcontroller.php


criacaosites

Recommended Posts

i try create a script that check a xml before add product in the cart.

<?php
class CartController extends CartControllerCore
{


	/**
	 * This process add or update a product in the cart
	 */
protected function processChangeProductInCart(){
		
		 parent::initContent();
		 
		require (_PS_MODULE_DIR_.'/'.$this->module_name.'/controllers/admin/data.php');
		require (_PS_MODULE_DIR_.'/'.$this->module_name.'/controllers/admin/data_v4.php');
		require (_PS_MODULE_DIR_.'/'.$this->module_name.'/controllers/admin/language.php');
		require (_PS_MODULE_DIR_.'/'.$this->module_name.'/controllers/admin/all.php');     

		$reference = Db::getInstance()->getValue('SELECT reference FROM '._DB_PREFIX_.'product WHERE id_product='.$product->id);
		$parameters=array(
                                               'Operation'       =>'ItemLookup'              ,
                                              'ItemId'        =>urlencode($reference)      ,
                                             'ResponseGroup'   =>'Medium'                ,
  )

		$A=aws_request($parameters);  

		foreach($A->Items->Item as $item){
		$info[$i]['valor'] = $item->ItemAttributes-> ListPrice->Amount;	
		$margemlucro = '1.'.Configuration::get('modulemarge');
		$info[$i]['precofinal'] = $info[$i]['valor']*$margemlucro/100;		
		$info[$i]['total'] = $item->OfferSummary->TotalNew;
		}

		if(( $info[$i]['total'] > 0) AND
			($product['price'] >= $info[$i]['precofinal'])) 
		{			
			return true;
		}else{
			if(($item->erro == TRUE) OR (empty($item)))
				return true;
				
				$produto= array('active' => 0);
	        	$execute = Db::getInstance()->update('product',$produto, 'reference' = $reference, $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true);
				
				$this->errors[] = Tools::displayError('This product is no longer available, because we dont have more on stock, if you wish them, send us a email that we team will check and back with you soon possible.', false);
			return;
						}
		
				}
	}


But is don't work. How i can check if this script is running on the website?

  • Like 1
Link to comment
Share on other sites

  • 6 months later...
  • 2 months later...

Hi criacaosites,

 

Did you find any solution for this cartcontroller override

 

I need to check the quantity before added in to the add to cart.

 

Tank you for fast reply, i did create the override file for replace the function processChangeProductInCart(), on part of 

"if (!$this->errors)" i add the verification "

class CartControllerCore extends FrontController {
	/**
	 * This process add or update a product in the cart
	 */
	protected function processChangeProductInCart()
	{
		$mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';

		if ($this->qty == 0)
			$this->errors[] = Tools::displayError('Null quantity.', !Tools::getValue('ajax'));
		elseif (!$this->id_product)
			$this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));

		$product = new Product($this->id_product, true, $this->context->language->id);
		if (!$product->id || !$product->active)
		{
			$this->errors[] = Tools::displayError('This product is no longer available.', !Tools::getValue('ajax'));
			return;
		}

		$qty_to_check = $this->qty;
		$cart_products = $this->context->cart->getProducts();

		if (is_array($cart_products))
			foreach ($cart_products as $cart_product)
			{
				if ((!isset($this->id_product_attribute) || $cart_product['id_product_attribute'] == $this->id_product_attribute) &&
					(isset($this->id_product) && $cart_product['id_product'] == $this->id_product))
				{
					$qty_to_check = $cart_product['cart_quantity'];

					if (Tools::getValue('op', 'up') == 'down')
						$qty_to_check -= $this->qty;
					else
						$qty_to_check += $this->qty;

					break;
				}
			}

		// Check product quantity availability
		if ($this->id_product_attribute)
		{
			if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $qty_to_check))
				$this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
		}
		elseif ($product->hasAttributes())
		{
			$minimumQuantity = ($product->out_of_stock == 2) ? !Configuration::get('PS_ORDER_OUT_OF_STOCK') : !$product->out_of_stock;
			$this->id_product_attribute = Product::getDefaultAttribute($product->id, $minimumQuantity);
			// @todo do something better than a redirect admin !!
			if (!$this->id_product_attribute)
				Tools::redirectAdmin($this->context->link->getProductLink($product));
			elseif (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $qty_to_check))
				$this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
		}
		elseif (!$product->checkQty($qty_to_check))
			$this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));

		// If no errors, process product addition
		if (!$this->errors && $mode == 'add')
		{
			// Add cart if no cart found
			if (!$this->context->cart->id)
			{
				if (Context::getContext()->cookie->id_guest)
				{
					$guest = new Guest(Context::getContext()->cookie->id_guest);
					$this->context->cart->mobile_theme = $guest->mobile_theme;
				}
				$this->context->cart->add();
				if ($this->context->cart->id)
					$this->context->cookie->id_cart = (int)$this->context->cart->id;
			}

			// Check customizable fields
			if (!$product->hasAllRequiredCustomizableFields() && !$this->customization_id)
				$this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax'));

			if (!$this->errors)
			{
				$cart_rules = $this->context->cart->getCartRules();
				$update_quantity = $this->context->cart->updateQty($this->qty, $this->id_product, $this->id_product_attribute, $this->customization_id, Tools::getValue('op', 'up'), $this->id_address_delivery);
				if ($update_quantity < 0)
				{
					// If product has attribute, minimal quantity is set with minimal quantity of attribute
					$minimal_quantity = ($this->id_product_attribute) ? Attribute::getAttributeMinimalQty($this->id_product_attribute) : $product->minimal_quantity;
					$this->errors[] = sprintf(Tools::displayError('You must add %d minimum quantity', !Tools::getValue('ajax')), $minimal_quantity);
				}
				elseif ($update_quantity == "amazon")
					$this->errors[] = Tools::displayError('We check the stock please reload the page.', !Tools::getValue('ajax'));
				elseif (!$update_quantity)
					$this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', !Tools::getValue('ajax'));
				elseif ((int)Tools::getValue('allow_refresh'))
				{
					// If the cart rules has changed, we need to refresh the whole cart
					$cart_rules2 = $this->context->cart->getCartRules();
					if (count($cart_rules2) != count($cart_rules))
						$this->ajax_refresh = true;
					else
					{
						$rule_list = array();
						foreach ($cart_rules2 as $rule)
							$rule_list[] = $rule['id_cart_rule'];
						foreach ($cart_rules as $rule)
							if (!in_array($rule['id_cart_rule'], $rule_list))
							{
								$this->ajax_refresh = true;
								break;
							}
					}
				}
			}
		}

		$removed = CartRule::autoRemoveFromCart();
		CartRule::autoAddToCart();
		if (count($removed) && (int)Tools::getValue('allow_refresh'))
			$this->ajax_refresh = true;
	}

}

But my shopping cart don't add or delete product from sumary

Edited by criacaosites (see edit history)
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...