Skip to content

Instantly share code, notes, and snippets.

View evgv's full-sized avatar
🏠
Working from home

Eugene Zubkov evgv

🏠
Working from home
View GitHub Profile
@evgv
evgv / mage_sql_get_product_text_attribute_by_code.md
Created August 29, 2016 09:43
Mage. SQL. Get product text attribute by code in example retrieve product short description.
SELECT cpet.entity_id, cpet.value, cpet.store_id, cpet.attribute_id FROM eav_attribute  AS ea
INNER JOIN  catalog_product_entity_text AS cpet ON cpet.attribute_id = ea.attribute_id
WHERE ea.attribute_code = 'short_description' 
AND ea.entity_type_id = 4
AND cpet.entity_id = 388
#GROUP BY cpet.entity_id
#LIMIT 3
@evgv
evgv / mage_get_all_block_on_page_names.md
Created August 29, 2016 13:18
Magento. Get all blocks on page and get all blocks names.
  // Get all blosk on page
  $blocks = Mage::app()->getLayout()->getAllBlocks();
  
  // Get all block on page names 
  $blockNames = array_keys($blocks);
@evgv
evgv / mage_checkout_object_and_session_object.md
Last active April 6, 2023 17:08
Magento. The Magento Checkout Object and Session Object.

The Magento Checkout Object and Session Object

The Magento Session object should be used when querying the current quote. Access it like so:

  $checkout = Mage::getSingleton('checkout/session')

The getQuote() method is responsible for retrieving the current quote. If a quote ID doesn't exist on the session object then a new quote object (Mage_Sales_Model_Quote) is created and set up with all of the necessary data (customer id, store id, remote ip etc).

If a product has never been added to the cart or the cart isn't being loaded from a previous session, it's likely the cart will never have been saved and will have no id. The cart gets saved when a product is added from the Checkout module's CartController (Mage_Checkout_CartController) in the addAction.

@evgv
evgv / mage_create_dropdown_countries_select.md
Created September 2, 2016 08:26
Magento. Create a drop-down of countries.

Create a drop-down of countries

When I first needed to access a collection of countries in Magento I assumed it would work like all other data collections but was shocked to find that this wasn't the case. Rather than store country data in the database, Magento stores country data in an XML file and loads it in on each request. Fortunately though, there are some simple functions that we can use to access country names and codes in Magento.

Get An Array of Country Names/Codes in Magento

  $countryList = Mage::getResourceModel('directory/country_collection')
  					->loadData()
  					->toOptionArray(false);
@evgv
evgv / mage_change_order_increment.markdown
Last active December 29, 2016 08:43
Magento. Change order increment.
  UPDATE `eav_entity_store` SET `increment_last_id` = {{NEW_LAST_INCREMENT_ID}} WHERE `entity_type_id` = {{ENTITY_TYPE_ID}} AND `store_id` = {{STORE_ID}};
  • {{NEW_LAST_INCREMENT_ID}} - any number what will be new last increment
  • {{ENTITY_TYPE_ID}} - entity type id from table eav_entity_type like order, invoice etc
  • {{STORE_ID}} - store id for what need change increment
@evgv
evgv / apache_htaccess_settings_for_load_fonts_on_subdomains.markdown
Last active September 8, 2016 11:57
Apache `.htaccess` settings for load fonts on subdomains.

Change your .htaccess file code on your website root directory (i.e. Your public_html ".htaccess" file)

############################################
## Allow fonts to load on subdomains.
<FilesMatch "(ttf | otf | woff | eot | svg). $">
    Header set Access-Control-Allow-Origin "*"
</FilesMatch>
@evgv
evgv / Vendor_ExtensionName_Helper_Data.php
Last active September 22, 2016 08:50
Magento. Get attribute options count for product.
<?php
class Vendor_ExtensionName_Helper_Data extends Mage_Core_Helper_Abstract
{
protected $_attributes;
protected $_options_count;
/**
* Retrieve product attribute options count
*
@evgv
evgv / mage_chek_if_product_has_options.md
Created September 22, 2016 15:46
Magento. Check if product has custom options and get product types.

Check if product has custom options

For simple and conigurable product types get true/false has options return 1 or 0:

  <?php if ( $_product->getData('has_options') ): ?>
    <!-- do something -->
  <?php endif; ?>

For flat category data option enabled

@evgv
evgv / php_sort_associated_array_by_value.md
Last active October 12, 2018 11:53
PHP. Sort associated array by value use anonymous function.

Sort associated array by value use anonymous function, sort by order field

  usort($data, function($a, $b) {return $a['order'] - $b['order'];});
@evgv
evgv / php_get_file_extension.md
Created September 23, 2016 08:28
PHP. Detects the file extension from a given string.

#Get File Extension

Detects the file extension from a given string. This function returns the text following the last dot of the file name, so more complicated file extensions such as .tar.gz are only returned as ".gz".

function file_ext($file) {
  $last_dot = strrpos($file, '.');
  if ($last_dot !== false) {
    return strtolower(substr($file, $last_dot + 1));
 }