Tuesday, December 12, 2017

Easy way to generate mod rewrites for .htaccess

Below is very useful tool for developers who want to play with  url rewrites. It converts the URLs to SEO friendly URLs. Using it in Magento is another thing :) you should be good enough in understanding URL structure and use it where it helps. Check it out.

http://www.generateit.net/mod-rewrite/index.php

Serialization of 'Magento\Framework\View\Layout\Element' is not allowed

It seems like a bug in Magento 2.1. EAV Attribute Cache mechanism is removed in Magento 2.2.0. Temporary solution will be disabling cache from backend or else we need to fix the module which is causing the issue which is not recommended way.

Link source: https://github.com/magento/magento2/issues/5339

Magento 2.2 commit link where the EAV cache mechanism is removed

https://github.com/magento/magento2/commit/3ae7c1909daad96d5a0ad6ab7048455448ba7480

Monday, December 11, 2017

How to Use Absolute Pricing for Custom Options in Magento 2 programatically


Here is the solution you can apply. It will work even if you have multiple custom options for a product.

create events.xml in the folder 'Namespace/Module/etc/frontend' and use the event 'checkout_cart_product_add_after'

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="checkout_cart_product_add_after">
            <observer name="customproductprice" instance="Namespace\Module\Observer\CustomProductPrice" />
        </event>
    </config>

Create CustomProductPrice.php in Observer folder.

    <?php
        namespace Namespace\Module\Observer;
   
        use Magento\Framework\Event\ObserverInterface;
        use Magento\Framework\App\RequestInterface;
   
        class CustomProductPrice implements ObserverInterface
        {
            protected $_objectManager;
            public function __construct(
                    \Magento\Framework\ObjectManagerInterface $objectManager
                ) {
                    $this->_objectManager = $objectManager;
                }
            public function execute(\Magento\Framework\Event\Observer $observer) {
                $item = $observer->getEvent()->getData('quote_item');       
                $product=$observer->getEvent()->getData('product');
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
                $_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($product);
                $totalPrice = 0;
                foreach ($_customOptions['options'] as $key => $value) {
                    $customOptions = $this->_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
                    foreach ($customOptions as $o) {
                        $values = $o->getValues();
                        foreach ($values as $v) {
                            if ($value['option_value'] == $v->getOptionTypeId()) {
                                $totalPrice += $v->getprice(); /* get price of custom option*/
                            }
                         }
                    }
                }
                $item->setCustomPrice($totalPrice);
                $item->setOriginalCustomPrice($totalPrice);
                $item->getProduct()->setIsSuperMode(true);
            }
   
        }