Thursday, 14 February 2019

Get product from save event observer - Magento 2

Get product from save event observer - Magento 2

If you want to $productobj after saving product from backend then you can easily use catalog_product_save_after event.
I am assuming that you already know how to create a module in M2.
Put this events.xml in below path
app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml
<?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="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path
app\code\YOUR_NAMESPACE\YOURMODULE\Observer\


<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{   
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $_sku=$_product->getSku(); // for sku

    }  
}

Tuesday, 12 February 2019

How to search for product by name or sku with PageSize and CurPage programatically magento 1.9?

How to search for product by name or sku with PageSize and CurPage programatically magento 1.9?
$products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToFilter(
                        array(
                            array('attribute' => 'sku', 'like' => '%'.$search.'%'),
                            array('attribute' => 'name', 'like' => '%'.$search.'%')
                        )
                    )
                    ->setPageSize(10)->setCurPage(1)->load();

Tuesday, 15 January 2019

How to get instagram feed in custom page in magento

<?php
$fields = array(
'client_id' => "bdb4fd62e6e24f938fb7d124acf80187",
'client_secret' => "1cca64ded0e046118b311fdc001bb52c",
'grant_type' => "authorization_code",
'redirect_uri' => "http://wgi-mobile-dev-949798541.ap-south-1.elb.amazonaws.com/insta.php",
//'code' => $_GET['code'],
);
//1484074220.1677ed0.af32987ae8984bb59d9803afabc83e65
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/v1/users/self/media/recent/?access_token=1484074220.1677ed0.af32987ae8984bb59d9803afabc83e65');
curl_setopt($ch, CURLOPT_POST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$posts = curl_exec($ch);
curl_close($ch);
$posts = json_decode($posts);

if ($posts) {
foreach ($posts->data as $dat) {
echo '<img src="' . $dat->images->standard_resolution->url . '" style="width:250px;height:200px;">';
}
}
?>

instagram access token generator :- https://instagram.pixelunion.net/

Thursday, 29 November 2018

How do Delete wishlist item in magento 1.9 by programatically?

  $wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($userId);
            $wishListItemCollection = $wishList->getItemCollection();
            foreach ($wishListItemCollection as $item) {
               $w_product_id = $item->getProductId();
                if ($w_product_id==$product_id) {
                    $item->delete();
                   
                    echo 'Deleted successfully';
                }else{
                    echo 'Not Deleted';

                }

How do List to wishlist item in magento 1.9 by programatically?

$itemCollection = Mage::getModel('wishlist/item')->getCollection()->addCustomerIdFilter($customarId);
 foreach($itemCollection as $product) {
                   
                    $product_id = $product->getId(),
                   
                 
                
            }

Wednesday, 28 November 2018

How do I add to wishlist in magento 1.9 by programatically?

$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
$product = Mage::getModel('catalog/product')->load($productId);

$buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product

$result = $wishlist->addNewItem($product, $buyRequest);
$wishlist->save();

Friday, 23 November 2018

How to get list billing address and list shipping address by customer id in magento 1.9

$customerId = 2;
$customer = Mage::getModel('customer/customer')->load($customerId);
$defaultBilling  = $customer->getDefaultBilling();
$defaultShipping = $customer->getDefaultShipping();

$allAddress = Mage::getModel('customer/address')->getCollection()->setCustomerFilter($customer);

foreach ($allAddress as $address) {
    if($defaultBilling == $address->getId()) {
        // its customer default billing address
    } else if($defaultShipping == $address->getId()) {
        // its customer default shipping address
    } else {
        // its customer other address that saved
    }
}