Last active
June 28, 2022 12:56
-
-
Save zaheerbadi/07695044acd44c3b98119e974cffd3fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################### Remove definer from sql file ########################## | |
sed 's/\sDEFINER=`[^`]*`@`[^`]*`//g' -i oldfile.sql | |
#################### Displya new product using widget xml (Mage2.1) #################### | |
<referenceContainer name="page.bottom.container"> | |
<block after="-" class="Magento\Catalog\Block\Product\Widget\NewWidget" name="new_product_blog" template="Magento_Catalog::product/widget/new/content/new_grid.phtml"> | |
<arguments> | |
<argument name="uiComponent" xsi:type="string">widget_new_products</argument> | |
<argument name="page_size" xsi:type="number">10</argument> | |
<!-- 'Product attributes to show' configuration --> | |
<argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument> | |
<!-- 'Buttons to show' configuration --> | |
<argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument> | |
</arguments> | |
</block> | |
</referenceContainer> | |
############################# Display Recently viewd product widget Xml (Mage2.1) ######################### | |
<referenceContainer name="page.bottom.container"> | |
<block after="-" class="Magento\Reports\Block\Product\Widget\Viewed" name="recently_viewed" template="Magento_Reports::widget/viewed/content/viewed_grid_slider.phtml"> | |
<arguments> | |
<argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument> | |
<argument name="page_size" xsi:type="number">10</argument> | |
<!-- 'Product attributes to show' configuration --> | |
<argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument> | |
<!-- 'Buttons to show' configuration --> | |
<argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument> | |
</arguments> | |
</block> | |
</referenceContainer> | |
####################################### Sample Data install after magento2 install ################ | |
php bin/magento sampledata:deploy | |
####################################### Install magento with command line ###################### | |
php bin/magento setup:install --base-url="http://127.0.0.1/mage227/" --db-host="localhost" --db-name="mage227" --db-user="root" --db-password="" --admin-firstname="zaheer" --admin-lastname="badi" --admin-email="[email protected]" --admin-user="admin" --admin-password="kgn@123" --language="en_GB" --currency="GBP" --timezone="Europe/London" --use-rewrites="1" --backend-frontname="admin" | |
################################################################################################## | |
###################### Get price inc and excl price ############## | |
$excPrice = $childProduct->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount(); | |
$inclPrice = $childProduct->getPriceInfo()->getPrice('final_price')->getAmount()->getValue(); | |
############################################################################# | |
########################## Check customer is logged in even if FPC is enable ############## | |
use: Magento\Framework\App\Http\Context::getValue(Magento\Customer\Model\Context::CONTEXT_AUTH) | |
to see if the current user is logged in. This should work even when you have FPC enabled. | |
############################################################################################ | |
#################### Use block in phtml using DI ######################### | |
You can create a new class (or Service Contract) and add it to the $data-array using di.xml: | |
<type name="Vendor\Module\Block\Example"> | |
<arguments> | |
<argument name="data" xsi:type="array"> | |
<item name="my-custom-stuff" xsi:type="object">Vendor\AnotherModule\Block\Example</item> | |
</argument> | |
</arguments> | |
</type> | |
The above code adds a new instance of Vendor\AnotherModule\Block\Example to your | |
(overwritten) template file. In that file you can access your block class like so: | |
/** @var \Vendor\Module\Block\Example $block */ | |
/** @var \Vendor\AnotherModule\Block\Example $myCustomStuff */ | |
$myCustomStuff = $block->getData(‘my-custom-stuff’); | |
############################ Set collection to Magento Listing ################################### | |
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\ListProduct")->setCollection($categoryProducts)->setTemplate("Magento_Catalog::product/homelist.phtml")->toHtml();?> | |
##################################### Check is customer logged in ###################################### | |
$om = \Magento\Framework\App\ObjectManager::getInstance(); | |
/** @var \Magento\Framework\App\Http\Context $context */ | |
$context = $om->get('Magento\Framework\App\Http\Context'); | |
/** @var bool $isLoggedIn */ | |
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); | |
if($isLoggedIn == 1){ | |
//Customer is Logged In | |
} | |
##################################### Ajax form submit ######################### | |
<script type="text/javascript"> | |
require([ | |
"jquery", | |
"mage/mage" | |
],function($) { | |
$(document).ready(function() { | |
$('#custom-contact-form').mage( | |
'validation', | |
{ | |
submitHandler: function(form) { | |
$.ajax({ | |
url: "<?php echo $baseurl.'contactdata/index/index'; ?>", | |
data: $('#custom-contact-form').serialize(), | |
type: 'POST', | |
dataType: 'json', | |
beforeSend: function() { | |
jQuery('.action.submit.primary').prop('disabled', 'disabled'); | |
jQuery('.action.submit.primary').html("Processing..."); | |
}, | |
success: function(data, status, xhr) { | |
jQuery('.success').fadeIn(200).show(); | |
jQuery('.action.submit.primary').html("Submit"); | |
jQuery('.action.submit.primary').removeAttr("disabled"); | |
if(data.status=='error'){ | |
jQuery('.message').addClass("error"); | |
jQuery('.messagetext').html(data.message); | |
}else{ | |
jQuery('.message').addClass("success"); | |
jQuery('.messagetext').html(data.message); | |
} | |
document.getElementById("custom-contact-form").reset(); | |
}, | |
error: function (xhr, status, errorThrown) { | |
jQuery('.message').addClass("error"); | |
jQuery('.messagetext').html(errorThrown); | |
document.getElementById("custom-contact-form").reset(); | |
jQuery('.action.submit.primary').html("Submit"); | |
jQuery('.action.submit.primary').removeAttr("disabled"); | |
} | |
}); | |
} | |
} | |
); | |
}); | |
}); | |
</script> | |
################################### Substract string with HTML ################################ | |
function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) { | |
if (is_array($ending)) { | |
extract($ending); | |
} | |
if ($considerHtml) { | |
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { | |
return $text; | |
} | |
$totalLength = mb_strlen($ending); | |
$openTags = array(); | |
$truncate = ''; | |
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); | |
foreach ($tags as $tag) { | |
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) { | |
if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) { | |
array_unshift($openTags, $tag[2]); | |
} else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { | |
$pos = array_search($closeTag[1], $openTags); | |
if ($pos !== false) { | |
array_splice($openTags, $pos, 1); | |
} | |
} | |
} | |
$truncate .= $tag[1]; | |
$contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3])); | |
if ($contentLength + $totalLength > $length) { | |
$left = $length - $totalLength; | |
$entitiesLength = 0; | |
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) { | |
foreach ($entities[0] as $entity) { | |
if ($entity[1] + 1 - $entitiesLength <= $left) { | |
$left--; | |
$entitiesLength += mb_strlen($entity[0]); | |
} else { | |
break; | |
} | |
} | |
} | |
$truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength); | |
break; | |
} else { | |
$truncate .= $tag[3]; | |
$totalLength += $contentLength; | |
} | |
if ($totalLength >= $length) { | |
break; | |
} | |
} | |
} else { | |
if (mb_strlen($text) <= $length) { | |
return $text; | |
} else { | |
$truncate = mb_substr($text, 0, $length - strlen($ending)); | |
} | |
} | |
if (!$exact) { | |
$spacepos = mb_strrpos($truncate, ' '); | |
if (isset($spacepos)) { | |
if ($considerHtml) { | |
$bits = mb_substr($truncate, $spacepos); | |
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER); | |
if (!empty($droppedTags)) { | |
foreach ($droppedTags as $closingTag) { | |
if (!in_array($closingTag[1], $openTags)) { | |
array_unshift($openTags, $closingTag[1]); | |
} | |
} | |
} | |
} | |
$truncate = mb_substr($truncate, 0, $spacepos); | |
} | |
} | |
$truncate .= $ending; | |
if ($considerHtml) { | |
foreach ($openTags as $tag) { | |
$truncate .= '</'.$tag.'>'; | |
} | |
} | |
return $truncate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment