注意 必须先清除缓存 然后google浏览器 F12=>应用=>储存=>本地储存空间=>选择当前域名=>mage-cache-storage=>cart

etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="maijindou_checkout_customer_data_cart" type="Maijindou\Checkout\Plugin\CustomerData\Cart" />
    </type>

    <preference for="Magento\Checkout\CustomerData\DefaultItem" type="Maijindou\Checkout\Plugin\CustomerData\DefaultItem" />
</config>
<?php
namespace Maijindou\Checkout\Plugin\CustomerData;

class Cart extends \Magento\Checkout\CustomerData\Cart
{
    /**
     * Get array of last added items
     *
     * @return \Magento\Quote\Model\Quote\Item[]
     */
    protected function getRecentItems()
    {
        $items = [];
        if (!$this->getSummaryCount()) {
            return $items;
        }

        foreach (array_reverse($this->getAllQuoteItems()) as $item) {
            /* @var $item \Magento\Quote\Model\Quote\Item */
            if (!$item->getProduct()->isVisibleInSiteVisibility()) {
                $product =  $item->getOptionByCode('product_type') !== null
                    ? $item->getOptionByCode('product_type')->getProduct()
                    : $item->getProduct();

                $products = $this->catalogUrl->getRewriteByProductStore([$product->getId() => $item->getStoreId()]);
                if (isset($products[$product->getId()])) {
                    $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
                    $item->getProduct()->setUrlDataObject($urlDataObject);
                }
            }
            $item_data = $this->itemPoolInterface->getItemData($item);
//如果是可配置产品更改图片路径 if($item_data['product_type']==='configurable'){ $children = $item->getChildren(); foreach($children as $child){ $child_item = $this->itemPoolInterface->getItemData($child); break; } $item_data['product_image']['src'] = isset($child_item['product_image']['src'])?$child_item['product_image']['src']:$item_data['product_image']['src']; } $items[] = $item_data; } return $items; } }
<?php


namespace Maijindou\Checkout\Plugin\CustomerData;


use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
use Magento\Checkout\CustomerData\AbstractItem;
use Magento\Framework\App\ObjectManager;

class DefaultItem extends AbstractItem
{
/**
* @var \Magento\Catalog\Helper\Image
*/
protected $imageHelper;

/**
* @var \Magento\Msrp\Helper\Data
*/
protected $msrpHelper;

/**
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;

/**
* @var \Magento\Catalog\Helper\Product\ConfigurationPool
*/
protected $configurationPool;

/**
* @var \Magento\Checkout\Helper\Data
*/
protected $checkoutHelper;

/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* @var ItemResolverInterface
*/
private $itemResolver;

/**
* @param \Magento\Catalog\Helper\Image $imageHelper
* @param \Magento\Msrp\Helper\Data $msrpHelper
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool
* @param \Magento\Checkout\Helper\Data $checkoutHelper
* @param \Magento\Framework\Escaper|null $escaper
* @param ItemResolverInterface|null $itemResolver
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Msrp\Helper\Data $msrpHelper,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
\Magento\Checkout\Helper\Data $checkoutHelper,
\Magento\Framework\Escaper $escaper = null,
ItemResolverInterface $itemResolver = null
) {
$this->configurationPool = $configurationPool;
$this->imageHelper = $imageHelper;
$this->msrpHelper = $msrpHelper;
$this->urlBuilder = $urlBuilder;
$this->checkoutHelper = $checkoutHelper;
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
}

/**
* @inheritdoc
*/
protected function doGetItemData()
{
$imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
$productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());

$src = $imageHelper->getUrl();
//如果是可配置产品更新产品图片
if($this->getProduct()->getTypeId()==='configurable'){
$children = $this->item->getChildren();
foreach($children as $child){
$imageHelperChild = $this->imageHelper->init($this->itemResolver->getFinalProduct($child), 'mini_cart_product_thumbnail');
}
$src = $imageHelperChild->getUrl();
}

return [
'options' => $this->getOptionList(),
'qty' => $this->item->getQty() * 1,
'item_id' => $this->item->getId(),
'configure_url' => $this->getConfigureUrl(),
'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
'product_id' => $this->item->getProduct()->getId(),
'product_name' => $productName,
'product_sku' => $this->item->getProduct()->getSku(),
'product_url' => $this->getProductUrl(),
'product_has_url' => $this->hasProductUrl(),
'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
'product_price_value' => $this->item->getCalculationPrice(),
'product_image' => [
'src' => $src,
'alt' => $imageHelper->getLabel(),
'width' => $imageHelper->getWidth(),
'height' => $imageHelper->getHeight(),
],
'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
&& $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
'message' => $this->item->getMessage(),
];
}

/**
* Get list of all options for product
*
* @return array
* @codeCoverageIgnore
*/
protected function getOptionList()
{
return $this->configurationPool->getByProductType($this->item->getProductType())->getOptions($this->item);
}

/**
* Returns product for thumbnail.
*
* @return \Magento\Catalog\Model\Product
* @codeCoverageIgnore
*/
protected function getProductForThumbnail()
{
return $this->itemResolver->getFinalProduct($this->item);
}

/**
* Returns product.
*
* @return \Magento\Catalog\Model\Product
* @codeCoverageIgnore
*/
protected function getProduct()
{
return $this->item->getProduct();
}

/**
* Get item configure url
*
* @return string
*/
protected function getConfigureUrl()
{
return $this->urlBuilder->getUrl(
'checkout/cart/configure',
['id' => $this->item->getId(), 'product_id' => $this->item->getProduct()->getId()]
);
}

/**
* Check Product has URL
*
* @return bool
*/
protected function hasProductUrl()
{
if ($this->item->getRedirectUrl()) {
return true;
}

$product = $this->item->getProduct();
$option = $this->item->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}

if ($product->isVisibleInSiteVisibility()) {
return true;
} else {
if ($product->hasUrlDataObject()) {
$data = $product->getUrlDataObject();
if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
return true;
}
}
}

return false;
}

/**
* Retrieve URL to item Product
*
* @return string
*/
protected function getProductUrl()
{
if ($this->item->getRedirectUrl()) {
return $this->item->getRedirectUrl();
}

$product = $this->item->getProduct();
$option = $this->item->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}

return $product->getUrlModel()->getUrl($product);
}
}
最后修改日期: 2022-10-28

作者