Magento常用方法

magento2

获取后台block

1
2
3
4
5
// 在phtml中
<?= $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('id')->toHtml(); ?>

// 在后台中
## cms: {{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}

获取产品集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
// filter current website products
$collection->addWebsiteFilter();
// filter current store products
$collection->addStoreFilter();
// set visibility filter
$collection->setVisibility($objectManager->get('\Magento\Catalog\Model\Product\Visibility')->getVisibleInSiteIds());

// fetching only 8 products
$collection->setPageSize(8);

foreach ($collection as $product): ?>
<?= $product->getName() ?>
<!-- 获取图片, 此处的 base 可改为 thumbnail -->
<img src="<?= $this->helper('Magento\Catalog\Helper\Image')->init($product, 'product_base_image' )->getUrl() ?>">
<?php endforeach; ?>

获取分类集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollectionFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$collection = $categoryCollectionFactory->create() ->load(2) ->getChildrenCategories();
foreach ($collection as $category): ?>
<div class="item">
<span class="name"><?= $category->getName() ?></span>
</div>
<?php endforeach; ?>

<!-- 在任意页面获取当前分类 -->
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current product
echo $category ->getName();
?>

获取库存

1
2
3
4
5
<?php
// 您可以使用该方法获得数量或其他值(如最小数量)。
echo $_product->getExtensionAttributes()->getStockItem()->getQty();
// 或完整的对象数据
print_r($_product->getExtensionAttributes()->getStockItem()->getData());

magento1

获取指定分类下的商品

1
2
3
4
5
6
Mage::getModel('catalog/category') ->load('分类id')
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->setPageSize(8);

获取数据库配置

1
2
3
4
$dbname = (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');
$host = (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/host');
$user = (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/username');
$pwd = (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/password');

Facebook Purchase事件

1
2
3
4
5
6
fbq('track', 'Purchase',{
value:'<?= number_format($order->getGrandTotal(),2,".","");?>',
currency: '<?= Mage::app()->getStore()->getCurrentCurrencyCode(); ?>',
content_ids: '<?= $product_sku; ?>',
content_type: 'product',
});

创建后台用户

1
2
3
4
5
6
7
8
9
set foreign_key_checks = 0;
truncate admin_user;
insert into admin_user value(1, 'c', 'ccheng', '1434389213@qq.com', 'admin', '18d3eb456e992e7ed126ad9fa03215c2:i8IrEDA41LGVj7Pmrjg5KwPLlotwV3kr', '2019-07-05 07:01:04', '2019-07-05 07:01:04', '2019-07-05 07:24:35', 4, 0, 1, 'a:1:{s:11:"configState";a:7:{s:12:"dev_restrict";s:1:"0";s:9:"dev_debug";s:1:"1";s:12:"dev_template";s:1:"0";s:20:"dev_translate_inline";s:1:"0";s:7:"dev_log";s:1:"0";s:6:"dev_js";s:1:"0";s:7:"dev_css";s:1:"0";}}', null, null);

# 管理员账号
# admin
# 管理员密码
# admin123
# 18d3eb456e992e7ed126ad9fa03215c2:i8IrEDA41LGVj7Pmrjg5KwPLlotwV3kr

测试清除冗余数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
SET FOREIGN_KEY_CHECKS=0;

TRUNCATE `catalogsearch_fulltext`;
TRUNCATE `catalogrule_product_price`;
TRUNCATE `core_url_rewrite`;
TRUNCATE `catalogrule_product`;

ALTER TABLE `catalogsearch_fulltext` AUTO_INCREMENT=1;
ALTER TABLE `catalogrule_product_price` AUTO_INCREMENT=1;
ALTER TABLE `core_url_rewrite` AUTO_INCREMENT=1;
ALTER TABLE `catalogrule_product` AUTO_INCREMENT=1;

-- 清空所有订单
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_track`;

ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;

-- 清空所有用户
TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;
TRUNCATE `log_customer`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;

ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `log_customer` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;

-- 清空日志:
truncate dataflow_batch_export;
truncate dataflow_batch_import;
truncate log_customer;
truncate log_quote;
truncate log_summary;
truncate log_summary_type;

truncate log_url;
truncate log_url_info;
truncate log_visitor;
truncate log_visitor_info;
truncate log_visitor_online;

truncate report_viewed_product_index;
truncate report_compared_product_index;
truncate report_event;


-- 清空产品:
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;

TRUNCATE `catalogsearch_fulltext`;
TRUNCATE `catalogsearch_query`;
TRUNCATE `catalogsearch_result`;
truncate sales_bestsellers_aggregated_daily;
truncate sales_bestsellers_aggregated_monthly;
truncate sales_bestsellers_aggregated_yearly;

ALTER TABLE `catalog_product_bundle_option` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_option_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_selection` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_gallery` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_tier_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_int` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_varchar` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_type` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_title` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_title` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_attribute` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_attribute_label` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_attribute_pricing` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_link` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_enabled_index` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_website` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock_item` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock_status` AUTO_INCREMENT=1;

insert into `catalog_product_link_type`(`link_type_id`,`code`) values (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
insert into `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) values (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
insert into `cataloginventory_stock`(`stock_id`,`stock_name`) values (1,'Default');

-- 重置所有的 ID counters
TRUNCATE `eav_entity_store`;
ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;

truncate oc_block;
truncate oc_block_data;
truncate oc_position;
truncate sale_tmp_data;
truncate smtppro_email_log;
支付宝打赏 微信打赏

赞赏是不耍流氓的鼓励