This module provides support to add products to cart via GraphQl. For this to be available for guests, it is needed to force quote creation, which is done upon first checkout session creation.
-
Install it into your Magento 2 project with composer:
composer require integer-net/magento2-addtocartgraphql
-
Enable module
bin/magento setup:upgrade
For your reference, there is also an add to cart snippet included.
Please mind: if you plan to use this for configurable products in the Hyvä theme, you need to pass the product selection to the addtocart component.
The easiest way is to add a method to Magento_ConfigurableProduct/templates/product/view/type/options/js/configurable-options.phtml
updateCurrentSelection() {
window.dispatchEvent(
new CustomEvent(
"update-currentSelection-" + this.productId,
{
detail: this.selectedValues
}
)
);
},
This should be called whenever a selection is made, in Hyvä default you would prepend its call to the method
changeOption(optionId, value) {
[...]
this.updateCurrentSelection()
}
The default qty on init of add to cart button is set to 1 - ideally, you would rather change Magento_Catalog/templates/product/view/quantity.phtml
to dispatch updateChangeQty
on init, as addtocart component already listens to this event - this way, you would always get the correct default qty.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
./vendor/bin/phpunit tests/unit
-
Configure test database in
dev/tests/integration/etc/install-config-mysql.php
. Read more in the Magento docs. -
Copy
tests/integration/phpunit.xml.dist
from the package todev/tests/integration/phpunit.xml
in your Magento installation. -
In that directory, run
../../../vendor/bin/phpunit
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.