Related Items (MS2)

Hi there.
How would I go about adding related items in checkout?

eg — if a user is buys a tablet, and a phone, then on (or near) checkout, how can I show accessories for each item?

Thank you
Joseph Mureithi
03 июля 2013, 10:49
modx.pro
1
2 472
0

Комментарии: 6

Joseph Mureithi
03 июля 2013, 18:47
0
Anyone...?
    Joseph Mureithi
    04 июля 2013, 12:48
    0
    Can anyone please advise me on this? I've tried searching the site, but I get no results… I would appreciate any help
      Василий Наумкин
      04 июля 2013, 14:33
      0
      It complicate to explain it on english, but i will try.

      1. You need create new link with type «one to many»
      2. Then you need to link your products (tablets and phones) with other products (accessories). See tab «links» in product management.
      3. Now you can add accessories on products page by snippet msProducts (see params link and master)
      4. But you want to add this in the checkout, so you need to make you own snippet, that will look into MS2 cart and show related items.

      Here is example:
      <?php
      $miniShop2 = $modx->getService('minishop2');
      $miniShop2->initialize($modx->context->key);
      
      // Get cart
      $cart = $miniShop2->cart->get();
      // Iterating through ordered products
      foreach ($cart as $v) {
      	// Get product links
      	$links = $modx->getCollection('msProductLink', array('master' => $v['id']));
      	foreach ($links as $link) {
      		// Here is your related product
      		if ($product = $link->getOne('Slave')) {
      			print_r($product->toArray());
      		}
      	}
      }
        Joseph Mureithi
        09 июля 2013, 15:32
        0
        Hi Vasily

        I've tried following the instructions, but my coding skills are lacking.

        I updated backup copy of my msCart snippet with your code (see below) but all I get on the cart now is an array (please see after the block of code)

        Where did I go wrong?

        <?php
        /* @var miniShop2 $miniShop2 */
        $miniShop2 = $modx->getService('minishop2');
        $miniShop2->initialize($modx->context->key);
        /* @var pdoFetch $pdoFetch */
        $pdoFetch = $modx->getService('pdofetch','pdoFetch', MODX_CORE_PATH.'components/pdotools/model/pdotools/',$scriptProperties);
        $pdoFetch->setConfig($scriptProperties);
        $pdoFetch->addTime('pdoTools loaded.');
        
        $cart = $miniShop2->cart->get();
        if (!empty($_GET['msorder'])) {
        	return '';
        	foreach ($cart as $v) {
        		// Get product links
        		$links = $modx->getCollection('msProductLink', array('master' => $v['id']));
        		foreach ($links as $link) {
        			// Here is your related product
        			if ($product = $link->getOne('Slave')) {
        				print_r($product->toArray());
        			}
        		}
        	}
        }
        else if (empty($cart)) {
        	return !empty($tplEmpty) ? $pdoFetch->getChunk($tplEmpty) : '';
        }
        
        // Include Thumbnails
        $thumbsLeftJoin = '';
        $thumbsSelect = array();
        if (!empty($includeThumbs)) {
        	$thumbs = array_map('trim',explode(',',$includeThumbs));
        	if(!empty($thumbs[0])){
        		foreach ($thumbs as $thumb) {
        			$thumbsLeftJoin .= ',{"class":"msProductFile","alias":"'.$thumb.'","on":"'.$thumb.'.product_id = msProduct.id AND '.$thumb.'.parent != 0 AND '.$thumb.'.path LIKE \'%/'.$thumb.'/\'"}';
        			$thumbsSelect[] = ' "'.$thumb.'":"'.$thumb.'.url as '.$thumb.'" ';
        		}
        		$pdoFetch->addTime('Included list of thumbnails: <b>'.implode(', ',$thumbs).'</b>.');
        	}
        }
        // End of including Thumbnails
        
        // Fields to select
        $resourceColumns = !empty($includeContent) ?  $modx->getSelectColumns('msProduct', 'msProduct') : $modx->getSelectColumns('msProduct', 'msProduct', '', array('content'), true);
        $dataColumns = $modx->getSelectColumns('msProductData', 'Data', '', array('id'), true);
        $vendorColumns = $modx->getSelectColumns('msVendor', 'Vendor', 'vendor.', array('id'), true);
        
        // Tables for joining
        $leftJoin = '{"class":"msProductData","alias":"Data","on":"msProduct.id=Data.id"},{"class":"msVendor","alias":"Vendor","on":"Data.vendor=Vendor.id"}';
        if (!empty($tvsLeftJoin)) {$leftJoin .= $tvsLeftJoin;}
        if (!empty($thumbsLeftJoin)) {$leftJoin .= $thumbsLeftJoin;}
        $select = '"msProduct":"'.$resourceColumns.'","Data":"'.$dataColumns.'","Vendor":"'.$vendorColumns.'"';
        if (!empty($thumbsSelect)) {$select .= ','.implode(',', $thumbsSelect);}
        $pdoFetch->addTime('Query parameters are prepared.');
        
        // Initializing chunk for template rows
        if (!empty($tplRow)) {$pdoFetch->getChunk($tplRow);}
        
        // Working
        $outer = array('goods' => '', 'total_count' => 0, 'total_weight' => 0, 'total_cost' => 0);
        foreach ($cart as $k => $v) {
        
        	$default = array(
        		'class' => 'msProduct'
        		,'where' => '{"msProduct.id":"'.$v['id'].'","class_key":"msProduct"}'
        		,'leftJoin' => '['.$leftJoin.']'
        		,'select' => '{'.$select.'}'
        		,'sortby' => 'id'
        		,'sortdir' => 'ASC'
        		,'groupby' => 'msProduct.id'
        		,'fastMode' => false
        		,'limit' => 0
        		,'return' => 'data'
        		,'nestedChunkPrefix' => 'minishop2_'
        	);
        	// Merge all properties and run!
        	$pdoFetch->config = array_merge($pdoFetch->config, $default, $scriptProperties);
        	$rows = $pdoFetch->run();
        
        	if (!empty($rows[0])) {
        		$row = $rows[0];
        		$row['key'] = $k;
        		$row['count'] = $v['count'];
        		$row['price'] = $miniShop2->formatPrice($v['price']);
        		$row['weight'] = $miniShop2->formatWeight($v['weight']);
        		$row['cost'] = $v['count'] * $v['price'];
        
        		// Additional properties of product
        		if (!empty($v['options']) && is_array($v['options'])) {
        			foreach ($v['options'] as $key => $value) {
        				$row['option.'.$key] = $value;
        			}
        		}
        		unset($v['options']);
        
        		$outer['goods'] .= !empty($tplRow) ? $pdoFetch->getChunk($tplRow, $row) : str_replace(array('[[',']]'),array('&091;&091;','&093;&093;'), print_r($row,1));
        		$outer['total_count'] += $v['count'];
        		$outer['total_cost'] +=  $v['count'] * $v['price'];
        		$outer['total_weight'] += $v['count'] * $v['weight'];
        	}
        }
        
        $outer['total_cost'] = $miniShop2->formatPrice($outer['total_cost']);
        $outer['total_weight'] = $miniShop2->formatWeight($outer['total_weight']);
        
        return !empty($tplOuter) ? $pdoFetch->getChunk($tplOuter, $outer) : str_replace(array('[[',']]'),array('&091;&091;','&093;&093;'), print_r($outer,1));
        The output:

        Array ( [goods] => Array ( [id] => 8 [type] => document [contentType] => text/html [pagetitle] => 9.7" Select Tablet [longtitle] => Lava 9.7" Select Tablet [description] => [alias] => lava-9.7-select-tablet [link_attributes] => [published] => 1 [pub_date] => 0 [unpub_date] => 0 [parent] => 3 [isfolder] => 0 [introtext] => [richtext] => 1 [template] => 4 [menuindex] => 3 [searchable] => 1 [cacheable] => 1 [createdby] => 1 [createdon] => 1365040697 [editedby] => 1 [editedon] => 1372927504 [deleted] => 0 [deletedon] => 0 [deletedby] => 0 [publishedon] => 1372927504 [publishedby] => 1 [menutitle] => [donthit] => 0 [privateweb] => 0 [privatemgr] => 0 [content_dispo] => 0 [hidemenu] => 0 [class_key] => msProduct [context_key] => web [content_type] => 1 [uri] => lava-9.7-select-tablet.html [uri_override] => 1 [hide_children_in_tree] => 0 [show_in_tree] => 0 [properties] => [article] => R9-116S-CH [price] => 3 250.00 [old_price] => 0.00 [weight] => 0.57 [image] => /lava/assets/images/products/8/cd264e10acda36036233db4263b6ddbf.jpg [thumb] => /lava/assets/images/products/8/120x90/cd264e10acda36036233db4263b6ddbf.jpg [vendor] => 0 [made_in] => [new] => 0 [popular] => 0 [favorite] => 0 [tags] => [""] [color] => [«Black»,«White»] [size] => [""] [source] => 2 [vendor.name] => [vendor.resource] => [vendor.country] => [vendor.logo] => [vendor.address] => [vendor.phone] => [vendor.fax] => [vendor.email] => [vendor.description] => [vendor.properties] => [key] => 10e44478457d8ac76e238f8d0eff8aeb [count] => 1 [cost] => 3250 [option.color] => Black ) [total_count] => 1 [total_weight] => 0.57 [total_cost] => 3 250.00 )
      Joseph Mureithi
      10 июля 2013, 03:59
      0
      Any advice on this? anyone?
      Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
      6