From f392045659b476d783f61f16f9375e35cf48429c Mon Sep 17 00:00:00 2001 From: Dmitry Fedyuk Date: Tue, 21 Jan 2020 08:37:51 +0600 Subject: [PATCH] 1.2.0: https://github.com/justuno-com/m2/issues/7 --- Controller/Cart/Add.php | 36 +++++++++++++++++++++++++++++++++++- composer.json | 2 +- etc/frontend/sections.xml | 9 +++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 etc/frontend/sections.xml diff --git a/Controller/Cart/Add.php b/Controller/Cart/Add.php index bb1299e..a6cfab2 100644 --- a/Controller/Cart/Add.php +++ b/Controller/Cart/Add.php @@ -2,12 +2,16 @@ 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(): @@ -15,5 +19,35 @@ class Add extends _P { * 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);} } \ No newline at end of file diff --git a/composer.json b/composer.json index dbc27fe..a737977 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/etc/frontend/sections.xml b/etc/frontend/sections.xml new file mode 100644 index 0000000..037778d --- /dev/null +++ b/etc/frontend/sections.xml @@ -0,0 +1,9 @@ + + + +
+ +