22
33use \Magento \Framework \App \ObjectManager ;
44
5- require_once VF_SYSTEM_DIR . 'engine/resolver.php ' ;
5+ require_once VF_SYSTEM_DIR . 'engine/resolver.php ' ;
66
77class ResolverStoreCart extends Resolver
88{
99 public function add ($ args )
1010 {
11- $ objectManager =ObjectManager::getInstance ();
11+ $ objectManager = ObjectManager::getInstance ();
1212
13+ /** @var Magento\Catalog\Model\ProductRepository $productRepository */
1314 $ productRepository = $ objectManager ->get ('Magento\Catalog\Model\ProductRepository ' );
15+ /** @var Magento\Checkout\Model\Cart $cart */
1416 $ cart = $ objectManager ->get ('Magento\Checkout\Model\Cart ' );
1517 $ options = array ();
18+ $ links = array ();
1619 $ super_attributes = array ();
1720
1821 foreach ($ args ['options ' ] as $ value ) {
@@ -25,58 +28,68 @@ public function add($args)
2528 });
2629 $ options [str_replace ('option_ ' , '' , $ value ['id ' ])] = $ values ;
2730 }
31+ } elseif ($ value ['id ' ] == 'links ' ) {
32+ $ values = array_filter (explode ('| ' , $ value ['value ' ]), function ($ value ) {
33+ return $ value !== '' ;
34+ });
35+ $ links = $ values ;
2836 } else {
2937 $ super_attributes [$ value ['id ' ]] = $ value ['value ' ];
3038 }
3139 }
3240
33-
3441 $ params = array (
35- 'product ' => $ args ['id ' ],
36- 'qty ' => $ args ['quantity ' ],
37- 'options ' => $ options ,
38- 'super_attribute ' => $ super_attributes
39- );
42+ 'product ' => $ args ['id ' ],
43+ 'qty ' => $ args ['quantity ' ],
44+ 'options ' => $ options ,
45+ 'super_attribute ' => $ super_attributes ,
46+ 'links ' => $ links
47+ );
4048 $ product_info = $ productRepository ->getById ($ args ['id ' ]);
4149
4250 try {
4351 $ cart ->addProduct ($ product_info , $ params );
4452 } catch (Exception $ e ) {
45- echo '<pre> ' ;
46- print_r ($ e ->getMessage ());
47- echo '</pre> ' ;
53+ throw new Exception ($ e ->getMessage ());
4854 }
4955
5056 $ cart ->save ();
5157
5258 return $ this ->get ($ args );
5359 }
60+
5461 public function update ($ args )
5562 {
5663 $ objectManager = ObjectManager::getInstance ();
64+ /** @var Magento\Checkout\Model\Cart $cart */
5765 $ cart = $ objectManager ->get ('Magento\Checkout\Model\Cart ' );
58-
66+
5967 $ item = $ cart ->getQuote ()->getItemById ($ args ['key ' ]);
6068 $ item ->setQty ($ args ['quantity ' ]);
6169 $ cart ->save ();
6270
6371 return $ this ->get ($ args );
6472 }
73+
6574 public function remove ($ args )
6675 {
67- $ objectManager =ObjectManager::getInstance ();
68-
76+ $ objectManager = ObjectManager::getInstance ();
77+ /** @var Magento\Checkout\Model\Cart $modelCart */
6978 $ modelCart = $ objectManager ->get ('Magento\Checkout\Model\Cart ' );
7079
7180 $ modelCart ->removeItem ($ args ['key ' ])->save ();
7281
7382 return $ this ->get ($ args );
7483 }
84+
7585 public function get ($ args )
7686 {
77- $ objectManager =ObjectManager::getInstance ();
87+ $ objectManager = ObjectManager::getInstance ();
7888
89+ /** @var Magento\Checkout\Model\Cart $modelCart */
7990 $ modelCart = $ objectManager ->get ('Magento\Checkout\Model\Cart ' );
91+ /** @var Magento\Downloadable\Api\LinkRepositoryInterface $linkRepository */
92+ $ linkRepository = $ objectManager ->get ('Magento\Downloadable\Api\LinkRepositoryInterface ' );
8093
8194 $ modelCart ->getQuote ()->collectTotals ();
8295 $ cart = array (
@@ -87,18 +100,35 @@ public function get($args)
87100 $ results = $ modelCart ->getItems ();
88101
89102 foreach ($ results as $ value ) {
103+ /** @var Magento\Catalog\Model\Product $product */
104+ $ product = $ value ->getProduct ();
90105 if (!$ value ->isDeleted () && !$ value ->getParentItemId () && !$ value ->getParentItem ()) {
91106 $ options = array ();
92-
93- $ result_options = $ value ->getProduct ()->getTypeInstance (true )->getOrderOptions ($ value ->getProduct ());
94107
108+ $ result_options = $ product ->getTypeInstance (true )->getOrderOptions ($ product );
109+ if (!empty ($ result_options ['links ' ])) {
110+ $ values = array ();
111+ $ productLinks = $ linkRepository ->getLinksByProduct ($ product );
112+
113+ foreach ($ productLinks as $ link ) {
114+ if (in_array ($ link ->getId (), $ result_options ['links ' ])) {
115+ $ values [] = $ link ->getTitle ();
116+ }
117+ }
118+
119+ $ options [] = array (
120+ 'name ' => $ value ->getProduct ()->getData ('links_title ' ),
121+ 'type ' => 'checkbox ' ,
122+ 'value ' => implode (', ' , $ values )
123+ );
124+ }
95125 if (!empty ($ result_options ['attributes_info ' ])) {
96126 foreach ($ result_options ['attributes_info ' ] as $ option ) {
97127 $ options [] = array (
98- 'name ' => $ option ['label ' ],
99- 'type ' => 'radio ' ,
100- 'value ' => $ option ['value ' ]
101- );
128+ 'name ' => $ option ['label ' ],
129+ 'type ' => 'radio ' ,
130+ 'value ' => $ option ['value ' ]
131+ );
102132 }
103133 }
104134 if (!empty ($ result_options ['options ' ])) {
@@ -135,11 +165,11 @@ public function get($args)
135165 }
136166
137167 $ cart ['products ' ][] = array (
138- 'key ' => $ value ->getId (),
139- 'product ' => $ this ->load ->resolver ('store/product/get ' , array ( 'product ' => $ value ->getProduct () )),
168+ 'key ' => $ value ->getId (),
169+ 'product ' => $ this ->load ->resolver ('store/product/get ' , array ('product ' => $ value ->getProduct ())),
140170 'quantity ' => $ value ->getQty (),
141- 'option ' => $ options ,
142- 'total ' => $ this ->currency ->format ($ value ->getPrice () * $ value ->getQty ())
171+ 'option ' => $ options ,
172+ 'total ' => $ this ->currency ->format ($ value ->getPrice () * $ value ->getQty ())
143173 );
144174 }
145175 }
0 commit comments