Created
February 18, 2016 14:58
-
-
Save humbertoromanojr/df633f362872cbd37970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use Cart\Model\Table\Cart; | |
use Cake\Network\Exception\NotFoundException; | |
use correioXml\Correios\Configure; | |
class CartController extends AppController { | |
public function index() | |
{ | |
$cart = Cart::factory(); | |
$products = $cart->all(); | |
//------- calculando o frete | |
$freight = 0; | |
if ($this->request->is('post')) { | |
$configure = new Configure; | |
$configure->sCepOrigem = 11676085; | |
$configure->sCepDestino = $this->request->data['cep']; | |
$configure->setService($this->request->data['service']); | |
$configure->nVlPeso = $cart->wheight(); | |
$configure->nVlComprimento = 30; | |
$configure->nAltura = 30; | |
$configure->nVlLargura = 30; | |
$configure->nVlDiametro = 30; | |
$correios = new Correios($configure); | |
$preco_prazo = $correios->calcPrecoPrazo(); | |
if ($preco_prazo->Erro == 0) { | |
$freight = (float)$preco_prazo->Valor->__toString(); | |
} else { | |
$this->Flash->error($preco_prazo->MsgErro); | |
} | |
} | |
$this->set(['cart'=>$products, 'freight'=>$freight]); | |
} | |
public function add($id=null) | |
{ | |
$this->loadModel('Products'); | |
$product = $this->Products->find('all',[ | |
'conditions'=>[ | |
'Products.id'=>$id | |
] | |
])->first(); | |
if (empty($product)) { | |
throw new NotFoundException("Artefact not found - Artefato não encontrado"); | |
} | |
$product->qtd = 1; | |
$cart = Cart::factory(); | |
$cart->add($product->toArray()); | |
return $this->redirect(['action'=>'index']); | |
} | |
public function delete($id=null) | |
{ | |
$this->loadModel('Products'); | |
$product = $this->Products->find('all',[ | |
'conditions'=>[ | |
'Products.id'=>$id | |
] | |
])->first(); | |
if (empty($product)) { | |
throw new NotFoundException("Artefact not found - Artefato não encontrado"); | |
} | |
$product->qtd = 1; | |
$cart = Cart::factory(); | |
$cart->delete($id); | |
return $this->redirect(['action'=>'index']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment