本文整理汇总了PHP中Magento\Quote\Api\CartRepositoryInterface类的典型用法代码示例。如果您正苦于以下问题:PHP CartRepositoryInterface类的具体用法?PHP CartRepositoryInterface怎么用?PHP CartRepositoryInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CartRepositoryInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Initialize coupon
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$couponCode = $this->getRequest()->getParam('remove') == 1 ? '' : trim($this->getRequest()->getParam('coupon_code'));
$cartQuote = $this->cart->getQuote();
$oldCouponCode = $cartQuote->getCouponCode();
$codeLength = strlen($couponCode);
if (!$codeLength && !strlen($oldCouponCode)) {
return $this->_goBack();
}
try {
$isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
$itemsCount = $cartQuote->getItemsCount();
if ($itemsCount) {
$cartQuote->getShippingAddress()->setCollectShippingRates(true);
$cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
$this->quoteRepository->save($cartQuote);
}
if ($codeLength) {
$escaper = $this->_objectManager->get('Magento\\Framework\\Escaper');
if (!$itemsCount) {
if ($isCodeLengthValid) {
$coupon = $this->couponFactory->create();
$coupon->load($couponCode, 'code');
if ($coupon->getId()) {
$this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
$this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
}
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
}
} else {
if ($isCodeLengthValid && $couponCode == $cartQuote->getCouponCode()) {
$this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
$this->cart->save();
}
}
} else {
$this->messageManager->addSuccess(__('You canceled the coupon code.'));
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addError(__('We cannot apply the coupon code.'));
$this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
}
return $this->_goBack();
}
示例2: get
/**
* {@inheritDoc}
*
* @param int $cartId The cart ID.
* @return Totals Quote totals data.
*/
public function get($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
/** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray($quoteTotals, $addressTotalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface');
$items = [];
foreach ($quote->getAllVisibleItems() as $index => $item) {
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
示例3: placeOrder
/**
* {@inheritdoc}
*/
public function placeOrder($cartId, $agreements = null, PaymentInterface $paymentMethod = null)
{
/** @var $quoteIdMask QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$this->cartRepository->get($quoteIdMask->getQuoteId())->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
return $this->quoteManagement->placeOrder($quoteIdMask->getQuoteId(), $agreements, $paymentMethod);
}
示例4: updateQuote
/**
* Update quote data
*
* @param Quote $quote
* @param array $details
* @return void
*/
private function updateQuote(Quote $quote, array $details)
{
$quote->setMayEditShippingAddress(false);
$quote->setMayEditShippingMethod(true);
$this->updateQuoteAddress($quote, $details);
$this->disabledQuoteAddressValidation($quote);
$quote->collectTotals();
$this->quoteRepository->save($quote);
}
示例5: savePaymentInQuote
/**
* Saves payment information in quote
*
* @param Object $response
* @return void
*/
public function savePaymentInQuote($response)
{
$quote = $this->quoteRepository->get($this->sessionTransparent->getQuoteId());
/** @var InfoInterface $payment */
$payment = $this->paymentManagement->get($quote->getId());
$payment->setAdditionalInformation('pnref', $response->getPnref());
$this->errorHandler->handle($payment, $response);
$this->paymentManagement->set($quote->getId(), $payment);
}
示例6: execute
/**
* Fetch coupon info
*
* Controller Action
*/
public function execute()
{
$total = $this->getRequest()->getParam('cost');
$quote = $this->_checkoutSession->getQuote();
//save value to DiscountCoupon collect
$this->_registry->register('mercadopago_total_amount', $total);
$this->quoteRepository->save($quote->collectTotals());
return;
}
示例7: execute
/**
* Initialize shipping information
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$city = (string) $this->getRequest()->getParam('estimate_city');
$regionId = (string) $this->getRequest()->getParam('region_id');
$region = (string) $this->getRequest()->getParam('region');
$this->cart->getQuote()->getShippingAddress()->setCountryId($country)->setCity($city)->setPostcode($postcode)->setRegionId($regionId)->setRegion($region)->setCollectShippingRates(true);
$this->quoteRepository->save($this->cart->getQuote());
$this->cart->save();
return $this->_goBack();
}
示例8: savePaymentInformation
/**
* {@inheritDoc}
*/
public function savePaymentInformation($cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null)
{
if ($billingAddress) {
$billingAddress->setEmail($email);
$this->billingAddressManagement->assign($cartId, $billingAddress);
} else {
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$this->cartRepository->getActive($quoteIdMask->getQuoteId())->getBillingAddress()->setEmail($email);
}
$this->paymentMethodManagement->set($cartId, $paymentMethod);
return true;
}
示例9: execute
/**
* Return shipping options items for shipping address from request
*
* @return void
*/
public function execute()
{
try {
$quoteId = $this->getRequest()->getParam('quote_id');
$this->_quote = $this->quoteRepository->get($quoteId);
$this->_initCheckout();
$response = $this->_checkout->getShippingOptionsCallbackResponse($this->getRequest()->getParams());
$this->getResponse()->setBody($response);
} catch (\Exception $e) {
$this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
}
}
示例10: testExecute
public function testExecute()
{
$quoteMock = $this->getQuoteMock();
$quoteMock->expects(self::exactly(2))->method('getIsVirtual')->willReturn(false);
$quoteMock->expects(self::exactly(2))->method('getShippingAddress')->willReturn($this->shippingAddressMock);
$this->shippingAddressMock->expects(self::once())->method('getShippingMethod')->willReturn(self::TEST_SHIPPING_METHOD . '-bad');
$this->disabledQuoteAddressValidationStep($quoteMock);
$this->shippingAddressMock->expects(self::once())->method('setShippingMethod')->willReturn(self::TEST_SHIPPING_METHOD);
$this->shippingAddressMock->expects(self::once())->method('setCollectShippingRates')->willReturn(true);
$quoteMock->expects(self::once())->method('collectTotals');
$this->quoteRepositoryMock->expects(self::once())->method('save')->with($quoteMock);
$this->shippingMethodUpdater->execute(self::TEST_SHIPPING_METHOD, $quoteMock);
}
示例11: calculate
/**
* {@inheritDoc}
*/
public function calculate($cartId, \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($cartId);
$this->validateQuote($quote);
if ($quote->getIsVirtual()) {
$quote->setBillingAddress($addressInformation->getAddress());
} else {
$quote->setShippingAddress($addressInformation->getAddress());
$quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod($addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode());
}
$quote->collectTotals();
return $this->cartTotalRepository->get($cartId);
}
示例12: recordOrder
protected function recordOrder($order)
{
$customOrderId = null;
//https://github.com/magento/magento2/issues/4233
$quoteId = $order->getQuoteId();
$quote = $this->quoteRepository->get($quoteId);
$shippingAddress = $quote->getShippingAddress();
$carrierType = $shippingAddress->getCarrierType();
// $order->setCarrierType($carrierType);
$order->setDestinationType($shippingAddress->getDestinationType());
$order->setValidationStatus($shippingAddress->getValidationStatus());
$this->carrierGroupHelper->saveOrderDetail($order, $shippingAddress);
$this->carrierGroupHelper->recordOrderItems($order);
$this->packageHelper->saveOrderPackages($order, $shippingAddress);
if (strstr($order->getShippingMethod(), 'shqshared_')) {
$orderDetailArray = $this->carrierGroupHelper->loadOrderDetailByOrderId($order->getId());
//SHQ16- Review for splits
foreach ($orderDetailArray as $orderDetail) {
$original = $orderDetail->getCarrierType();
$carrierTypeArray = explode('_', $orderDetail->getCarrierType());
if (is_array($carrierTypeArray)) {
$orderDetail->setCarrierType($carrierTypeArray[1]);
//SHQ16-1026
$currentShipDescription = $order->getShippingDescription();
$shipDescriptionArray = explode('-', $currentShipDescription);
$cgArray = $this->shipperDataHelper->decodeShippingDetails($orderDetail->getCarrierGroupDetail());
foreach ($cgArray as $key => $cgDetail) {
if (isset($cgDetail['carrierType']) && $cgDetail['carrierType'] == $original) {
$cgDetail['carrierType'] = $carrierTypeArray[1];
}
if (is_array($shipDescriptionArray) && isset($cgDetail['carrierTitle'])) {
$shipDescriptionArray[0] = $cgDetail['carrierTitle'] . ' ';
$newShipDescription = implode('-', $shipDescriptionArray);
$order->setShippingDescription($newShipDescription);
}
$cgArray[$key] = $cgDetail;
}
$encoded = $this->shipperDataHelper->encode($cgArray);
$orderDetail->setCarrierGroupDetail($encoded);
$orderDetail->save();
}
$this->shipperLogger->postInfo('Shipperhq_Shipper', 'Rates displayed as single carrier', 'Resetting carrier type on order to be ' . $carrierTypeArray[1]);
}
}
if ($this->shipperDataHelper->useDefaultCarrierCodes()) {
$order->setShippingMethod($this->getDefaultCarrierShipMethod($order, $shippingAddress));
}
$order->save();
}
示例13: beforeDispatch
/**
* @param \Magento\Checkout\Controller\Cart $subject
* @param \Magento\Framework\App\RequestInterface $request
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(\Magento\Checkout\Controller\Cart $subject, \Magento\Framework\App\RequestInterface $request)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->checkoutSession->getQuote();
// Clear shipping addresses and item assignments after Multishipping flow
if ($quote->isMultipleShippingAddresses()) {
foreach ($quote->getAllShippingAddresses() as $address) {
$quote->removeAddress($address->getId());
}
$quote->getShippingAddress();
$quote->setIsMultiShipping(false);
$quote->collectTotals();
$this->cartRepository->save($quote);
}
}
示例14: testSavePaymentInQuote
public function testSavePaymentInQuote()
{
$quoteId = 1;
$response = new DataObject();
$payment = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Payment')->disableOriginalConstructor()->getMock();
$payment->expects($this->once())->method('setAdditionalInformation')->with('pnref');
$this->errorHandlerMock->expects($this->once())->method('handle')->with($payment, $response);
$quote = $this->getMock('Magento\\Quote\\Api\\Data\\CartInterface', [], [], '', false);
$quote->expects($this->exactly(2))->method('getId')->willReturn($quoteId);
$this->sessionTransparent->expects($this->once())->method('getQuoteId')->willReturn($quoteId);
$this->quoteRepository->expects($this->once())->method('get')->willReturn($quote);
$this->paymentMethodManagementInterface->expects($this->once())->method('get')->willReturn($payment);
$this->paymentMethodManagementInterface->expects($this->once())->method('set');
$this->model->savePaymentInQuote($response);
}
示例15: getQuote
/**
* Retrieve quote model object
*
* @return \Magento\Quote\Model\Quote
*/
public function getQuote()
{
if ($this->_quote === null) {
$this->_quote = $this->quoteFactory->create();
if ($this->getStoreId()) {
if (!$this->getQuoteId()) {
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
$this->_quote->setIsActive(false);
$this->_quote->setStoreId($this->getStoreId());
$this->quoteRepository->save($this->_quote);
$this->setQuoteId($this->_quote->getId());
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
} else {
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
$this->_quote->setStoreId($this->getStoreId());
}
if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
$customer = $this->customerRepository->getById($this->getCustomerId());
$this->_quote->assignCustomer($customer);
$this->quoteRepository->save($this->_quote);
}
}
$this->_quote->setIgnoreOldQty(true);
$this->_quote->setIsSuperMode(true);
}
return $this->_quote;
}