<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:在以下路径中创建“注册.php”文件:</p><p>app\code\Vendor\Extension</p><p>现在添加以下代码:</p><p></p><pre class="brush:bash;toolbar:false"><?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Extension', __DIR__ );</pre><p>第 2 步:在以下路径内创建一个“模块.xml”文件:</p><p>app\code\Vendor\Extension\etc</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:Module/etc/module.xsd"> <module name="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0"> </module> </config></pre><p>第 3 步:在以下路径内创建一个“checkout_index_index.xml”文件:</p><p>app\code\Vendor\Extension\view\frontend\layout</p><p>现在添加以下代码:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" 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="custom_sidebar" xsi:type="array"> <item name="component" xsi:type="string">Vendor_Extension/js/view/custom_sidebar</item> <item name="displayArea" xsi:type="string">summary</item> <item name="config" xsi:type="array"> <item name="template" xsi:type="string">Vendor_Extension/custom_sidebar </item> </item> </item> </item> </item> </item> </item> </item> </argument> </arguments> </referenceBlock> </body> </page></pre><p>第 4 步:在以下路径内创建一个“custom_sidebar.js”文件:</p><p>app\code\Vendor\Extension\view\frontend\web\js\view</p><p>现在添加以下代码:</p><p></p><pre class="brush:bash;toolbar:false">define([ 'uiComponent', 'ko', 'jquery' ], function (Component, ko, $,) { 'use strict'; return Component.extend({ }); });</pre><p>步骤5:在以下路径内创建一个“custom_sidebar.html”文件:</p><p>app\code\Vendor\Extension\view\frontend\web\template</p><p>现在添加以下代码:</p><pre class="brush:bash;toolbar:false"><div id="opc-sidebar" class="custom" style="background-color: #d6d1d1;"> <h3 style="padding:20px;">Custom Sidebar</h3> </div></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>注意:在这里,我们使用了联系人电子邮件模板,并演示了如何在该模板上获取系统配置值。</p><p>步骤1:更新vendor/magento/module-contact/view/frontend/email 文件夹中的submitted_form.html文件,并通过以下代码参考更新代码(您可以根据需要使用文件路径)</p><pre class="brush:bash;toolbar:false"><!-- /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ --> <!--@subject {{trans "Contact Form"}} @--> <!--@vars { "var data.comment":"Comment", "var data.email":"Sender Email", "var data.name":"Sender Name", "var data.telephone":"Sender Telephone" } @--> {{template config_path="design/email/header_template"}} <table class="message-details"> <tr> <td><strong>{{trans "Name"}}</strong></td> <td>{{var data.name}}</td> </tr> <tr> <td><strong>{{trans "Email"}}</strong></td> <td>{{var data.email}}</td> </tr> <tr> <td><strong>{{trans "Phone"}}</strong></td> <td>{{var data.telephone}}</td> </tr> </table> <p><strong>{{trans "Message"}}</strong></p> <p>{{var data.comment}}</p> <!-- Code For Getting System Configuration Value Start-→ <p><strong>{{trans "Store Contact Number"}}</strong></p> <p>{{config path="general/store_information/phone"}}</p> <!-- Code For Getting System Configuration Value End-→ {{template config_path="design/email/footer_template"}}</pre><p>步骤2:最后运行以下命令</p><pre class="brush:bash;toolbar:false">php bin/magento cache:flush</pre><p>结论:</p><p>通过这种方式,您可以在电子邮件模板中获取系统配置值。</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:在以下路径中创建一个示例系统.xml:</p><p>app/code/Vendor/Extension/etc/adminhtml/system.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="../../../../Magento/Backend/etc/system_file.xsd"> <system> <tab id="vendortab" translate="label" sortOrder="100"> <label>Vendor TAB</label> </tab> <section id="sectionid" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>General Config</label> <tab>vendortab</tab> <resource>Vendor_Extension::config</resource> <group id="group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0"> <label>General Configuration</label> <field id="field" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Field</label> </field> </group> </section> </system> </config></pre><p>步骤2:为此,请在以下路径中创建di.xml:</p><p>app\code\Vendor\Extension\etc\adminhtml\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\Config\Model\Config\Structure\Data"> <plugin name="vendor_dynamic" type="Vendor\Extension\Plugin\Config\Field\Data"/> </type> </config><span style="font-weight: 400;"> </span></pre><p>步骤3: 接下来,在插件中创建一个名为 Data.php 文件的插件:</p><p>app\code\Vendor\Extension\Plugin\Config\Field\Data.php</p><p>并将以下代码添加到文件中:</p><pre class="brush:bash;toolbar:false"><?php namespace Vendor\Extension\Plugin\Config\Field; use Magento\Config\Model\Config\Structure\Data as StructureData; use Magento\Framework\Module\ModuleListInterface; class Data { public function __construct(ModuleListInterface $moduleList) { $this->_moduleList = $moduleList; } public function beforeMerge(StructureData $object, array $config) { $moduleList = $this->_moduleList->getNames(); foreach ($moduleList as $name) { if (strpos($name, 'Vendor_Extension') === false) { continue; } $this->moduleslist[] = $name; } if (!isset($config['config']['system'])) { return [$config]; } $sections = $config['config']['system']['sections']; foreach ($sections as $sectionId => $section) { if (isset($section['tab']) && ($section['tab'] === 'vendor') && ($section['id'] !== 'vendor')) { foreach ($this->moduleslist as $moduleName) { if ($section['id'] !== 'sectionid') { continue; } $dynamicGroups = $this->getGroups($moduleName, $section['id']); if (!empty($dynamicGroups)) { $config['config']['system']['sections'][$sectionId]['children'] = $dynamicGroups + $section['children']; } break; } } } return [$config]; } protected function getGroups($moduleName, $sectionName) { $defaultFieldOptions = [ 'type' => 'text', 'showInDefault' => '1', 'showInWebsite' => '1', 'showInStore' => '1', 'sortOrder' => 1, 'module_name' => $moduleName, 'validate' => 'required-entry', '_elementType' => 'field', 'path' => $sectionName . '/module' ]; $fields = []; foreach ($this->getNewField() as $id => $option) { $fields[$id] = array_merge($defaultFieldOptions, ['id' => $id], $option); } return [ 'module' => [ 'id' => 'module', 'label' => __('Dynamic Section'), 'showInDefault' => '1', 'showInWebsite' => '0', 'showInStore' => '0', '_elementType' => 'group', 'path' => $sectionName, 'children' => $fields ] ]; } protected function getNewField() { return [ 'name1' => [ 'label' => __('Dynamic Field'), 'frontend_class' => 'vendor_dynamic_class', 'show' => 1, 'tooltip' => __('Dynamic Field Tooltip'), 'comment' => __('Dynamic Field Comment') ], ]; } }</pre><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 步:首先,我们需要在以下路径上在扩展中创建一个“di.xml”文件:</p><p>app\code\Vendor\Extension\etc</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\Catalog\Ui\DataProvider\Product\ProductDataProvider"> <plugin name="join_product_grid_with_stock_status" type="Vender\Extension\Plugin\Ui\DataProvider\Product\ProductDataProvider" sortOrder="100" /> </type> </config></pre><p>步骤2: 之后,我们需要 在以下扩展名的文件夹路径内创建一个“ProductDataProvider.php”文件:</p><p>app\code\Vendor\Extension\Plugin\Ui\DataProvider\Product</p><p>然后添加以下代码:</p><pre class="brush:bash;toolbar:false"><?php namespace Vender\Extension\Plugin\Ui\DataProvider\Product; class ProductDataProvider { /** * @param \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject * @param \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection * @return mixed */ public function afterGetCollection( \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject, $collection) { $columns = $collection->getSelect()->getPart(\Zend_Db_Select::COLUMNS); if (!$collection->isLoaded() && !$this->checkJoin($columns)) { $collection->joinTable( 'cataloginventory_stock_status', 'product_id=entity_id', ["stock_status" => "stock_status"], null , 'left' )->addAttributeToSelect('stock_status'); } return $collection; } /** * @param array $columns * @return bool */ private function checkJoin($columns) { foreach ($columns as $column) { if(is_array($column)) { if(in_array('stock_status', $column)) { return true; } } } return false; } }</pre><p>步骤3:之后,我们需要在以下扩展名文件夹路径内创建一个“product_listing.xml”文件:</p><p>app\code\Vendor\Extension\view\adminhtml\ui_component</p><p>之后添加代码,如下所示:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0" encoding="UTF-8"?> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <listingToolbar name="listing_top"> <filters name="listing_filters"> <filterSelect name="stock_status" provider="${ $.parentName }"> <settings> <options class="Vender\Extension\Ui\Component\Listing\Columns\StockStatus\Options"/> <label translate="true">Stock Status</label> <dataScope>stock_status</dataScope> </settings> </filterSelect> </filters> </listingToolbar> <columns name="product_columns"> <column name="stock_status" class="Vender\Extension\Ui\Component\Listing\Columns\StockStatus" sortOrder="900"> <settings> <addField>true</addField> <filter>select</filter> <options class="Vender\Extension\Ui\Component\Listing\Columns\StockStatus\Options"/> <label translate="true">Stock Status</label> <dataType>select</dataType> </settings> </column> </columns> </listing></pre><p>步骤4: 之后,我们需要在以下扩展名文件夹路径内创建一个“StockStatus.php”文件:</p><p>app\code\Vendor\Extension\Ui\Component\Listing\Columns</p><p>接下来,添加以下代码:</p><pre class="brush:bash;toolbar:false"><?php namespace Vender\Extension\Ui\Component\Listing\Columns; class StockStatus extends \Magento\Ui\Component\Listing\Columns\Column { /** * Column name */ const NAME = 'column.stock_status'; /** * Prepare Data Source * * @param array $dataSource * @return array */ public function prepareDataSource(array $dataSource) { if (isset($dataSource['data']['items'])) { $fieldName = $this->getData('name'); foreach ($dataSource['data']['items'] as & $item) { if (isset($item[$fieldName])) { $item[$fieldName] = $this->getStatus($item[$fieldName]); } } } return $dataSource; } /** * @param $status * @return \Magento\Framework\Phrase */ private function getStatus($status) { if($status == 1) { return __('In Stock'); } else { return __('Out of Stock'); } } }</pre><p>步骤5: 最后,我们需要在以下扩展名的文件夹路径中创建“Options.php”文件:</p><p>app\code\Vendor\Extension\Ui\Component\Listing\Columns\StockStatus</p><p>最后,添加代码,如下所述:</p><pre class="brush:bash;toolbar:false"><?php namespace Vender\Extension\Ui\Component\Listing\Columns\StockStatus; use Magento\Framework\Data\OptionSourceInterface; class Options implements OptionSourceInterface { /** * @var array */ protected $options; /** * Get options * * @return array */ public function toOptionArray() { if ($this->options !== null) { return $this->options; } $this->options = [ [ 'label' => __('In Stock'), 'value' => 1 ], [ 'label' => __('Out of Stock'), 'value' => 0 ] ]; return $this->options; } }</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>将主题中的供应商/magento/module-sales/view/frontend/templates/order/history.phtml 文件复制到以下路径下:</p><p>app/design/frontend/themees/yourtheme/Magento_Sales/templates/order/history.phtml</p><p>现在,添加以下代码:</p><pre class="brush:bash;toolbar:false"><?php $_orders = $block->getOrders(); ?> <?= $block->getChildHtml('info') ?> <?php if ($_orders && count($_orders)): ?> <div class="table-wrapper orders-history"> <table class="data table table-order-items history" id="my-orders-table"> <caption class="table-caption"><?= /* @escapeNotVerified */ __('Orders') ?></caption> <thead> <tr> <th scope="col" class="col id"><?= /* @escapeNotVerified */ __('Order #') ?></th> <th scope="col" class="col date"><?= /* @escapeNotVerified */ __('Date') ?></th> <?= /* @noEscape */ $block->getChildHtml('extra.column.header') ?> <th scope="col" class="col shipping"><?= /* @escapeNotVerified */ __('Ship To') ?></th> <th scope="col" class="col total"><?= /* @escapeNotVerified */ __('Order Total') ?></th> <th scope="col" class="col status"><?= /* @escapeNotVerified */ __('Status') ?></th> <th scope="col" class="col actions"><?= /* @escapeNotVerified */ __('Action') ?></th> <th scope="col" class="col actions">Track Order</th> </tr> </thead> <tbody> <?php foreach ($_orders as $_order): ?> <tr> <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id"><?= /* @escapeNotVerified */ $_order->getRealOrderId() ?></td> <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= /* @escapeNotVerified */ $block->formatDate($_order->getCreatedAt()) ?></td> <?php $extra = $block->getChildBlock('extra.container'); ?> <?php if ($extra): ?> <?php $extra->setOrder($_order); ?> <?= /* @noEscape */ $extra->getChildHtml() ?> <?php endif; ?> <td data-th="<?= $block->escapeHtml(__('Ship To')) ?>" class="col shipping"><?= $_order->getShippingAddress() ? $block->escapeHtml($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td> <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>" class="col total"><?= /* @escapeNotVerified */ $_order->formatPrice($_order->getGrandTotal()) ?></td> <td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="col status"><?= /* @escapeNotVerified */ $_order->getStatusLabel() ?></td> <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions"> <a href="<?= /* @escapeNotVerified */ $block->getViewUrl($_order) ?>" class="action view"> <span><?= /* @escapeNotVerified */ __('View Order') ?></span> </a> <?php if ($this->helper('Magento\Sales\Helper\Reorder')->canReorder($_order->getEntityId())) : ?> <a href="#" data-post='<?php /* @escapeNotVerified */ echo $this->helper(\Magento\Framework\Data\Helper\PostHelper::class) ->getPostData($block->getReorderUrl($_order)) ?>' class="action order"> <span><?= /* @escapeNotVerified */ __('Reorder') ?></span> </a> <?php endif ?> </td> <td class="col actions"> <a href="#" class="action track" title="Track Your Order" data-mage-init='{"popupWindow": {"windowURL":"<?= $this->helper('\Magento\Shipping\Helper\Data')->getTrackingPopupUrlBySalesModel($_order); ?>","windowName":"trackorder","width":800,"height":600,"left":0,"top":0,"resizable":1,"scrollbars":1}}'> <span>Track Your Order</span> </a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php if ($block->getPagerHtml()): ?> <div class="order-products-toolbar toolbar bottom"><?= $block->getPagerHtml() ?></div> <?php endif ?> <?php else: ?> <div class="message info empty"><span><?= /* @escapeNotVerified */ __('You have placed no orders.') ?></span></div> <?php endif ?></pre><p>更改后刷新缓存。</p><p>通过上述代码的实现,跟踪订单列链接将添加到订单主页,如下所示:</p><p><img src="/uploads/images/20230831/cb8ee6b6bb468b2513ae2c386b646a47.png" title="a.png" alt="" width="777" height="354"/></p><p>跟踪顺序</p><p>结论:</p><p>期待的是,所有人都能够 添加跟踪订单 链接与Magento 2中的订单历史记录页面上的订单。除此之外,使用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:为此,请在app\Code\Vendor\Extension\etc\文件夹中创建events.xml文件,然后添加以下代码:</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:Event/etc/events.xsd"> <event name="layout_generate_blocks_after"> <observer instance="Vendor\Extension\Observer\Productcount" name="Vendor\Extension\Observer\Productcount"/> </event> </config></pre><p>第 2 步:接下来,在app\Code\Vendor\Extension\Observer\文件夹中创建Productcount.php并添加以下代码:</p><pre class="brush:bash;toolbar:false"><?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class Productcount implements ObserverInterface { public function execute(Observer $observer) { if ($observer->getEvent()->getData('full_action_name') === 'catalog_category_view') { $layout = $observer->getEvent()->getData('layout'); $listBlock = $layout->getBlock('category.products.list'); $categoryViewBlock = $layout->getBlock('category.products'); $count = 0; if ($listBlock) { $productCollection = $listBlock->getLoadedProductCollection(); if ($productCollection && $productCollection->count() >0) { $count = $productCollection->count(); } } if ($categoryViewBlock) { $currentCategory = $categoryViewBlock->getCurrentCategory(); if ($currentCategory) { $count = $currentCategory->getProductCollection()->count(); } } $pageMainTitle = $layout->getBlock('page.main.title'); if ($pageMainTitle) { $oldTitle =$pageMainTitle->getPageTitle(); $pageMainTitle->setPageTitle( $oldTitle . _(' - Product Count: ' . (int) $count) ); } } } }</pre><p>步骤3:最后运行以下命令:</p><pre class="brush:bash;toolbar:false">php bin/magento cache:flush</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中以编程方式取消订单而不禁用URL安全密钥的步骤:</p><p>步骤 1:导航到以下路径:</p><p>app/code/VENDOR/EXTENSION/Controller/Adminhtml/Order/Cancel.php</p><p>并添加以下代码:</p><pre class="brush:bash;toolbar:false">namespace VENDOR\EXTENSION\Controller\Adminhtml\Order; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Sales\Controller\Adminhtml\Order\Cancel as CancelOrderController; class Cancel extends CancelOrderController implements HttpGetActionInterface { public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $order = $this->_initOrder(); if ($order) { try { $this->orderManagement->cancel($order->getEntityId()); $this->messageManager->addSuccessMessage(__('You canceled the order.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addErrorMessage(__('You have not canceled the item.')); $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e); } } return $resultRedirect->setPath('sales/order/index'); } }</pre><p>步骤2:现在,移至以下路径:</p><p>app/code/VENDOR/EXTENSION/view/adminhtml/ui_component/sales_order_grid.xml</p><p>现在,添加以下代码:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0" encoding="UTF-8"?> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <columns name="sales_order_columns"> <actionsColumn name="actions" class="VENDOR\EXTENSION\UI\Component\Listing\Column\Actions"/> </columns> </listing></pre><p>步骤3:接下来,导航到以下路径:</p><p>app/code/vendor/extension/ui/Component/Listing/Column/Actions.php</p><p>并添加以下代码:</p><pre class="brush:bash;toolbar:false">namespace VENDOR\EXTENSION\UI\Component\Listing\Column; use Magento\Sales\Ui\Component\Listing\Column\ViewAction; class Actions extends ViewAction { public function prepareDataSource(array $dataSource) { $dataSource = parent::prepareDataSource($dataSource); if (isset($dataSource['data']['items'])) { foreach ($dataSource['data']['items'] as & $item) { if (isset($item['entity_id'])) { $urlEntityParamName = $this->getData('config/urlEntityParamName') ?: 'entity_id'; $item[$this->getData('name')]['cancel'] = [ 'href' => $this->urlBuilder->getUrl( 'cancel/order/cancel', [ $urlEntityParamName => $item['entity_id'] ] ), 'label' => __('Cancel') ]; } } } return $dataSource; } }</pre><p>步骤4:最后,导航到以下路径:</p><p>app/code/VENDOR/EXTENSION/etc/adminhtml/routes.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:App/etc/routes.xsd"> <router id="admin"> <route id="cancel" frontName="cancel"> <module name="VENDOR_EXTENSION" before="Magento_Sales" /> </route> </router> </config></pre><p>结论:</p><p>因此,希望每个人都能够以编程方式取消订单,而无需在Magento 2中禁用URL安全密钥。</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/view/frontend/layout/checkout_cart_index.xml</p><p>并添加以下代码:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magento\Checkout\Block\Onepage\Link" name="checkout.cart.methods.onepage.top" template="VENDOR_EXTENSION::onepage/link_top.phtml" before="checkout.cart" /> </referenceContainer> </body> </page></pre><p>步骤2:接下来,转到以下路径</p><p>app/code/VENDOR/EXTENSION/view/frontend/templates/onepage/link_top.phtml</p><p>并添加以下代码</p><p></p><pre class="brush:bash;toolbar:false"><?php /** @var $block \Magento\Checkout\Block\Onepage\Link */ ?> <?php if ($block->isPossibleOnepageCheckout()) :?> <button type="button" data-role="proceed-to-checkout" title="<?= $block->escapeHtmlAttr(__('Proceed to Checkout Top')) ?>" data-mage-init='{ "Magento_Checkout/js/proceed-to-checkout":{ "checkoutUrl":"<?= $block->escapeJs($block->escapeUrl($block->getCheckoutUrl())) ?>" } }' class="action primary checkout<?= ($block->isDisabled()) ? ' disabled' : '' ?>" <?php if ($block->isDisabled()) :?> disabled="disabled" <?php endif; ?>> <span><?= $block->escapeHtml(__('Proceed to Checkout Top')) ?></span> </button> <?php endif; ?></pre><p>步骤3:最后,运行以下命令</p><pre class="brush:bash;toolbar:false">php bin/magento cache:clean php bin/magento cache:flush</pre><p>结论:</p><p>这样,您可以轻松地在Magento 2的购物车页面上的网格顶部添加额外的“继续结帐”按钮。</p><p><br/><br/></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中使用REST API获取订单详细信息的步骤:</p><p>步骤1:在Magento根文件夹中创建getOrderDetails.php脚本文件并添加以下代码</p><p></p><pre class="brush:bash;toolbar:false"><?php $userData = array("username" => "admin", "password" => "adminPassword"); $baseUrl = "http://127.0.0.1/magento24/index.php"; // your magento base url $ch = curl_init($baseUrl."/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)))); $token = curl_exec($ch); $ch = curl_init($baseUrl."/rest/V1/orders/1"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); $result = json_decode($result, 1); echo '<pre>';print_r($result);?></pre><p>注意 - 您可以在Magento文件系统中的任何位置使用此代码。例如:您也可以在自定义模块中使用它。</p><p>结论:</p><p>通过这种方式,您可以使用Magento 2中的REST API获取订单详细信息。除此之外,您还可以使用Magento 2中的REST API获取订单状态。</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中使用默认curl类进行API调用的步骤:</p><p>步骤1:在以下路径中创建数据.php文件</p><p>Vendor\Extension\Helper\Data.php</p><p>并添加以下代码</p><pre class="brush:bash;toolbar:false"><?php namespace Vendor\Extension\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\HTTP\Client\Curl; /** * Class Data * Vendor\Extension\Helper */ class Data extends AbstractHelper { /** * Data constructor. * @param Curl $curl */ public function __construct( Curl $curl ) { $this->curl = $curl; } /** * @return string * @throws LocalizedException */ public function defualtCurl() { try { if ($this->isEnable()) { $postData = [ 'first_param' => 'first_value' ]; $this->curl->setOption(CURLOPT_SSL_VERIFYPEER, false); $this->curl->setOption(CURLOPT_SSL_VERIFYHOST, 2); // we can add all option which is we used for default PHP curl $this->curl->post($this->getApiUrl(), $postData); // Response Text $response = $this->curl->getBody(); // Response Header $responseHeader = $this->curl->getHeaders(); // Response cookies $responseCookies = $this->curl->getCookies(); return $response; } } catch (Exception $e) { $return = ["status"=>false, "message"=> $e->getMessage()]; } } }</pre><p>结论:</p><p>因此,可以在Magento 2中成功地使用默认的curl类进行API调用。</p><p><br/></p>