Magento2 Minicart 的工作过程

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/layout/default.xml#L25-L25
 
  1. <item name="template" xsi:type="string">Magento_Checkout/minicart/content</item>
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/web/template/minicart/content.html#L68-L72
 
  1. <ol id="mini-cart" class="minicart-items" data-bind="foreach: { data: getCartParam('items'), as: 'item' }">
  2. <!-- ko foreach: $parent.getRegion($parent.getItemRenderer(item.product_type)) -->
  3. <!-- ko template: {name: getTemplate(), data: item, afterRender: function() {$parents[1].initSidebar()}} --><!-- /ko -->
  4. <!-- /ko -->
  5. </ol>
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/web/js/view/minicart.js#L126-L139
 
  1. /**
  2. * Get cart param by name.
  3. * @param {String} name
  4. * @returns {*}
  5. */
  6. getCartParam: function (name) {
  7. if (!_.isUndefined(name)) {
  8. if (!this.cart.hasOwnProperty(name)) {
  9. this.cart[name] = ko.observable();
  10. }
  11. }
  12.  
  13. return this.cart[name]();
  14. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/web/js/view/minicart.js#L107-L109
 
  1. getItemRenderer: function (productType) {
  2. return this.itemRenderer[productType] || 'defaultRenderer';
  3. },
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/layout/checkout_cart_sidebar_item_renderers.xml#L16-L20
 
  1. <item name="itemRenderer" xsi:type="array">
  2. <item name="default" xsi:type="string">defaultRenderer</item>
  3. <item name="simple" xsi:type="string">defaultRenderer</item>
  4. <item name="virtual" xsi:type="string">defaultRenderer</item>
  5. </item>
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/layout/checkout_cart_sidebar_item_renderers.xml#L25-L28
 
  1. <item name="config" xsi:type="array">
  2. <item name="displayArea" xsi:type="string">defaultRenderer</item>
  3. <item name="template" xsi:type="string">Magento_Checkout/minicart/item/default</item>
  4. </item>
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/etc/frontend/di.xml#L41-L45
 
  1. <type name="Magento\Checkout\CustomerData\ItemPoolInterface">
  2. <arguments>
  3. <argument name="defaultItemId" xsi:type="string">Magento\Checkout\CustomerData\DefaultItem</argument>
  4. </arguments>
  5. </type>
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/ItemPool.php#L39-L55
 
  1. /**
  2. * Construct
  3. *
  4. * @param ObjectManagerInterface $objectManager
  5. * @param string $defaultItemId
  6. * @param array $itemMap
  7. * @codeCoverageIgnore
  8. */
  9. public function __construct(
  10. ObjectManagerInterface $objectManager,
  11. $defaultItemId,
  12. array $itemMap = []
  13. ) {
  14. $this->objectManager = $objectManager;
  15. $this->defaultItemId = $defaultItemId;
  16. $this->itemMap = $itemMap;
  17. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/ItemPool.php#L66-L84
 
  1. /**
  2. * Get section source by name
  3. *
  4. * @param string $type
  5. * @return ItemInterface
  6. * @throws LocalizedException
  7. */
  8. protected function get($type)
  9. {
  10. $itemId = isset($this->itemMap[$type]) ? $this->itemMap[$type] : $this->defaultItemId;
  11. $item = $this->objectManager->get($itemId);
  12.  
  13. if (!$item instanceof ItemInterface) {
  14. throw new LocalizedException(
  15. __('%1 doesn\'t extend \Magento\Checkout\CustomerData\ItemInterface', $type)
  16. );
  17. }
  18. return $item;
  19. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/DefaultItem.php#L64-L86
 
  1. protected function doGetItemData()
  2. {
  3. $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
  4. return [
  5. 'options' => $this->getOptionList(),
  6. 'qty' => $this->item->getQty() * 1,
  7. 'item_id' => $this->item->getId(),
  8. 'configure_url' => $this->getConfigureUrl(),
  9. 'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
  10. 'product_name' => $this->item->getProduct()->getName(),
  11. 'product_url' => $this->getProductUrl(),
  12. 'product_has_url' => $this->hasProductUrl(),
  13. 'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
  14. 'product_image' => [
  15. 'src' => $imageHelper->getUrl(),
  16. 'alt' => $imageHelper->getLabel(),
  17. 'width' => $imageHelper->getWidth(),
  18. 'height' => $imageHelper->getHeight(),
  19. ],
  20. 'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
  21. && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
  22. ];
  23. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/AbstractItem.php#L24-L31
 
  1. public function getItemData(Item $item)
  2. {
  3. $this->item = $item;
  4. return \array_merge(
  5. ['product_type' => $item->getProductType()],
  6. $this->doGetItemData()
  7. );
  8. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/Cart.php#L143-L163
 
  1. protected function getRecentItems()
  2. {
  3. $items = [];
  4. if (!$this->getSummaryCount()) {
  5. return $items;
  6. }
  7.  
  8. foreach (array_reverse($this->getAllQuoteItems()) as $item) {
  9. /* @var $item \Magento\Quote\Model\Quote\Item */
  10. if (!$item->getProduct()->isVisibleInSiteVisibility()) {
  11. $productId = $item->getProduct()->getId();
  12. $products = $this->catalogUrl->getRewriteByProductStore([$productId => $item->getStoreId()]);
  13. if (!isset($products[$productId])) {
  14. continue;
  15. }
  16. $urlDataObject = new \Magento\Framework\DataObject($products[$productId]);
  17. $item->getProduct()->setUrlDataObject($urlDataObject);
  18. }
  19. $items[] = $this->itemPoolInterface->getItemData($item);
  20. }
  21. return $items;
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/CustomerData/Cart.php#L87-L100
 
  1. public function getSectionData()
  2. {
  3. $totals = $this->getQuote()->getTotals();
  4. return [
  5. 'summary_count' => $this->getSummaryCount(),
  6. 'subtotal' => isset($totals['subtotal'])
  7. ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
  8. : 0,
  9. 'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
  10. 'items' => $this->getRecentItems(),
  11. 'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
  12. 'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
  13. ];
  14. }
 
 

magento/magento2/blob/487f5f45/app/code/Magento/Checkout/view/frontend/web/template/minicart/item/default.html#L7-L112
 
  1. <li class="item product product-item" data-role="product-item">
  2. <div class="product">
  3. <!-- ko if: product_has_url -->
  4. <a data-bind="attr: {href: product_url, title: product_name}" tabindex="-1" class="product-item-photo">
  5. <!-- ko foreach: $parent.getRegion('itemImage') -->
  6. <!-- ko template: {name: getTemplate(), data: item.product_image} --><!-- /ko -->
  7. <!-- /ko -->
  8. </a>
  9. <!-- /ko -->
  10. <!-- ko ifnot: product_has_url -->
  11. <span class="product-item-photo">
  12. <!-- ko foreach: $parent.getRegion('itemImage') -->
  13. <!-- ko template: {name: getTemplate(), data: item.product_image} --><!-- /ko -->
  14. <!-- /ko -->
  15. </span>
  16. <!-- /ko -->
  17.  
  18. <div class="product-item-details">
  19. <strong class="product-item-name">
  20. <!-- ko if: product_has_url -->
  21. <a data-bind="attr: {href: product_url}, text: product_name"></a>
  22. <!-- /ko -->
  23. <!-- ko ifnot: product_has_url -->
  24. <!-- ko text: product_name --><!-- /ko -->
  25. <!-- /ko -->
  26. </strong>
  27.  
  28. <!-- ko if: options.length -->
  29. <div class="product options" data-mage-init='{"collapsible":{"openedState": "active", "saveState": false}}'>
  30. <span data-role="title" class="toggle"><!-- ko i18n: 'See Details' --><!-- /ko --></span>
  31.  
  32. <div data-role="content" class="content">
  33. <strong class="subtitle"><!-- ko i18n: 'Options Details' --><!-- /ko --></strong>
  34. <dl class="product options list">
  35. <!-- ko foreach: { data: options, as: 'option' } -->
  36. <dt class="label"><!-- ko text: option.label --><!-- /ko --></dt>
  37. <dd class="values">
  38. <!-- ko if: Array.isArray(option.value) -->
  39. <span data-bind="html: option.value.join('<br>')"></span>
  40. <!-- /ko -->
  41. <!-- ko ifnot: Array.isArray(option.value) -->
  42. <span data-bind="html: option.value"></span>
  43. <!-- /ko -->
  44. </dd>
  45. <!-- /ko -->
  46. </dl>
  47. </div>
  48. </div>
  49. <!-- /ko -->
  50.  
  51. <div class="product-item-pricing">
  52. <!-- ko if: canApplyMsrp -->
  53.  
  54. <div class="details-map">
  55. <span class="label" data-bind="i18n: 'Price'"></span>
  56. <span class="value" data-bind="i18n: 'See price before order confirmation.'"></span>
  57. </div>
  58. <!-- /ko -->
  59. <!-- ko ifnot: canApplyMsrp -->
  60. <!-- ko foreach: $parent.getRegion('priceSidebar') -->
  61. <!-- ko template: {name: getTemplate(), data: item.product_price, as: 'price'} --><!-- /ko -->
  62. <!-- /ko -->
  63. <!-- /ko -->
  64.  
  65. <div class="details-qty qty">
  66. <label class="label" data-bind="i18n: 'Qty', attr: {
  67. for: 'cart-item-'+item_id+'-qty'}"></label>
  68. <input data-bind="attr: {
  69. id: 'cart-item-'+item_id+'-qty',
  70. 'data-cart-item': item_id,
  71. 'data-item-qty': qty
  72. }, value: qty"
  73. type="number"
  74. size="4"
  75. class="item-qty cart-item-qty"
  76. maxlength="12"/>
  77. <button data-bind="attr: {
  78. id: 'update-cart-item-'+item_id,
  79. 'data-cart-item': item_id,
  80. title: $t('Update')
  81. }"
  82. class="update-cart-item"
  83. style="display: none">
  84. <span data-bind="i18n: 'Update'"></span>
  85. </button>
  86. </div>
  87. </div>
  88.  
  89. <div class="product actions">
  90. <!-- ko if: is_visible_in_site_visibility -->
  91. <div class="primary">
  92. <a data-bind="attr: {href: configure_url, title: $t('Edit item')}" class="action edit">
  93. <span data-bind="i18n: 'Edit'"></span>
  94. </a>
  95. </div>
  96. <!-- /ko -->
  97. <div class="secondary">
  98. <a href="#" data-bind="attr: {'data-cart-item': item_id, title: $t('Remove item')}"
  99. class="action delete">
  100. <span data-bind="i18n: 'Remove'"></span>
  101. </a>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. </li>
 
 
转发:https://mage2.pro/t/topic/905
 
 

转载于:https://my.oschina.net/ganfanghua/blog/3031514

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值