--------------------------------------------------------------------------------
/app/code/Lemundo/FeaturedProducts/data/lemundo_featuredproducts_setup/data-install-0.0.1.php:
--------------------------------------------------------------------------------
1 | startSetup();
7 |
8 | $installer->addAttribute(
9 | \Magento\Catalog\Model\Product::ENTITY,
10 | 'lemundo_featured_product',
11 | [
12 | 'type' => 'int',
13 | 'backend' => '',
14 | 'frontend' => '',
15 | 'label' => 'Is Featured Product',
16 | 'input' => 'select',
17 | 'class' => '',
18 | 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
19 | 'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,
20 | 'visible' => true,
21 | 'required' => false,
22 | 'user_defined' => true,
23 | 'default' => '0',
24 | 'searchable' => false,
25 | 'filterable' => false,
26 | 'comparable' => false,
27 | 'visible_on_front' => false,
28 | 'used_in_product_listing' => true,
29 | 'unique' => false,
30 | 'group' => 'Product Details'
31 | ]
32 | );
33 |
34 | $installer->endSetup();
--------------------------------------------------------------------------------
/app/code/Lemundo/FeaturedProducts/etc/view.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 | 1
11 | 50
12 | 75
13 | 85
14 | 135
15 | 56
16 | 265
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/code/Lemundo/FeaturedProducts/etc/adminhtml/system.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | separator-top
15 |
16 | lemundo
17 | Lemundo_FeaturedProducts::config_featured_products
18 |
19 |
20 |
21 |
22 | Number of products displayed
23 |
24 |
25 |
26 | Pixels
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation for mage2Modules/featuredProducts
2 |
3 | ### First words
4 |
5 | This module was created during our monthly hackathon at Lemundo (http://www.lemundo.de) to get comfortable with Magento2.
6 | Since Magento2 is still in the early beta, use it at your own risk. No guarantee from our side ;)
7 |
8 | ### Purpose of this module
9 |
10 | The module creates a new boolean product attribute which you can set to Yes/No at the product.
11 | All products which are set to Yes are featured products.
12 | They will be shown in a custom block of this module, which you can include within a CMS file using:
13 | ```{{block class="Lemundo\FeaturedProducts\Block\ProductList" template="list.phtml"}}```
14 | or in the XML via ``````
15 |
16 | ### Backend configuration
17 |
18 | There are two possible settings in the backend under the group "Featured Products" in the system configuration.
19 | You can set a limit of products which is the maximum number of featured products shown inside the block (they are randomized, Default: 3).
20 | And you can set the width of the product image in pixels (Default: 300).
21 |
22 | See screenshot:
23 |
24 | 
25 |
26 | When you edit a product, the new attribute will be visible in the "Product Details" group at the very bottom.
27 | By default it is set to No for all products. Set it to Yes for your featured products as shown in the screenshot and save the product:
28 |
29 | 
30 |
31 | ### Installation
32 |
33 | You can install this module in three ways:
34 |
35 | * a) Copy the folder app/ into your magento2 store
36 | * b) Using modman: modman init followed by modman clone /url/to/this/repository
37 | * c) Using composer: Add the module to your composer.json file
38 |
39 | After either of theses three ways, you need to activate the module by editing your app/etc/config.php file:
40 |
41 | ```
42 | ...
43 | 'modules' =>
44 | array (
45 | 'Lemundo_FeaturedProducts' => 1,
46 | ...
47 | ```
48 |
49 | Afterwards, make sure that Magento installs the new attribute by running: ```php setup/index.php update```
50 |
51 | ### Authors
52 |
53 | * Michael Timm
54 | * Marc Päpper (https://twitter.com/mpaepper)
55 |
--------------------------------------------------------------------------------
/app/code/Lemundo/FeaturedProducts/Block/ProductList.php:
--------------------------------------------------------------------------------
1 | _objectManager = \Magento\Framework\App\ObjectManager::getInstance();
53 | $this->_collection = $collection;
54 | $this->_config = $scopeConfig->getValue('featured_products/settings');
55 | $this->_imageHelper = $imageHelper;
56 |
57 | parent::__construct($context, $data);
58 | }
59 |
60 |
61 | /**
62 | * Get product collection
63 | */
64 | public function getProducts() {
65 | $limit = $this->getProductLimit();
66 |
67 | $collection = $this->_collection
68 | ->addMinimalPrice()
69 | ->addFinalPrice()
70 | ->addTaxPercents()
71 | ->addAttributeToSelect('name')
72 | ->addAttributeToSelect('image')
73 | ->addAttributeToFilter('is_saleable', 1, 'left')
74 | ->addAttributeToFilter('lemundo_featured_product', 1, 'left');
75 |
76 | $collection->getSelect()
77 | ->order('rand()')
78 | ->limit($limit);
79 |
80 | return $collection;
81 | }
82 |
83 |
84 | /**
85 | * Get image helper
86 | */
87 | public function getImageHelper() {
88 | return $this->_imageHelper;
89 | }
90 |
91 |
92 | /**
93 | * Get module configuration
94 | */
95 | public function getConfig() {
96 | return $this->_config;
97 | }
98 |
99 | /**
100 | * Get the configured limit of products
101 | * @return int
102 | */
103 | public function getProductLimit() {
104 | return $this->_config["limit"];
105 | }
106 |
107 | /**
108 | * Get the configured width of images
109 | * @return int
110 | */
111 | public function getImageWidth() {
112 | return $this->_config["image_width"];
113 | }
114 |
115 | }
--------------------------------------------------------------------------------