Skip to content

Instantly share code, notes, and snippets.

View 0-Sony's full-sized avatar

Phuong LÊ 0-Sony

View GitHub Profile
@0-Sony
0-Sony / UpdateCustomerAttributeForWebsite.php
Created August 24, 2023 10:15
Magento2 : Update Customer Attribute for specific Website with DataPatch
<?php
declare(strict_types=1);
namespace MyNamespace\MyModule\Setup\Patch\Data;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\Customer;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Setup\EavSetupFactory;
@0-Sony
0-Sony / createCompany.json
Created August 16, 2023 15:17
Magento 2 : Create Company via API (Postman example)
Method @POST : www.mydomaine.com/rest/{storeCode}/V1/company
{
"company": {
"status": 1,
"company_name": "MY COMPANY",
"company_email": "john@doe.com",
"street": [
"10 rue du test"
],
@0-Sony
0-Sony / UpdateProductAttribute.php
Created July 24, 2023 13:35
Magento 2 : Update Product Attribute from DataPatch
<?php
declare(strict_types=1);
namespace MyNamespace\MyModule\Setup\Patch\Data;
use Magento\Catalog\Api\Data\EavAttributeInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
@0-Sony
0-Sony / GetFileContentFromModule.php
Created July 6, 2023 10:14
Magento 2 : Get File Content from Specific Module
<?php
declare(strict_types=1);
namespace MyNamespace\MyModule\Setup\Patch\Data;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir;
@0-Sony
0-Sony / catalog_attributes.xml
Created March 13, 2023 10:21
Magento 2 : Make product attribute available from Quote Item
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
<group name="quote_item">
<attribute name="my_custom_attribute"/>
</group>
</config>
@0-Sony
0-Sony / add_price_all_products.sql
Last active September 12, 2022 12:43
Magento2: Add stock for all products / Add price for all products
#Let's find the attribute_id of price
SELECT attribute_id, backend_type
FROM `eav_attribute`
WHERE attribute_code = 'price' AND entity_type_id = 4;
# For instance the attribute_id returned is 77
# /!\ Warning /!\
# Community Edition : use `entity_id`
# Enterprise Edition : use `row_id`
@0-Sony
0-Sony / AddViewModelToBlock.php
Last active January 7, 2022 16:03
Magento2: Add ViewModel by using plugin
<?php
declare(strict_types=1);
namespace MyNamespace\MyModule\Plugin;
use Vendor\Module\Block\Any\Block as AnyBlock;
/**
* Class AddViewModelToBlock
@0-Sony
0-Sony / cms_page_view_selectable_about-us_AboutUs.xml
Created November 24, 2021 14:39
Magento2 : Add Custom Layout Update for a specific Cms Page
<?xml version="1.0" encoding="UTF-8" ?>
<!--
pattern xml file name : cms_page_view_selectable_<CMS Page Identifier>_<Layout Update Name>.xml
-->
<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="\Vendor\AboutUS\Block\Custom" name="vendor.about-us.custom" template="Vendor_AboutUs::custom-template.phtml" before="-"/>
</referenceContainer>
@0-Sony
0-Sony / di.xml
Created June 7, 2021 08:39
Magento2 : Add Custom Store Configuration exposed to graphQL
<!-- etc/graphql/di.xml -->
<?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\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
<arguments>
<argument name="extendedConfigData" xsi:type="array">
<item name="custom_config" xsi:type="string">mynamespace_mymodule/general/custom_config</item>
</argument>
@0-Sony
0-Sony / AttributeManagement.php
Last active August 11, 2023 12:14
Magento 2: change attribute type to Visual Swatch / Visual Text and add swatch options Ids
<?php
declare(strict_types=1);
namespace MyNamespace\Catalog\Model;
use Dnd\Catalog\Api\AttributeManagementInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeInterface;