Перейти к содержанию
Форум поддержки пользователей VamShop

Получаем содержимое корзины в json формате!


Рекомендуемые сообщения

Для примера, сделаем страницу http://ваш-магазин.ру/ddelivery/cart.json

Которая будет возвращать содержимое корзины посетителя в json формате.

1. Открываем /app/Config/routes.php и правим:

Router::parseExtensions('rss','xml');

на:

Router::parseExtensions('rss','xml','json');

2. Создаём файл DdeliveryController.php в папке /app/Controller/ с таким содержимым:

<?php
/* -----------------------------------------------------------------------------------------
   VamShop - http://vamshop.com
   -----------------------------------------------------------------------------------------
   Copyright (c) 2014 VamSoft Ltd.
   License - http://vamshop.com/license.html
   ---------------------------------------------------------------------------------------*/
class DdeliveryController extends AppController {
	public $name = 'Ddelivery';

	public function cart () {

	App::uses('CurrencyBaseComponent', 'Controller/Component');
	$CurrencyBase = new CurrencyBaseComponent(new ComponentCollection());

	App::uses('ContentBaseComponent', 'Controller/Component');
	$ContentBase = new ContentBaseComponent(new ComponentCollection());

	global $order;
	global $config;
	global $content;

	$order_items = array();

		$total_quantity = null;
		$total_weight = null;

	foreach($order['OrderProduct'] AS $cart_item)
	{
		$content_id = $cart_item['content_id'];
		$total_quantity += $cart_item['quantity'];
		$total_weight += $cart_item['weight']*$cart_item['quantity'];
		$image = array();
		$content_image = $ContentBase->get_content_image($cart_item['content_id']);
		$content_information = $ContentBase->get_content_information($cart_item['content_id']);

		// Content Image
		
		if($content_image != "") {
			$image_url = $content_id . '/' . $content_image . '/40';
			$image_path = BASE . '/img/content/' . $content_image;
			$thumb_name = substr_replace($content_image , '', strrpos($content_image , '.')).'-40.png';	
			$thumb_path = IMAGES . 'content/' . $thumb_name;
			$thumb_url = BASE . '/img/content/' . $thumb_name;

				if(file_exists($thumb_path) && is_file($thumb_path)) {
					list($width, $height, $type, $attr) = getimagesize($thumb_path);
					$image['image'] = true;
					$image['image_original'] =  $image_path;
					$image['image_thumb'] =  $thumb_url;
					$image['image_width'] = $width;
					$image['image_height'] = $height;
				} else {
					$image['image'] = true;
					$image['image_original'] =  $image_path;
					$image['image_thumb'] = BASE . '/images/thumb/' . $image_url;
					$image['image_width'] = null;
					$image['image_height'] = null;
				}

		} else { 

			$image_url = 'noimage.png/40';
			$thumb_name = 'noimage-40.png';	
			$thumb_path = IMAGES . 'content/' . $thumb_name;
			$thumb_url = BASE . '/img/content/' . $thumb_name;

				if(file_exists($thumb_path) && is_file($thumb_path)) {
					list($width, $height, $type, $attr) = getimagesize($thumb_path);
					$image['image'] = true;
					$image['image_thumb'] =  $thumb_url;
					$image['image_width'] = $width;
					$image['image_height'] = $height;
				} else {
					$image['image'] = true;
					$image['image_thumb'] = BASE . '/images/thumb/' . $image_url;
					$image['image_width'] = null;
					$image['image_height'] = null;
				}

		}
		
		$order_items[$content_id] = array(
			'id' => $cart_item['content_id'],
			'link' => BASE . '/product/' . $content_id . $config['URL_EXTENSION'],
			'name' => $cart_item['name'],
			'width' => $cart_item['width'],
			'height' => $cart_item['height'],
			'length' => $cart_item['length'],
			'volume' => $cart_item['volume'],
			'model' => $cart_item['model'],
			'sku' => $cart_item['sku'],
			'weight' => $cart_item['weight'],
			'image' => $image,
			'price' => $CurrencyBase->display_price($cart_item['price']),
			'price_plain' => $cart_item['price'],
			'qty' => $cart_item['quantity'],
			'url' => BASE . '/product/' . $content_information['Content']['alias'] . $config['URL_EXTENSION'],
			'line_total' => $CurrencyBase->display_price($cart_item['quantity']*$cart_item['price'])
		);
	}

		$this->set('products', $order_items);
	}

}
?>

3. Создаём папку /app/View/Ddelivery/json и файл cart.ctp внутри этой папки с таким содержимым:

<?php
if(!$products) return;
$output .= 'var products = ['. "\n";
foreach ($products as $cart_json) {
$output .= '{'. "\n";
$output .= 'id:' . $cart_json['id'] . ',' . "\n";
$output .= 'name:' . $cart_json['name'] . ',' . "\n";
$output .= 'price:' . $cart_json['price_plain'] . ',' . "\n";
$output .= 'width:' . $cart_json['width'] . ',' . "\n";
$output .= 'height:' . $cart_json['height'] . ',' . "\n";
$output .= 'length:' . $cart_json['length'] . ',' . "\n";
$output .= 'weight:' . $cart_json['weight'] . ',' . "\n";
$output .= 'quantity:' . $cart_json['qty'] . ',' . "\n";
$output .= 'sku:' . $cart_json['sku'] . "\n";
$output .= '},'. "\n";
}
$output .= '];'. "\n";

echo $output;

Всё.

Теперь при открытии адреса http://магзаин.ру/ddelivery/cart.json будет выводиться содержимое корзины.

var products = [
{
id:95,
name:Samsung GALAXY Note 3,
price:13999,
width:20,
height:5,
length:30,
weight:0.6,
quantity:1,
sku:
},
];

 

Ссылка на сообщение
Поделиться на другие сайты
×
×
  • Создать...