Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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