Skip to content

Instantly share code, notes, and snippets.

@erikfig
Forked from humbertoromanojr/gist:df633f362872cbd37970
Last active February 18, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikfig/29721d8a3cba370d2443 to your computer and use it in GitHub Desktop.
Save erikfig/29721d8a3cba370d2443 to your computer and use it in GitHub Desktop.
<?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 \correioXml\Correios\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