Skip to content

Commit

Permalink
1.2.0: #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Jan 21, 2020
1 parent 114c8a2 commit f392045
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
36 changes: 35 additions & 1 deletion Controller/Cart/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,52 @@
namespace Justuno\M2\Controller\Cart;
use Df\Framework\W\Result\Json;
use Justuno\M2\Response as R;
use Magento\Catalog\Model\Product as P;
use Magento\Framework\App\Action\Action as _P;
// 2020-01-21
/** @final Unable to use the PHP «final» keyword here because of the M2 code generation. */
class Add extends _P {
/**
* 2020-01-21
* "Implement the «add a configurable product to the cart» endpoint": https://github.com/justuno-com/m2/issues/7
* @see \Magento\Checkout\Controller\Cart\Add::execute()
* https://github.com/magento/magento2/blob/2.3.3/app/code/Magento/Checkout/Controller/Cart/Add.php#L77-L178
* @override
* @see _P::execute()
* @used-by \Magento\Framework\App\Action\Action::dispatch():
* $result = $this->execute();
* https://github.com/magento/magento2/blob/2.2.1/lib/internal/Magento/Framework/App/Action/Action.php#L84-L125
* @return Json
*/
function execute() {return R::p(function() {});}
function execute() {return R::p(function() {
/**
* 2020-01-21
* @see \Magento\Checkout\Controller\Cart\Add::_initProduct()
* https://github.com/magento/magento2/blob/2.3.3/app/code/Magento/Checkout/Controller/Cart/Add.php#L56-L75
*/
$p = self::product('product'); /** @var P $p */
$params = ['product' => $p->getId(), 'qty' => df_nat(df_request('qty', 1))];
if (df_configurable($p)) {
$ch = self::product('variant'); /** @var P $ch */
$sa = []; /** @var array(int => int) $sa */
foreach ($p->getTypeInstance(true)->getConfigurableAttributesAsArray($p) as $a) {
/** @var array(string => mixed) $a */
$sa[(int)$a['attribute_id']] = $ch[$a['attribute_code']];
}
$params['super_attribute'] = $sa;
}
df_cart()->addProduct($p, $params);
df_cart()->save();
df_dispatch('checkout_cart_add_product_complete', [
'product' => $p, 'request' => $this->getRequest(), 'response' => $this->getResponse()
]);
});}

/**
* 2020-01-21
* @used-by execute()
* @param string $k
* @return P
*/
private static function product($k) {return df_product(df_nat(df_request($k)), true);}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "justuno.com/m2"
,"version": "1.1.8"
,"version": "1.2.0"
,"description": "Justuno module for Magento 2"
,"type": "magento2-module"
,"homepage": "https://github.com/justuno-com/m2"
Expand Down
9 changes: 9 additions & 0 deletions etc/frontend/sections.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0'?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='urn:magento:module:Magento_Customer:etc/sections.xsd'
>
<action name='justuno/cart/add'>
<section name='cart'/>
</action>
</config>

0 comments on commit f392045

Please sign in to comment.