0

I upgraded from magento 2.2.2 to 2.2.11 since I have an error when selecting a shipping address.

I get the error: "Notice: Array to string conversion in mywebsite/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3043".

enter image description here

But this error is present only when it is an address registered in the database, if I add an address it will work.

I've searched for the value that causes the problem.

In the logs I managed to get this :

INFO (6): array (
 'region_code' => NULL,
 'region' => NULL,
 'region_id' => 0,
) 

I tried to find out where the region values were retrieved from. To not have them because they are useless to me and are all empty or equal to Null.

asked Oct 12, 2020 at 16:19
3
  • You mean, you downgraded? Commented Oct 12, 2020 at 22:08
  • No It's an upgrade 2.2.2 to 2.2.11 Commented Oct 13, 2020 at 7:17
  • OpenSource to Commerce? Commented Oct 13, 2020 at 8:28

1 Answer 1

0

Ok I have resolve my problem.

For this I have create a plugin on WebSite/Cost/Plugin/Total/Quote/Credit.php

With fixQuoteRegion

 <?php
namespace WebSite\Cost\Plugin\Total\Quote;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\Quote\Model\Quote\Address;
use Mirasvit\Credit\Model\Config;
use Mirasvit\Credit\Model\BalanceFactory;
use Mirasvit\Credit\Helper\Calculation;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total as AddressTotal;
class Credit
{
 /**
 * @var array
 */
 protected $processedShippingAddresses = [];
 /**
 * @var int
 */
 protected $amountUsed = 0;
 /**
 * @var int
 */
 protected $baseAmountUsed = 0;
 /**
 * @var Calculation
 */
 private $calculationHelper;
 public function __construct(
 Calculation $calculationHelper,
 \Magento\Sales\Model\Order $order,
 \Magento\Framework\Pricing\Helper\Data $currencyHelper,
 \Mirasvit\Credit\Model\BalanceFactory $balanceFactory,
 \Magento\Checkout\Helper\Data $checkoutHelper,
 Config $config
 ) {
 $this->calculationHelper = $calculationHelper;
 $this->currencyHelper = $currencyHelper;
 $this->balanceFactory = $balanceFactory;
 $this->checkoutHelper = $checkoutHelper;
 $this->config = $config;
 $this->_order = $order;
 }
 /**
 * @param \Mirasvit\Credit\Model\Total\Quote\Credit $subject
 * @param $proceed
 * @param \Magento\Quote\Model\Quote $quote
 * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
 * @param Address\Total $total
 * @return $this
 */
 public function aroundCollect(
 \Mirasvit\Credit\Model\Total\Quote\Credit $subject,
 $proceed,
 \Magento\Quote\Model\Quote $quote,
 \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
 \Magento\Quote\Model\Quote\Address\Total $total
 ) {
 $address = $shippingAssignment->getShipping()->getAddress();
 $this->fixQuoteRegion($quote);
 if ($quote->getUseCredit() == Config::USE_CREDIT_NO) {
 $address->setBaseCreditAmount(0) //fix credit in address when no use anymore
 ->setCreditAmount(0)
 ->save();
 return $this;
 }
 if ($quote->getUseCredit() == Config::USE_CREDIT_UNDEFINED && !$this->config->isAutoApplyEnabled()) {
 $address->setBaseCreditAmount(0) //fix credit in address when no use anymore
 ->setCreditAmount(0)
 ->save();
 return $this;
 }
 if (!$quote->getCustomer() || !$quote->getCustomer()->getId()) {
 $address->setBaseCreditAmount(0) //fix credit in address when no use anymore
 ->setCreditAmount(0)
 ->save();
 return $this;
 }
 $quote->setUseCredit(Config::USE_CREDIT_YES);
 $quote->setBaseCreditAmountUsed(0)
 ->setCreditAmountUsed(0)
 ->save();
 $this->resetMultishippingTotalsOnRecollection($quote, $address->getId());
 $baseUsed = $this->balanceFactory->create()
 ->loadByCustomer($quote->getCustomerId(), $this->_order->getOrderCurrency())
 ->getAmount();
 $used = $this->currencyHelper->currency($baseUsed, false, false);
 $customerUsed = $used;
 $customerBaseUsed = $baseUsed;
 if ($quote->getIsMultiShipping()) {
 $used -= $this->amountUsed;
 $baseUsed -= $this->baseAmountUsed;
 } else {
 $this->amountUsed = $used;
 $this->baseAmountUsed = $baseUsed;
 }
 if ($used > $total->getGrandTotal() && /*(float)$total->getGrandTotal() &&*/ $total->getGrandTotal() >= 0) {
 $used = $total->getGrandTotal();
 $baseUsed = $total->getBaseGrandTotal();
 }
 $maxUsed = $this->calculationHelper->calc(
 $total->getGrandTotal(), $total->getTotalAmount('tax'), $total->getTotalAmount('shipping')
 );
 if ($maxUsed < $used) {
 $used = $maxUsed;
 }
 if ($maxUsed < $baseUsed) {
 $baseUsed = $maxUsed;
 }
 if ($quote->getIsMultiShipping()) {
 $this->amountUsed += $used;
 $this->baseAmountUsed += $baseUsed;
 if ($this->amountUsed > $customerUsed) {
 $this->amountUsed = $customerUsed;
 $this->baseAmountUsed = $customerBaseUsed;
 }
 $this->processedShippingAddresses[$address->getId()] = $used;
 }
 //Overload : Allow to place order and not refill thr storeCredit if credit < 0
 if($quote->getIgnoreNegatif() == true){
 $used = $total->getGrandTotal();
 $baseUsed = $total->getBaseGrandTotal();
 $this->amountUsed = $total->getGrandTotal();
 $this->baseAmountUsed = $total->getBaseGrandTotal();
 }
 $quote->setBaseCreditAmountUsed($this->baseAmountUsed)
 ->setCreditAmountUsed($this->amountUsed);
 $address->setBaseCreditAmount($baseUsed)
 ->setCreditAmount($used)
 ->save();
 $total->setBaseTotalAmount($subject->getCode(), $baseUsed);
 $total->setTotalAmount($subject->getCode(), $used);
 $total->setBaseGrandTotal($total->getBaseGrandTotal() - $baseUsed);
 $total->setGrandTotal($total->getGrandTotal() - $used);
 $total->setBaseCreditAmount($baseUsed);
 $total->setCreditAmount($used);
 $quote->setCreditCollected(true)
 ->save();
 return $this;
 }
 /**
 * @param \Magento\Quote\Model\Quote $quote
 * @param int $addressId
 *
 * @return void
 */
 protected function resetMultishippingTotalsOnRecollection($quote, $addressId)
 {
 if (
 $quote->getIsMultiShipping()
 && !empty($this->processedShippingAddresses[$addressId])
 && $this->amountUsed
 ) {
 $this->amountUsed = 0;
 $this->processedShippingAddresses = [];
 }
 }
 /**
 * @param \Magento\Quote\Model\Quote $quote
 *
 * @return void
 */
 protected function fixQuoteRegion($quote)
 {
 /** @var \Magento\Customer\Model\Data\Region $region */
 /** @var \Magento\Framework\App\PageCache\Version $region */
 $region = $quote->getShippingAddress()->getRegion();
 if ($region instanceof \Magento\Customer\Model\Data\Region) {
 $quote->getShippingAddress()->setRegion($region->getRegion());
 } elseif (is_array($region)) { //M2.2.x
 $quote->getShippingAddress()->setRegion($region['region'] ?: '');
 }
 $region = $quote->getBillingAddress()->getRegion();
 if ($region instanceof \Magento\Customer\Model\Data\Region) {
 $quote->getBillingAddress()->setRegion($region->getRegion());
 } elseif (is_array($region)) { //M2.2.x
 $quote->getBillingAddress()->setRegion($region['region'] ?: '');
 }
 }
}
answered Oct 16, 2020 at 12:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.