<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在所有CMS页面上调用自定义phtml文件的步骤:</p><p>步骤1:首先,您需要在以下路径中创建cms_page_view.xml</p><p>app\code\Vendor\Extension\view\frontend\layout\cms_page_view.xml</p><pre class="brush:bash;toolbar:false"><?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="custom_phtml_in_cms" template="Vendor_Extension::call_allcms.phtml" /> </referenceContainer> </body> </page></pre><p>步骤2:现在您需要在以下路径中添加call_allcms.phtml文件</p><p>app\code\Vendor\Extension\view\frontend\templates\call_allcms.phtml</p><pre class="brush:bash;toolbar:false"><?php echo "Call this phtml file in all cms pages"; ?></pre><p>就是这样!</p><p><br/></p><p>结论:</p><p>按照上述步骤,您可以在Magento 2的所有CMS页面上调用自定义phtml文件。</p><p><br/></p>
文章列表
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中的管理员用户创建表单中添加新字段的步骤:</p><p>步骤1: 首先, 在以下路径创建一个“di.xml”文件。</p><p>app\code\Vendor\Extension\adminhtml\etc\di.xml</p><p>现在添加代码</p><pre class="brush:bash;toolbar:false"><?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\User\Block\User\Edit\Tab\Main"> <plugin name="admin_user_image" type="Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab\UserField" sortOrder="1"/> </type> </config></pre><p>步骤2:之后,我们需要 在以下路径创建一个“UserField.php”文件</p><p>app\code\Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab\UserField.php</p><p>并添加以下代码</p><pre class="brush:bash;toolbar:false"><?php namespace Vendor\Extension\Plugin\Block\Adminhtml\User\Edit\Tab; class UserField { public function aroundGetFormHtml( \Magento\User\Block\User\Edit\Tab\Main $subject, \Closure $proceed) { $form = $subject->getForm(); if (is_object($form)) { $fieldset = $form->addFieldset('admin_user_image', ['legend' => __('Custom Field')]); $fieldset->addField( 'user_image', 'image', [ 'name' => 'user_image', 'label' => __('Image'), 'id' => 'user_image', 'title' => __('Image'), 'required' => false, 'note' => 'Allow image type: jpg, jpeg, png' ] ); $subject->setForm($form); } return $proceed(); } }</pre><p>在这里,我们添加了一个图像自定义字段。但是,您可以通过自定义上述代码来添加所需的自定义字段。</p><p><img src="/uploads/images/20230831/58db64c0273c188838bf7655ee6f6611.png" title="a.png" alt="" width="745" height="409"/></p><p>自定义字段</p><p>结论:</p><p>希望所有人都能够在Magento 2中的管理员用户创建表单中添加新字段。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中将折扣组件添加到结帐订单摘要的步骤:</p><p>步骤 1:在以下路径创建checkout_index_index.xml</p><p>应用\代码\供应商\扩展\视图\前端\布局\checkout_index_index.xml</p><p>现在,添加代码,如下所示:</p><pre class="brush:bash;toolbar:false"><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="checkout.root"> <arguments> <argument name="jsLayout" xsi:type="array"> <item name="components" xsi:type="array"> <item name="checkout" xsi:type="array"> <item name="children" xsi:type="array"> <item name="sidebar" xsi:type="array"> <item name="children" xsi:type="array"> <item name="summary" xsi:type="array"> <item name="children" xsi:type="array"> <item name="summary-discount" xsi:type="array"> <item name="component" xsi:type="string">Magento_SalesRule/js/view/payment/discount</item> <item name="children" xsi:type="array"> <item name="errors" xsi:type="array"> <item name="sortOrder" xsi:type="string">0</item> <item name="component" xsi:type="string">Magento_SalesRule/js/view/payment/discount-messages</item> <item name="displayArea" xsi:type="string">messages</item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </argument> </arguments> </referenceBlock> </body> </page></pre><p>折扣组件将添加到结帐订单摘要中,如下所示</p><p><img src="/uploads/images/20230831/19f06ba553b2db88aa2d9414946f7d45.png" title="a.png" alt="" width="767" height="316"/></p><p>结账订单摘要时的折扣</p><p>结语:</p><p>因此,上述步骤将帮助您在Magento 2中的结帐订单摘要中添加折扣组件。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中以编程方式创建CMS静态页面的步骤:</p><p>步骤1:在Magento 2根目录中创建一个cmspage.php文件并添加以下代码。</p><pre class="brush:bash;toolbar:false"><?php use Magento\Framework\AppInterface; try { require_once __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try { $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $om = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $om->create('Magento\Cms\Model\PageFactory'); $pagedata = [ 'title' => 'Your Title Here', 'identifier' => 'page-identifier-here', 'stores' => 0, 'is_active' => 1, 'content_heading' => 'Page Heading Here', 'content' => 'page content here', 'page_layout' => '1column' ]; $storeManager->create()->setData($pagedata)->save(); echo "CMS static Page programmatically successfully created"; } catch(\Exception $e) { print_r($e->getMessage()); }</pre><p>添加上述代码后,运行以下链接。</p><p>https://www.domain.com/cmspage.php</p><p>结论:</p><p>因此,现在所有人都能够在Magento 2中以编程方式创建CMS静态页面。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中舍入所有价格的步骤:</p><p>第1步: 首先, 在我们的扩展名中,在下面的路径上创建一个“di.xml”文件。</p><p>app\code\Vendor\Extension\etc\di.xml</p><p>现在,添加此代码</p><pre class="brush:bash;toolbar:false"><?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Pricing\Render\Amount" type="Vendor\Extension\Pricing\Render\Amount"/> </config></pre><p>步骤2: 之后, 在以下扩展名文件夹路径内创建一个“金额.php”文件。</p><p>app\code\Vendor\Extension\Pricing\Render\Amount.php</p><p>并且,添加代码,如下所述</p><pre class="brush:bash;toolbar:false"><?php namespace Vendor\Extension\Pricing\Render; use Magento\Framework\Pricing\Amount\AmountInterface; use Magento\Framework\Pricing\SaleableInterface; use Magento\Framework\Pricing\Price\PriceInterface; use Magento\Framework\Pricing\PriceCurrencyInterface; use Magento\Framework\View\Element\Template; class Amount extends Template implements \Magento\Framework\Pricing\Render\AmountRenderInterface { protected $saleableItem; protected $price; protected $adjustmentRenders; protected $priceCurrency; protected $rendererPool; protected $amount; protected $displayValue; protected $adjustmentsHtml = []; public function __construct( Template\Context $context, AmountInterface $amount, PriceCurrencyInterface $priceCurrency, \Magento\Framework\Pricing\Render\RendererPool $rendererPool, SaleableInterface $saleableItem = null, PriceInterface $price = null, array $data = []) { parent::__construct($context, $data); $this->amount = $amount; $this->saleableItem = $saleableItem; $this->price = $price; $this->priceCurrency = $priceCurrency; $this->rendererPool = $rendererPool; } public function setDisplayValue($value) { $this->displayValue = $value; } public function getDisplayValue() { if ($this->displayValue !== null) { return round($this->displayValue); } else { return round($this->getAmount()->getValue()); } } public function getAmount() { return $this->amount; } public function getSaleableItem() { return $this->saleableItem; } public function getPrice() { return $this->price; } public function getDisplayCurrencyCode() { return $this->priceCurrency->getCurrency()->getCurrencyCode(); } public function getDisplayCurrencySymbol() { return $this->priceCurrency->getCurrencySymbol(); } public function hasAdjustmentsHtml() { return (bool) count($this->adjustmentsHtml); } public function getAdjustmentsHtml() { return implode('', $this->adjustmentsHtml); } protected function _toHtml() { $adjustmentRenders = $this->getAdjustmentRenders(); if ($adjustmentRenders) { $adjustmentHtml = $this->getAdjustments($adjustmentRenders); if (!$this->hasSkipAdjustments() || ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false)) { $this->adjustmentsHtml = $adjustmentHtml; } } $html = parent::_toHtml(); return $html; } protected function getAdjustmentRenders() { return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price); } protected function getAdjustments($adjustmentRenders) { $this->setAdjustmentCssClasses($adjustmentRenders); $data = $this->getData(); $adjustments = []; foreach ($adjustmentRenders as $adjustmentRender) { if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false) { $html = $adjustmentRender->render($this, $data); if (trim($html)) { $adjustments[$adjustmentRender->getAdjustmentCode()] = $html; } } } return $adjustments; } public function formatCurrency( $amount, $includeContainer = true, $precision = PriceCurrencyInterface::DEFAULT_PRECISION) { return $this->priceCurrency->format($amount, $includeContainer, $precision); } protected function setAdjustmentCssClasses($adjustmentRenders) { $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : []; $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders)); $this->setData('adjustment_css_classes', join(' ', $cssClasses)); return $this; } }</pre><p>结论:</p><p>通过这种方式,您可以四舍五入Magento 2中的所有价格。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>防止在Magento 2中多次将同一产品添加到购物车的步骤:</p><p>步骤1:首先,转到以下路径</p><p>app\code\Vendor\Extension\etc\di.xml</p><p>并添加代码如下</p><pre class="brush:bash;toolbar:false"><?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\Model\Cart"> <plugin disabled="false" name="Vendor_Extension_Plugin_Magento_Checkout_Cart_BeforeAddToCartProduct" type="Vendor\Extension\Plugin\Magento\Checkout\Cart\BeforeAddToCartProduct"/> </type> </config></pre><p>步骤2:现在,移至以下路径</p><p>app\code\Vendor\Extension\Plugin\Magento\Checkout\Cart\BeforeAddToCart.php</p><p>并添加下面提到的代码</p><pre class="brush:bash;toolbar:false"><?php declare(strict_types=1); namespace Vendor\Extension\Plugin\Magento\Checkout\Cart; use Magento\Checkout\Model\Cart; use Magento\Checkout\Model\Session; use Magento\Catalog\Model\Product; use Magento\Checkout\Model\Session\Proxy as SessionProxy; use Magento\Framework\Message\ManagerInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\UrlInterface; class BeforeAddToCartProduct { private $messageManager; private $cartSession; private $configurableProduct; private $url; private $session; public function __construct( Configurable $configurableProduct, ManagerInterface $messageManager, SessionProxy $cartSession, UrlInterface $url, Session $session) { $this->messageManager = $messageManager; $this->cartSession = $cartSession; $this->configurableProduct = $configurableProduct; $this->url = $url; $this->session = $session; } public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo=null) { $enableProductCartControl=true; $product = null; $parentProduct=null; if ($productInfo instanceof Product) { $product = $productInfo; if (!$product->getId()) { throw new \Magento\Framework\Exception\LocalizedException( __("This product wasn't found. Verify the product and try again.") ); } } if ($product) { // For Simple Product if ($product->getTypeId()==='configurable') { if (isset($requestInfo['super_attribute'])) { $parentProduct=$product; $childProduct = $this->configurableProduct->getProductByAttributes($requestInfo['super_attribute'] ,$product); // change $product to child $product=$childProduct; } } if ($product && $enableProductCartControl) { // For check product is in cart or not if($this->cartSession->getQuote()->hasProductId($product->getId())) { // Redirection to the cart page $this->session->setRedirectUrl($this->url->getUrl('checkout/cart/index')); throw new \Magento\Framework\Exception\LocalizedException( __("[x] This product is already in the cart. Testing, testing : ". $product->getSku()) ); } } } return [$productInfo, $requestInfo]; } /** * Get request for product add to cart procedure * * @param \Magento\Framework\DataObject|int|array $requestInfo * @return \Magento\Framework\DataObject * @throws \Magento\Framework\Exception\LocalizedException */ private function _getProductRequest($requestInfo) { if ($requestInfo instanceof \Magento\Framework\DataObject) { $request = $requestInfo; } elseif (is_numeric($requestInfo)) { $request = new \Magento\Framework\DataObject(['qty' => $requestInfo]); } elseif (is_array($requestInfo)) { $request = new \Magento\Framework\DataObject($requestInfo); } else { throw new \Magento\Framework\Exception\LocalizedException( __('We found an invalid request for adding product to quote.') ); } return $request; } }</pre><p>结论:</p><p>因此,通过上述步骤,您可以防止在Magento 2中多次将同一产品添加到购物车。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中使用JavaScript模块使用本地和cookie存储的步骤:</p><p>第 1 步:首先在扩展或主题中创建 JS 文件。为此,请创建自定义cookie.js</p><p>app\code\Vendor\Extension\view\Frontend\Web\Js\customcookie.js</p><p></p><pre class="brush:bash;toolbar:false">define([ 'jquery', 'uiComponent', 'jquery/jquery-storageapi' ], function ($, Component) { return Component.extend( { defaults: { cookieMessages: [] }, initialize: function () { this._super(); this.cookieMessages = $.cookieStorage.get('mage-string'); $.cookieStorage.set('mage-string', ''); } }); });</pre><p>要使用此功能,我们必须同时添加jquery和jquery/jquery-storageapi。</p><p>第 2 步:所以现在您可以使用</p><p>$.cookieStorage.get(<span style="color: #ce9178;">'mage-string'</span>)</p><p>步骤3:window.localStorage也支持将信息存储在本地存储中</p><pre class="brush:bash;toolbar:false">window.localStorage.setItem('myname', 'Vendor'); window.localStorage.getItem('myname'); window.localStorage.removeItem('myname'); window.localStorageclear();</pre><p>这些都是存储信息的不同方法。</p><p>结论:</p><p>因此,在上述代码的帮助下,所有人都能够做到。</p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2的结帐摘要中添加数量增量和递减功能的步骤:</p><p>步骤1:首先您需要在以下路径中添加/修改详细信息.html文件</p><p>应用程序\设计\前端\主题\您的主题\Magento_Checkout\web\template\summary\item\details.html</p><pre class="brush:bash;toolbar:false"><!-- ko foreach: getRegion('before_details') --> <!-- ko template: getTemplate() --><!-- /ko --> <!-- /ko --> <div class="product-item-details"> <div class="product-item-inner"> <div class="product-item-name-block"> <strong class="product-item-name" data-bind="html: $parent.name"> </strong> <div class="details-qty qty"> <label class="label" data-bind="i18n: 'Qty', attr: { for: 'cart-item-'+$parent.item_id+'-qty'}" style="display: none;"> </label> <button data-bind="attr: { id: 'minus-cart-item-'+$parent.item_id, 'data-cart-item': $parent.item_id, 'data-btn-minus': 'minus', },click:updateItemQtyCheckout" class="update-cart-item minus"> - </button> <input data-bind="attr: { id: 'cart-item-'+$parent.item_id+'-qty', 'data-cart-item': $parent.item_id, 'data-item-qty': $parent.qty, 'data-cart-item-id': $parent.product_sku }, value: $parent.qty" type="number" size="4" class="item-qty cart-item-qty" readonly> <button data-bind="attr: { id: 'plus-cart-item-'+$parent.item_id, 'data-cart-item': $parent.item_id, 'data-btn-plus': 'plus' },click:updateItemQtyCheckout" class="update-cart-item plus"> <!--<span data-bind="i18n: '+'"></span>--> + </button> <button data-bind="attr: { id: 'update-cart-item-'+$parent.item_id, 'data-cart-item': $parent.item_id, title: $t('Update') }" class="update-cart-item" style="display: none"> <span data-bind="i18n: 'Update'"> </span> </button> </div> </div> <!-- ko foreach: getRegion('after_details') --> <!-- ko template: getTemplate() --><!-- /ko --> <!-- /ko --> </div> <!-- ko if: (JSON.parse($parent.options).length > 0)--> <div class="product options" data-bind="mageInit: {'collapsible':{'openedState': 'active'}}"> <span data-role="title" class="toggle"><!-- ko i18n: 'View Details' --><!-- /ko --> </span> <div data-role="content" class="content"> <strong class="subtitle"><!-- ko i18n: 'Options Details' --><!-- /ko --> </strong> <dl class="item-options"> <!--ko foreach: JSON.parse($parent.options)--> <dt class="label" data-bind="text: label"> </dt> <!-- ko if: ($data.full_view)--> <dd class="values" data-bind="html: full_view"> </dd> <!-- /ko --> <!-- ko ifnot: ($data.full_view)--> <dd class="values" data-bind="html: value"> </dd> <!-- /ko --> <!-- /ko --> </dl> </div> </div> <!-- /ko --> </div> <!-- ko foreach: getRegion('item_message') --> <!-- ko template: getTemplate() --><!-- /ko --> <!-- /ko --></pre><p>步骤2:现在在以下路径中添加/修改详细信息.js文件</p><p>app\design\frontend\Themes\Yourtheme\Magento_Checkout\web\js\view\summary\item\details.js</p><p></p><pre class="brush:bash;toolbar:false">define([ 'jquery', 'uiComponent', 'Magento_Customer/js/model/authentication-popup', 'Magento_Customer/js/customer-data', 'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/action/get-totals', 'Magento_Checkout/js/model/shipping-service', 'Magento_Checkout/js/model/shipping-rate-registry', 'Magento_Checkout/js/model/resource-url-manager', 'mage/storage', 'Magento_Checkout/js/model/error-processor', 'mage/url', 'Magento_Ui/js/modal/alert', 'Magento_Ui/js/modal/confirm', 'underscore', 'jquery/ui', 'mage/decorate', 'mage/collapsible', 'mage/cookies' ], function ($, Component, authenticationPopup, customerData, quote, getTotalsAction, shippingService, rateRegistry, resourceUrlManager, storage, errorProcessor, url,alert, confirm, _) { 'use strict'; return Component.extend({ shoppingCartUrl: window.checkout.shoppingCartUrl, defaults: { template: 'Magento_Checkout/summary/item/details' }, /** * @param {Object} quoteItem * @return {String} */ getValue: function (quoteItem) { return quoteItem.name; }, updateItemQtyCheckout: function (data, event) { var btnminus = ""; var btnplus = ""; if (event.target.classList[1] == "minus") { btnminus = event.currentTarget.dataset.btnMinus; } if (event.target.classList[1] == "plus") { btnplus = event.currentTarget.dataset.btnPlus; } var itemId = event.currentTarget.dataset.cartItem; // If element is minus and quantity is '1' than remove var elem = $('#cart-item-' + itemId + '-qty'); if(event.target.classList[1] == 'plus') { elem.val(parseInt(elem.val()) + 1) } else if(event.target.classList[1] == 'minus') { elem.val(parseInt(elem.val()) - 1) } if (event.target.classList[1] == "minus" && $('#cart-item-' + itemId + '-qty').val() == '0') { var productData = this._getProductById(Number(itemId)); if (!_.isUndefined(productData)) { var self = this; var elemr = elem; self._ajax(url.build('checkout/sidebar/removeItem'), { 'item_id': itemId }, elemr, self._removeItemAfter); if (window.location.href === self.shoppingCartUrl) { window.location.reload(false); } } } else { this._ajax(url.build('checkout/sidebar/updateItemQty'), { 'item_id': itemId, 'item_qty': $('#cart-item-' + itemId + '-qty').val(), 'item_btn_plus': btnplus, 'item_btn_minus': btnminus }, elem, this._updateItemQtyAfter); } }, _getProductById: function (productId) { return _.find(customerData.get('cart')().items, function (item) { return productId === Number(item['item_id']); }); }, _updateItemQtyAfter: function (elem) { var productData = this._getProductById(Number(elem.data('cart-item'))); if (!_.isUndefined(productData)) { $(document).trigger('ajax:updateCartItemQty'); if (window.location.href === this.shoppingCartUrl) { window.location.reload(false); } } this._hideItemButton(elem); this._customerData(); }, _customerData: function () { var deferred = $.Deferred(); getTotalsAction([], deferred); var sections = ['cart']; customerData.invalidate(sections); customerData.reload(sections, true); var self = this; self._estimateTotalsAndUpdateRatesCheckout(); }, _ajax: function (url, data, elem, callback) { $.extend(data, { 'form_key': $.mage.cookies.get('form_key') }); $.ajax({ url: url, data: data, type: 'post', dataType: 'json', context: this, /** @inheritdoc */ beforeSend: function () { elem.attr('disabled', 'disabled'); }, /** @inheritdoc */ complete: function () { elem.attr('disabled', null); } }) .done(function (response) { var msg; if (response.success) { callback.call(this, elem, response); } else { msg = response['error_message']; if (msg) { alert({ content: msg }); } } }) .fail(function (error) { console.log(JSON.stringify(error)); }); }, _hideItemButton: function (elem) { var itemId = elem.data('cart-item'); $('#update-cart-item-' + itemId).hide('fade', 300); }, _removeItemAfter: function (elem) { var productData = this._getProductById(Number(elem.data('cart-item'))); if (!_.isUndefined(productData)) { $(document).trigger('ajax:removeFromCart', { productIds: [productData['product_id']] }); var sections = ['cart']; setTimeout(function () { if (customerData.get('cart')().items.length == 0) { window.location.reload(); } }, 2000); if (window.location.href.indexOf(this.shoppingCartUrl) === 0) { window.location.reload(); } } this._customerData(); }, _estimateTotalsAndUpdateRatesCheckout: function () { var serviceUrl, payload; var address = quote.shippingAddress(); shippingService.isLoading(true); serviceUrl = resourceUrlManager.getUrlForEstimationShippingMethodsForNewAddress(quote); payload = JSON.stringify({ address: { 'street': address.street, 'city': address.city, 'region_id': address.regionId, 'region': address.region, 'country_id': address.countryId, 'postcode': address.postcode, 'email': address.email, 'customer_id': address.customerId, 'firstname': address.firstname, 'lastname': address.lastname, 'middlename': address.middlename, 'prefix': address.prefix, 'suffix': address.suffix, 'vat_id': address.vatId, 'company': address.company, 'telephone': address.telephone, 'fax': address.fax, 'custom_attributes': address.customAttributes, 'save_in_address_book': address.saveInAddressBook } } ); storage.post( serviceUrl, payload, false ).done(function (result) { rateRegistry.set(address.getCacheKey(), result); shippingService.setShippingRates(result); }).fail(function (response) { shippingService.setShippingRates([]); errorProcessor.process(response); }).always(function () { shippingService.isLoading(false); }); } }); });</pre><p>实现上述代码后,数量增量和减少功能将添加到Magento 2商店的结帐摘要中。</p><p><img src="/uploads/images/20230831/22c7076a0d6b4849751768f23da2691b.png" title="a.png" alt="" width="768" height="402"/></p><p>结论:</p><p>希望每个人都能够在Magento 2的结帐摘要中添加数量增量和减少功能。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中显示可配置产品的常规和特价的步骤:</p><p>步骤 1:从 final_price.phtml 复制</p><p>Vendor\magento\module-configurable-product\view\base\templates\product\price\final_price.phtml</p><p>到</p><p>app\design\frontend\themees\Yourtheme\Magento_ConfigurableProduct\templates\product\price\final_price.phtml</p><p>步骤2:现在找到下面的代码行</p><pre class="brush:bash;toolbar:false"><?php if ($block->hasSpecialPrice()) : ?> <span class="old-price sly-old-price"></pre><p>并替换为,</p><pre class="brush:bash;toolbar:false"><?php if (!$block->isProductList() && $block->hasSpecialPrice()) : ?> <span class="old-price sly-old-price no-display"></pre><p>执行上述步骤后,特价和原始价格将显示在Magento 2中可配置产品的类别页面上。</p><p>结论:</p><p>通过这种方式,您可以在Magento 2中显示可配置产品的常规和特价。</p><p><br/></p>
<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>在Magento 2中使用选项卡小部件的步骤:</p><p>步骤1:为此,请覆盖list.phtml</p><p>app\design\frontend\Themes\Yourtheme\Magento_Catalog\templates\product\list.phtml</p><pre class="brush:bash;toolbar:false"><div class="product info detailed"> <div class="product data items" data-mage-init='{"tabs":{"openedState":"active"}}'> <div class="data item title" data-role="collapsible" id="tab-label-1"> <a class="data switch" tabindex="-1" data-toggle="trigger" href="#temp-1" id="tab-label-1-title"> <?php __("Tab 1"); ?> </a> </div> <div class="data item content" aria-labelledby="tab-label-1-title" id="temp-1" data-role="content"> <?php __("Hello Tab 1 Text"); ?> </div> <div class="data item title" data-role="collapsible" id="tab-label-2"> <a class="data switch" tabindex="-1" data-toggle="trigger" href="#temp-2" id="tab-label-2-title"> <?php __("Hello Tab 2 Text"); ?> </a> </div> <div class="data item content" aria-labelledby="tab-label-2-title" id="temp-2" data-role="content"> Hello 2 Text </div> </div> </div></pre><p>在这里,我创建了一个主题并在扩展名中覆盖Magento_catalog list.phtml 文件。您可以在 phtml 文件中的任何位置使用上述代码。</p><p>下面是显示两个选项卡的示例。它一次只显示一段内容。更改新的“选项卡”将导致同一组中的其他人消失,而新选项卡将单独显示。</p><p>结论:</p><p>这样,您就可以在Magento 2中使用选项卡小部件。</p><p><br/></p>