Allow only one item into cart in magento2Set Custom Price to Quote item magento2 from controller
Vocabulary for giving just numbers, not a full answer
What are actual Tesla M60 models used by AWS?
What are some noteworthy "mic-drop" moments in math?
Motivation for Zeta Function of an Algebraic Variety
When traveling to Europe from North America, do I need to purchase a different power strip?
How can I ensure my trip to the UK will not have to be cancelled because of Brexit?
Does the nature of the Apocalypse in The Umbrella Academy change from the first to the last episode?
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Is it possible to avoid unpacking when merging Association?
Can Mathematica be used to create an Artistic 3D extrusion from a 2D image and wrap a line pattern around it?
NASA's RS-25 Engines shut down time
Intuition behind counterexample of Euler's sum of powers conjecture
weren't playing vs didn't play
They call me Inspector Morse
Reverse string, can I make it faster?
Does "Until when" sound natural for native speakers?
PTIJ: wiping amalek’s memory?
Should I tell my boss the work he did was worthless
Latex does not go to next line
Reversed Sudoku
Plausibility of Mushroom Buildings
How did Alan Turing break the enigma code using the hint given by the lady in the bar?
Can one live in the U.S. and not use a credit card?
How is the wildcard * interpreted as a command?
Allow only one item into cart in magento2
Set Custom Price to Quote item magento2 from controller
I have created a custom controller in my module and calling via ajax to add the item to cart.
Below is my controller code.
<?php
namespace {Vendor}{Module}ControllerProduct;
class AddProduct extends MagentoFrameworkAppActionAction
{
protected $formKey;
protected $cart;
protected $product;
protected $checkoutSession;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoCheckoutModelSession $checkoutSession,
array $data = []) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->checkoutSession = $checkoutSession;
parent::__construct($context);
}
public function execute()
{
$productId = 1;
$customPrice = 150;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productId, //product Id
'qty' => 1 //quantity of product
);
$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
//Enable super mode on the product.
$productItem->getProduct()->setIsSuperMode(true);
$this->cart->save();
echo "success";
}
public function getProductQuote($product)
{
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;
}
}
The above code adds the product of qty 1 to the cart with a custom price whenever it is called.
I want to add only 1 qty of product to the cart. suppose if its called two or three times, only one qty should be present in cart. Need to remove previously added qty from the cart.
Is something can be done programmatically? There is a product setting in backend "Maximum qty allowed in cart", we can set this value but I am looking for the code how it can be done programmatically.
Can anyone look into it and help me, please. Thanks
magento2 product addtocart controllers
add a comment |
I have created a custom controller in my module and calling via ajax to add the item to cart.
Below is my controller code.
<?php
namespace {Vendor}{Module}ControllerProduct;
class AddProduct extends MagentoFrameworkAppActionAction
{
protected $formKey;
protected $cart;
protected $product;
protected $checkoutSession;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoCheckoutModelSession $checkoutSession,
array $data = []) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->checkoutSession = $checkoutSession;
parent::__construct($context);
}
public function execute()
{
$productId = 1;
$customPrice = 150;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productId, //product Id
'qty' => 1 //quantity of product
);
$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
//Enable super mode on the product.
$productItem->getProduct()->setIsSuperMode(true);
$this->cart->save();
echo "success";
}
public function getProductQuote($product)
{
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;
}
}
The above code adds the product of qty 1 to the cart with a custom price whenever it is called.
I want to add only 1 qty of product to the cart. suppose if its called two or three times, only one qty should be present in cart. Need to remove previously added qty from the cart.
Is something can be done programmatically? There is a product setting in backend "Maximum qty allowed in cart", we can set this value but I am looking for the code how it can be done programmatically.
Can anyone look into it and help me, please. Thanks
magento2 product addtocart controllers
add a comment |
I have created a custom controller in my module and calling via ajax to add the item to cart.
Below is my controller code.
<?php
namespace {Vendor}{Module}ControllerProduct;
class AddProduct extends MagentoFrameworkAppActionAction
{
protected $formKey;
protected $cart;
protected $product;
protected $checkoutSession;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoCheckoutModelSession $checkoutSession,
array $data = []) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->checkoutSession = $checkoutSession;
parent::__construct($context);
}
public function execute()
{
$productId = 1;
$customPrice = 150;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productId, //product Id
'qty' => 1 //quantity of product
);
$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
//Enable super mode on the product.
$productItem->getProduct()->setIsSuperMode(true);
$this->cart->save();
echo "success";
}
public function getProductQuote($product)
{
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;
}
}
The above code adds the product of qty 1 to the cart with a custom price whenever it is called.
I want to add only 1 qty of product to the cart. suppose if its called two or three times, only one qty should be present in cart. Need to remove previously added qty from the cart.
Is something can be done programmatically? There is a product setting in backend "Maximum qty allowed in cart", we can set this value but I am looking for the code how it can be done programmatically.
Can anyone look into it and help me, please. Thanks
magento2 product addtocart controllers
I have created a custom controller in my module and calling via ajax to add the item to cart.
Below is my controller code.
<?php
namespace {Vendor}{Module}ControllerProduct;
class AddProduct extends MagentoFrameworkAppActionAction
{
protected $formKey;
protected $cart;
protected $product;
protected $checkoutSession;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoCheckoutModelSession $checkoutSession,
array $data = []) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->checkoutSession = $checkoutSession;
parent::__construct($context);
}
public function execute()
{
$productId = 1;
$customPrice = 150;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productId, //product Id
'qty' => 1 //quantity of product
);
$_product = $this->product->create()->load($productId);
$this->cart->addProduct($_product, $params);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
//Enable super mode on the product.
$productItem->getProduct()->setIsSuperMode(true);
$this->cart->save();
echo "success";
}
public function getProductQuote($product)
{
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;
}
}
The above code adds the product of qty 1 to the cart with a custom price whenever it is called.
I want to add only 1 qty of product to the cart. suppose if its called two or three times, only one qty should be present in cart. Need to remove previously added qty from the cart.
Is something can be done programmatically? There is a product setting in backend "Maximum qty allowed in cart", we can set this value but I am looking for the code how it can be done programmatically.
Can anyone look into it and help me, please. Thanks
magento2 product addtocart controllers
magento2 product addtocart controllers
asked 55 secs ago
jafar pinjarjafar pinjar
678414
678414
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f265396%2fallow-only-one-item-into-cart-in-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f265396%2fallow-only-one-item-into-cart-in-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown