Guest User

Untitled

a guest
May 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.79 KB | None | 0 0
  1. # Common #
  2.  
  3. ## Magento : Get Base Url , Skin Url , Media Url , Js Url , Store Url and Current Url ##
  4.  
  5. ### Get Url in phtml files ###
  6.  
  7. 1. Get Base Url :
  8.  
  9. ```php
  10. Mage::getBaseUrl();
  11. ```
  12.  
  13. 2. Get Skin Url :
  14.  
  15. ```php
  16. Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
  17. ```
  18.  
  19. (a) Unsecure Skin Url :
  20.  
  21. ```php
  22. $this->getSkinUrl('images/imagename.jpg');
  23. ```
  24.  
  25. (b) Secure Skin Url :
  26.  
  27. ```php
  28. $this->getSkinUrl('images/imagename.gif', array('_secure'=>true));
  29. ```
  30. 3. Get Media Url :
  31.  
  32. ```php
  33. Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
  34. ```
  35. 4. Get Js Url :
  36.  
  37. ```php
  38. Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
  39. ```
  40. 5. Get Store Url :
  41.  
  42. ```php
  43. Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
  44. ```
  45. 6. Get Current Url
  46.  
  47. ```php
  48. Mage::helper('core/url')->getCurrentUrl();
  49. ```
  50. ### Get Url in cms pages or static blocks ###
  51.  
  52. 1. Get Base Url :
  53.  
  54. ```php
  55. {{store url=""}}
  56. ```
  57.  
  58. 2. Get Skin Url :
  59. ```php
  60. {{skin url='images/imagename.jpg'}}
  61. ```
  62.  
  63. 3. Get Media Url :
  64.  
  65. ```php
  66. {{media url='/imagename.jpg'}}
  67. ```
  68. 4. Get Store Url :
  69. ```php
  70. {{store url='mypage.html'}}
  71. ```
  72.  
  73. ## Run Magento Code Externally ##
  74.  
  75. ```php
  76. <?php
  77. require_once('app/Mage.php'); //Path to Magento
  78. umask(0);
  79. Mage::app();
  80. // Run you code here
  81. ?>
  82. ```
  83.  
  84. ## $_GET, $_POST & $_REQUEST Variables ##
  85. ```php
  86. <?php
  87.  
  88. // get all parameters
  89. $allParams = Mage::app()->getRequest()->getParams();
  90. // $_GET
  91. $productId = Mage::app()->getRequest()->getParam('product_id');
  92. // The second parameter to getParam allows you to set a default value which is returned if the GET value isn't set
  93. $productId = Mage::app()->getRequest()->getParam('product_id', 44);
  94. $postData = Mage::app()->getRequest()->getPost();
  95. // You can access individual variables like...
  96. $productId = $postData['product_id']);
  97. ?>
  98. ```
  99.  
  100. ## Magento Custom Notification Message ##
  101.  
  102. ```php
  103. <?php
  104. ### Notice ###
  105. Mage::getSingleton('core/session')->addNotice('Notice message');
  106.  
  107. ### Success ###
  108. Mage::getSingleton('core/session')->addSuccess('Success message');
  109.  
  110. ### Error ###
  111. Mage::getSingleton('core/session')->addError('Error message');
  112.  
  113. ### Warning (admin only) ###
  114. Mage::getSingleton('adminhtml/session')->addWarning('Warning message');
  115. ?>
  116. ```
  117.  
  118. ## Get Currency Symbol ##
  119.  
  120. ```php
  121. <?php
  122. Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
  123. ?>
  124.  
  125. ```
  126. ## Format Price ##
  127.  
  128. ```php
  129. <?php
  130. $formattedPrice = Mage::helper('core')->currency($_finalPrice,true,false);
  131. ?>
  132. ```
  133. ## Get Currency Code ##
  134.  
  135. ```php
  136. <?php
  137. Mage::app()->getStore()->getCurrentCurrencyCode();
  138. ?>
  139. ```
  140.  
  141. ## Changing price from any one currency to another ##
  142.  
  143. ```php
  144. <?php
  145. $from = 'USD';
  146. $to = 'NPR';
  147. $price = 10;
  148.  
  149. $newPrice = Mage::helper('directory')->currencyConvert($price, $from, $to);
  150. ?>
  151. ```
  152.  
  153. ## Track Visitor’s Information ##
  154.  
  155. ```php
  156. <?php
  157. $visitorData = Mage::getSingleton('core/session')->getVisitorData();
  158. print_r($visitorData);
  159. ?>
  160. ```
  161.  
  162. ## Convert Price from Current Currency to Base Currency and vice-versa ##
  163.  
  164. ```php
  165. <?php
  166. $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
  167. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  168. $price = 100;
  169.  
  170. // convert price from current currency to base currency
  171. $priceOne = Mage::helper('directory')->currencyConvert($price, $currentCurrencyCode, $baseCurrencyCode);
  172.  
  173. // convert price from base currency to current currency
  174. $priceTwo = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
  175. ?>
  176. ```
  177.  
  178. ## Magento Session ##
  179.  
  180. set custom session in frontend
  181.  
  182. ```php
  183.  
  184. // To Set the Session
  185. Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
  186.  
  187. // To Get the Session
  188. $myValue = Mage::getSingleton('core/session')->getMySessionVariable();
  189. echo $myValue;
  190.  
  191. // To Unset the session
  192.  
  193. Mage::getSingleton('core/session')->unsMySessionVariable();
  194.  
  195. ```
  196. How to Use customer or core session in frontend.
  197. Use adminhtml session in the backend.
  198. ```php
  199. Core Session :- Mage::getSingleton('core/session');
  200. Customer Session :- Mage::getSingleton('customer/session');
  201. Admin Session :- Mage::getSingleton('adminhtml/session');
  202. Shopping Cart Session :-Mage::getSingleton('checkout/session')->getQuote();
  203. ```
  204.  
  205. ## Working with Collection ##
  206.  
  207. ```php
  208. $_products = Mage::getModel('catalog/product')->getCollection();
  209. ```
  210.  
  211. For EAV Tables
  212.  
  213. ```php
  214. ## Equals: eq ##
  215.  
  216. $_products->addAttributeToFilter('status', array('eq' => 1));
  217.  
  218. ## Not Equals - neq ##
  219.  
  220. $_products->addAttributeToFilter('sku', array('neq' => 'test-product'));
  221.  
  222. ## Like - like ##
  223.  
  224. $_products->addAttributeToFilter('sku', array('like' => 'UX%'));
  225.  
  226. One thing to note about like is that you can include SQL wildcard characters such as the percent sign.
  227.  
  228. ## Not Like - nlike ##
  229.  
  230. $_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
  231.  
  232. ## In - in ##
  233.  
  234. $_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
  235.  
  236. When using in, the value parameter accepts an array of values.
  237.  
  238. ## Not In - nin ##
  239.  
  240. $_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
  241.  
  242. ## NULL - null ##
  243.  
  244. $_products->addAttributeToFilter('description', 'null');
  245.  
  246. ## Not NULL - notnull ##
  247.  
  248. $_products->addAttributeToFilter('description', 'notnull');
  249.  
  250. ## Greater Than - gt ##
  251.  
  252. $_products->addAttributeToFilter('id', array('gt' => 5));
  253.  
  254. ## Less Than - lt ##
  255.  
  256. $_products->addAttributeToFilter('id', array('lt' => 5));
  257.  
  258. ## Greater Than or Equals To- gteq ##
  259.  
  260. $_products->addAttributeToFilter('id', array('gteq' => 5));
  261.  
  262. ## Less Than or Equals To - lteq ##
  263.  
  264. $_products->addAttributeToFilter('id', array('lteq' => 5));
  265.  
  266. ```
  267.  
  268. For Single Tables
  269.  
  270. ```php
  271. //for AND
  272. $collection = Mage::getModel('sales/order')->getCollection()
  273. ->addAttributeToSelect('*')
  274. ->addFieldToFilter('my_field1', 'my_value1')
  275. ->addFieldToFilter('my_field2', 'my_value2');
  276.  
  277. echo $collection->getSelect()->__toString();
  278.  
  279. //for OR - please note 'attribute' is the key name and must remain the same, only replace //the value (my_field1, my_field2) with your attribute name
  280.  
  281.  
  282. $collection = Mage::getModel('sales/order')->getCollection()
  283. ->addAttributeToSelect('*')
  284. ->addFieldToFilter(
  285. array(
  286. array('attribute'=>'my_field1','eq'=>'my_value1'),
  287. array('attribute'=>'my_field2', 'eq'=>'my_value2')
  288. )
  289. );
  290. ```
  291. ## Retrieve Magento Core Configuration variables on the Magento frontend ##
  292.  
  293. ```php
  294. Mage::getStoreConfig('myappgeneral/current_time/second', $store_id)
  295.  
  296. ```
  297.  
  298. ## Clear cache/reindex ##
  299. ```php
  300. <?php
  301. // clear cache
  302. Mage::app()->removeCache('catalog_rules_dirty');
  303. // reindex prices
  304. Mage::getModel('index/process')->load(2)->reindexEverything();
  305. /*
  306. 1 = Product Attributes
  307. 2 = Product Attributes
  308. 3 = Catalog URL Rewrites
  309. 4 = Product Flat Data
  310. 5 = Category Flat Data
  311. 6 = Category Products
  312. 7 = Catalog Search Index
  313. 8 = Tag Aggregation Data
  314. 9 = Stock Status
  315. */
  316. ?>
  317. ```
  318. ## Which page is this? – Magento ##
  319.  
  320. ### Detect Home Page – Magento ###
  321. ```php
  322. if($this->getIsHomePage())
  323. {
  324. // Home page
  325. }
  326. ```
  327. ### Detect Category Page – Magento ###
  328. ```php
  329. if (Mage::registry('current_category'))
  330. {
  331. // category page
  332. }
  333. ```
  334. ### Detect Category Page – Magento ###
  335. ```php
  336. if (Mage::registry('current_category'))
  337. {
  338. // Category Name
  339. echo Mage::registry('current_category')->getName();
  340.  
  341. // Category ID
  342. echo Mage::registry('current_category')->getId();
  343. }
  344. ```
  345. ### Detect CMS Page – Magento ###
  346. ```php
  347. if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
  348. {
  349. // CMS page
  350. }
  351. ```
  352.  
  353. ### Get CMS page name if current one is the CMS page. ###
  354. ```php
  355. if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
  356. {
  357. echo Mage::getSingleton('cms/page')->getIdentifier();
  358. }
  359. ```
  360.  
  361. ### Detect Product Detail Page – Magento ###
  362. ```php
  363. if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == 'configure')
  364. {
  365. // Product Configuration page
  366. }
  367. ```
  368. ### Detect Cart Page – Magento ###
  369. ```php
  370. $request = $this->getRequest();
  371. $module = $request->getModuleName();
  372. $controller = $request->getControllerName();
  373. $action = $request->getActionName();
  374. if($module == 'checkout' && $controller == 'cart' && $action == 'index')
  375. {
  376. //Cart Page
  377. }
  378. ```
  379.  
  380. # CMS Page #
  381.  
  382. ## Call a PHTML file within a CMS page Magento ##
  383.  
  384. ```php
  385. {{ block type="core/template" name="mycstompage" template="SkyMagento/mycustompage.phtml" }}
  386.  
  387. ```
  388.  
  389. ## Call cms static block within a CMS page Magento ##
  390.  
  391. ```php
  392. {{block type="cms/block" block_id="your_block_id"}}
  393. ```
  394.  
  395. ## Call Static Block in phtml ##
  396.  
  397. ```php
  398.  
  399. <?php echo $this->getLayout()
  400. ->createBlock('cms/block')
  401. ->setBlockId('your_block_id')->toHtml(); ?>
  402.  
  403. ```
  404.  
  405. ## Call static block using layout(XML) ##
  406. ```xml
  407. <default>
  408. <cms_page> <!-- need to be redefined for your needs -->
  409. <reference name="content">
  410. <block type="cms/block" name="cms_newest_product" as="cms_newest_product">
  411. <action method="setBlockId"><block_id>newest_product</block_id></action>
  412. </block>
  413. </reference>
  414. </cms_page>
  415. </default>
  416. ```
  417. In your phtml template call file as mention:
  418.  
  419. ```php
  420. echo $this->getChildHtml('newest_product');
  421. ```
  422.  
  423. ## Inject any block programmatically in any page any where ##
  424.  
  425. ```php
  426. <?php
  427. echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();
  428. ?>
  429. ```
  430.  
  431. ## Add block in CMS page using layout ##
  432.  
  433. go to design and change the layout what you want
  434.  
  435. to add left column in cms page just add belw code in layout
  436.  
  437. ```xml
  438. <reference name="left">
  439. <block type="cms/block" name="sample_block" before="-">
  440. <action method="setBlockId"><block_id>sample_block</block_id></action>
  441. </block>
  442. </reference>
  443. ```
  444.  
  445. to add right column in cms page just add belw code in layout
  446. ```xml
  447. <reference name="right">
  448. <block type="cms/block" name="sample_block" before="-">
  449. <action method="setBlockId"><block_id>sample_block</block_id></action>
  450. </block>
  451. </reference>
  452. ```
  453.  
  454. ## Displaying Products in a Static Block ##
  455. ```php
  456. {{block type="catalog/product_list" name="product_list" category_id="3" column_count="6" count="6"
  457. limit="4" mode="grid" template="catalog/product/list.phtml"}}
  458. ```
  459.  
  460. # Debugging #
  461.  
  462. ## Get methods of an object ##
  463.  
  464. First, use get_class to get the name of an object's class.
  465.  
  466. ```php
  467. <?php $class_name = get_class($object); ?>
  468. ```
  469.  
  470. Then, pass that get_class_methods to get a list of all the callable methods on an object
  471. ```php
  472. <?php
  473. $class_name = get_class($object);
  474. $methods = get_class_methods($class_name);
  475. foreach($methods as $method)
  476. {
  477. var_dump($method);
  478. }
  479. ?>
  480. ```
  481.  
  482. ## Log to custom file ##
  483.  
  484. ```php
  485.  
  486. <?php Mage::log('Your Log Message', Zend_Log::INFO, 'your_log_file.log'); ?>
  487.  
  488. ```
  489.  
  490. ## Print Query of current collection ##
  491.  
  492. ```php
  493.  
  494. <?php
  495. $collection = Mage::getModel('modulename/modelname')->getCollection();
  496. echo $collection->printLogQuery(true);
  497. echo $collection->getSelect();
  498. ?>
  499.  
  500. ```
  501. ## Debug using zend ##
  502.  
  503. ```php
  504. <?php Zend_Debug::dump($thing_to_debug, 'debug'); ?>
  505.  
  506. ```
  507. ## Try catch ##
  508. ```php
  509. try {
  510. // code to run
  511.  
  512. } catch (Exception $e) {
  513. var_dump($e->getMessage());
  514. }
  515. ```
  516. # Customer #
  517.  
  518. ## Check if customer is logged in ##
  519.  
  520. ```php
  521. $_customer = Mage::getSingleton('customer/session')->isLoggedIn();
  522. if ($_customer) {}
  523.  
  524. ```
  525.  
  526. ## Get Customer Shipping/Billing Address ##
  527.  
  528. ```php
  529. <?php
  530. $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
  531. if ($customerAddressId){
  532. $address = Mage::getModel('customer/address')->load($customerAddressId);
  533. }
  534. ?>
  535.  
  536. ```
  537.  
  538. ## Get Details of logged in user in Magento ##
  539.  
  540. ```php
  541. $customer = Mage::getSingleton('customer/session')->getCustomer();
  542. $customer->getPrefix();
  543. $customer->getName(); // Full Name
  544. $customer->getFirstname(); // First Name
  545. $customer->getMiddlename(); // Middle Name
  546. $customer->getLastname(); // Last Name
  547. $customer->getSuffix();
  548.  
  549. // All other customer data
  550. $customer->getWebsiteId(); // ID
  551. $customer->getEntityId(); // ID
  552. $customer->getEntityTypeId(); // ID
  553. $customer->getAttributeSetId(); // ID
  554. $customer->getEmail();
  555. $customer->getGroupId(); // ID
  556. $customer->getStoreId(); // ID
  557. $customer->getCreatedAt(); // yyyy-mm-ddThh:mm:ss+01:00
  558. $customer->getUpdatedAt(); // yyyy-mm-dd hh:mm:ss
  559. $customer->getIsActive(); // 1
  560. $customer->getDisableAutoGroupChange();
  561. $customer->getTaxvat();
  562. $customer->getPasswordHash();
  563. $customer->getCreatedIn(); // Admin
  564. $customer->getGender(); // ID
  565. $customer->getDefaultBilling(); // ID
  566. $customer->getDefaultShipping(); // ID
  567. $customer->getDob(); // yyyy-mm-dd hh:mm:ss
  568. $customer->getTaxClassId(); // ID
  569. ```
  570.  
  571. ## Get logged in customer id and email ##
  572. ```php
  573. $customer = Mage::getSingleton('customer/session')->getCustomer();
  574. $customer_id = $customer->getId();
  575. $customer_email = $customer->getEmail();
  576. ```
  577.  
  578.  
  579. ## Programmatically adding new customers to the Magento store ##
  580.  
  581. ```php
  582. $websiteId = Mage::app()->getWebsite()->getId();
  583. $store = Mage::app()->getStore();
  584.  
  585. $customer = Mage::getModel("customer/customer");
  586. $customer ->setWebsiteId($websiteId)
  587. ->setStore($store)
  588. ->setFirstname('John')
  589. ->setLastname('Doe')
  590. ->setEmail('jd1@ex.com')
  591. ->setPassword('somepassword');
  592.  
  593. try{
  594. $customer->save();
  595. }
  596. catch (Exception $e) {
  597. Zend_Debug::dump($e->getMessage());
  598. }
  599. ```
  600. # Product Page #
  601.  
  602. ## Load product by id or sku ##
  603.  
  604. ```php
  605. <?php
  606. $_product_1 = Mage::getModel('catalog/product')->load(12);
  607. $_product_2 = Mage::getModel('catalog/product')->loadByAttribute('sku','cordoba-classic-6-String-guitar');
  608. ?>
  609.  
  610. ```
  611. ## Get the current category/product/cms page ##
  612. ```php
  613. <?php
  614. $currentCategory = Mage::registry('current_category');
  615. $currentProduct = Mage::registry('current_product');
  616. $currentCmsPage = Mage::registry('cms_page');
  617. ?>
  618. ```
  619.  
  620. ## Is product purchasable? ##
  621.  
  622. ```php
  623. <?php if($_product->isSaleable()) { // do stuff } ?>
  624.  
  625. ```
  626.  
  627.  
  628. ## Get Product URL In Magento ##
  629.  
  630. ```php
  631. <?php
  632. $_product = Mage::getModel('catalog/product')->load(4);
  633. echo $_product->getProductUrl();
  634. ?>
  635. ```
  636.  
  637. ## Get Stock of Product in Magento ##
  638.  
  639. ```php
  640. <?php
  641. $_product = Mage::getModel('catalog/product')->load(4);
  642. $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
  643. echo $stock->getQty();
  644. echo $stock->getMinQty();
  645. echo $stock->getMinSaleQty();
  646.  
  647. ?>
  648. ```
  649. ## Getting Configurable Attributes (Super Attributes) of a Configurable Product ##
  650.  
  651. ```php
  652. <?php
  653. /**
  654. * Load Product you want to get Super attributes of
  655. */
  656. $product=Mage::getModel("catalog/product")->load(52520);
  657.  
  658. /**
  659. * Get Configurable Type Product Instace and get Configurable attributes collection
  660. */
  661. $configurableAttributeCollection=$product->getTypeInstance()->getConfigurableAttributes();
  662.  
  663. /**
  664. * Use the collection to get the desired values of attribute
  665. */
  666. foreach($configurableAttributeCollection as $attribute){
  667. echo "Attr-Code:".$attribute->getProductAttribute()->getAttributeCode()."<br/>";
  668. echo "Attr-Label:".$attribute->getProductAttribute()->getFrontend()->getLabel()."<br/>";
  669. echo "Attr-Id:".$attribute->getProductAttribute()->getId()."<br/>";
  670. var_dump($_attribute->debug()); // returns the set of values you can use the get magic method on
  671. }
  672. ?>
  673. ```
  674.  
  675.  
  676. ## Load Products by Category ID ##
  677.  
  678. ```php
  679. <?php
  680. $_category = Mage::getModel('catalog/category')->load(47);
  681. $_productCollection = $_category->getProductCollection();
  682. if($_productCollection->count()) {
  683. foreach( $_productCollection as $_product ):
  684. echo $_product->getProductUrl();
  685. echo $this->getPriceHtml($_product, true);
  686. echo $this->htmlEscape($_product->getName());
  687. endforeach;
  688. }
  689. ?>
  690. ```
  691.  
  692. ## Create a Country Drop Down in the Frontend of Magento ##
  693.  
  694. ```php
  695. <?php
  696. $_countries = Mage::getResourceModel('directory/country_collection')
  697. ->loadData()
  698. ->toOptionArray(false) ?>
  699. <?php if (count($_countries) > 0): ?>
  700. <select name="country" id="country">
  701. <option value="">-- Please Select --</option>
  702. <?php foreach($_countries as $_country): ?>
  703. <option value="<?php echo $_country['value'] ?>">
  704. <?php echo $_country['label'] ?>
  705. </option>
  706. <?php endforeach; ?>
  707. </select>
  708. <?php endif; ?>
  709.  
  710. ```
  711.  
  712. ## Get product image ##
  713. ```php
  714. <?php
  715. $_product = Mage::getModel('catalog/product')->load(4);
  716. // input is $_product and result is iterating child products
  717. $this->helper('catalog/image')->init($_product, 'image');
  718. ?>
  719.  
  720. ```
  721.  
  722. ## Get actual price and special price of a product ##
  723.  
  724. ```php
  725. <?php
  726. $_productId = 52;
  727. $_product = Mage::getModel('catalog/product')->load($_productId);
  728.  
  729. // without currency sign
  730. $_actualPrice = number_format($_product->getPrice(), 2);
  731. // with currency sign
  732. $_formattedActualPrice = Mage::helper('core')->currency(number_format($_product->getPrice(), 2),true,false);
  733.  
  734. // without currency sign
  735. $_specialPrice = $_product->getFinalPrice();
  736. // with currency sign
  737. $_formattedSpecialPrice = Mage::helper('core')->currency(number_format($_product->getFinalPrice(), 2),true,false);
  738. ?>
  739. ```
  740.  
  741. ## Downsize large product images but not enlarge small images ##
  742.  
  743. ```php
  744. <?php
  745. $this->helper('catalog/image')
  746. ->init($_product, 'image')
  747. ->keepFrame(false) // avoids getting the small image in original size on a solid background color presented (can be handy not to break some layouts)
  748. ->constrainOnly(true) // avoids getting small images bigger
  749. ->resize(650); // sets the desired width and the height accordingly (proportional by default)
  750. ?>
  751.  
  752. ```
  753.  
  754. ## Add block after add to cart in magento using layout ##
  755.  
  756. ```xml
  757. <catalog_product_view>
  758.  
  759. <reference name="product.info.container1" after="-">
  760. <action method="setTemplate">
  761. <template>pincode/index.phtml</template>
  762. </action>
  763. <block type="catalog/product_view" name="original_addtocart"
  764. as="original_addtocart" template="pincode/index.phtml"/>
  765. </reference>
  766. </catalog_product_view>
  767.  
  768. ```
  769.  
  770. ## Get Configurable product's Child products ##
  771.  
  772. ```php
  773. <?php
  774. // input is $_product and result is iterating child products
  775. $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
  776. ?>
  777.  
  778. ```
  779.  
  780. ## Return Product Attributes ##
  781.  
  782. ```php
  783. <?php
  784. $_product->getThisattribute();
  785.  
  786. $_product->getAttributeText('thisattribute');
  787.  
  788. $_product->getResource()->getAttribute('thisattribute')->getFrontend()->getValue($_product);
  789.  
  790. $_product->getData('thisattribute');
  791.  
  792. // The following returns the option IDs for an attribute that is a multiple-select field:
  793. $_product->getData('color'); // i.e. 456,499
  794.  
  795. // The following returns the attribute object, and instance of Mage_Catalog_Model_Resource_Eav_Attribute:
  796. $_product->getResource()->getAttribute('color'); // instance of Mage_Catalog_Model_Resource_Eav_Attribute
  797.  
  798. // The following returns an array of the text values for the attribute:
  799. $_product->getAttributeText('color') // Array([0]=>'red', [1]=>'green')
  800.  
  801. // The following returns the text for the attribute
  802. if ($attr = $_product->getResource()->getAttribute('color')):
  803. echo $attr->getFrontend()->getValue($_product); // will display: red, green
  804. endif;
  805. ?>
  806. ```
  807.  
  808. ## Get Parent product's by Child products ##
  809.  
  810. ```php
  811. <?php
  812. // pass the sku of the child product
  813. public function getParentproduct(int $sku){
  814.  
  815. $product = Mage::getModel('catalog/product');
  816. $productobject = $product->load($product->getIdBySku($sku));
  817. $ProductId = $product->getIdBySku($sku);
  818. $helperdata = Mage::helper("modulename/data");
  819.  
  820. if($productobject->getTypeId() == 'simple'){
  821. //product_type_grouped
  822. $parentIds = Mage::getModel('catalog/product_type_grouped')
  823. ->getParentIdsByChild($productobject->getId());
  824.  
  825.  
  826. //product_type_configurable
  827. if(!$parentIds){
  828. $parentIds = Mage::getModel('catalog/product_type_configurable')
  829. ->getParentIdsByChild($productobject->getId());
  830.  
  831. }
  832. //product_type_bundle
  833. if(!$parentIds){
  834. $parentIds = Mage::getModel('bundle/product_type')
  835. ->getParentIdsByChild($productobject->getId());
  836.  
  837. }
  838.  
  839. }
  840. return $parentIds;
  841. }
  842. ?>
  843.  
  844. ```
  845.  
  846. # Category Page #
  847.  
  848. ## Load category by id ##
  849.  
  850. ```php
  851. <?php
  852. $_category = Mage::getModel('catalog/category')->load(89);
  853. $_category_url = $_category->getUrl();
  854. ?>
  855. ```
  856.  
  857. ## Get The Root Category In Magento ##
  858.  
  859. ```php
  860. <?php
  861. $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
  862. $_category = Mage::getModel('catalog/category')->load($rootCategoryId);
  863. // You can then get all of the top level categories using:
  864. $_subcategories = $_category->getChildrenCategories();
  865. ?>
  866. ```
  867.  
  868. ## Get Category URL In Magento ##
  869.  
  870. ```php
  871. <?php
  872. $category = Mage::getModel('catalog/category')->load(4);
  873. echo $category->getUrl();
  874. ?>
  875. ```
  876.  
  877. ## Category Navigation Listings in Magento ##
  878.  
  879. Make sure the block that you’re working is of the type catalog/navigation. If you’re editing catalog/navigation/left.phtml then you should be okay.
  880. ```php
  881. <div id="leftnav">
  882. <?php $helper = $this->helper('catalog/category') ?>
  883. <?php $categories = $this->getStoreCategories() ?>
  884. <?php if (count($categories) > 0): ?>
  885. <ul id="leftnav-tree" class="level0">
  886. <?php foreach($categories as $category): ?>
  887. <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
  888. <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
  889. <?php if ($this->isCategoryActive($category)): ?>
  890. <?php $subcategories = $category->getChildren() ?>
  891. <?php if (count($subcategories) > 0): ?>
  892. <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
  893. <?php foreach($subcategories as $subcategory): ?>
  894. <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
  895. <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
  896. </li>
  897. <?php endforeach; ?>
  898. </ul>
  899. <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
  900. <?php endif; ?>
  901. <?php endif; ?>
  902. </li>
  903. <?php endforeach; ?>
  904. </ul>
  905. <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
  906. <?php endif; ?>
  907. </div>
  908. ```
  909.  
  910. # Cart Page #
  911.  
  912. ## Magento: Get Sales Quote & Order in both Frontend and Admin ##
  913.  
  914. ```php
  915.  
  916. ## Get Sales Quote in Frontend ##
  917.  
  918. Mage::getSingleton('checkout/session')->getQuote();
  919.  
  920. ## Get Sales Quote in Admin ##
  921.  
  922. $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();
  923.  
  924. ## Get Latest Order in Frontend ##
  925.  
  926. $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
  927. $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
  928.  
  929. ## Get Latest Order in Admin ##
  930.  
  931. $order = Mage::getModel('sales/order')->getCollection()
  932. ->setOrder('created_at','DESC')
  933. ->setPageSize(1)
  934. ->setCurPage(1)
  935. ->getFirstItem();
  936.  
  937.  
  938. ```
  939.  
  940. ## Get all items information in cart ##
  941.  
  942. ```php
  943.  
  944. $quote = Mage::getSingleton('checkout/session')->getQuote();
  945.  
  946. $items = $quote->getAllItems();
  947.  
  948. foreach($items as $item) {
  949. echo 'ID: '.$item->getProductId().'<br />';
  950. echo 'Name: '.$item->getName().'<br />';
  951. echo 'Sku: '.$item->getSku().'<br />';
  952. echo 'Quantity: '.$item->getQty().'<br />';
  953. echo 'Price: '.$item->getPrice().'<br />';
  954. echo "<br />";
  955. }
  956.  
  957. ```
  958.  
  959. ## Check if there are any items in cart or not ##
  960.  
  961. ```php
  962.  
  963. $totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount();
  964.  
  965. ```
  966.  
  967. ## Get First and/or Last item from collection ##
  968.  
  969. ```php
  970.  
  971. $firstItem = $collection->getFirstItem(); Mage::log($firstItem->getData());
  972. $lastItem = $collection->getLastItem(); Mage::log($lastItem->getData());
  973.  
  974. ```
  975.  
  976. ## Total items added in cart ##
  977.  
  978. ```php
  979. <?php
  980. Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
  981. Mage::getSingleton('checkout/session')->getQuote()->getItemsCount();
  982. ?>
  983.  
  984. ```
  985.  
  986. ## Total Quantity added in cart ##
  987.  
  988. ```php
  989. <?php
  990. Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
  991. Mage::getSingleton('checkout/session')->getQuote()->getItemsQty();
  992. ?>
  993.  
  994. ```
  995. ## Sub Total for item added in cart ##
  996.  
  997. ```php
  998. <?php
  999. Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
  1000. Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
  1001. ?>
  1002.  
  1003. ```
  1004. ## Grand total for item added in cart ##
  1005.  
  1006. ```php
  1007. <?php
  1008. Mage::helper('checkout')->formatPrice(Mage::getModel('checkout/cart')->getQuote()->getGrandTotal());
  1009. Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal());
  1010. ?>
  1011. ```
  1012. ## Sub total of cart inkl tax without shipping ##
  1013.  
  1014. ```php
  1015. <?php
  1016. $quote = Mage::getSingleton('checkout/session')->getQuote();
  1017. $items = $quote->getAllItems();
  1018. foreach ($items as $item) {
  1019. $priceInclVat += $item->getRowTotalInclTax();
  1020. }
  1021. Mage::helper('checkout')->formatPrice($priceInclVat);
  1022. ?>
  1023. ```
  1024.  
  1025. ## Get number of items in cart and total quantity in cart ##
  1026.  
  1027. ```php
  1028. <?php
  1029. $totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
  1030. $totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
  1031. ?>
  1032. ```
  1033.  
  1034.  
  1035. # Sales Order #
  1036.  
  1037. ## Load order by id and increment id ##
  1038.  
  1039. ```php
  1040.  
  1041. To load by Order id you would just call load:
  1042.  
  1043. $order = Mage::getModel('sales/order')->load(24999); //use an entity id here
  1044.  
  1045. To load an order by increment id one would do:
  1046.  
  1047. $order = Mage::getModel('sales/order')->loadByIncrementId('10000001'); //use a real increment order
  1048.  
  1049. ```
  1050.  
  1051. ## Get order details by order id ##
  1052.  
  1053. ```php
  1054.  
  1055. To load by Order id you would just call load:
  1056.  
  1057. echo '<pre>';
  1058.  
  1059. $order = Mage::getModel('sales/order')->load(24999); //use an entity id here
  1060.  
  1061. $orderData = $order->getData();
  1062.  
  1063. print_r($orderData);
  1064.  
  1065. ```
  1066.  
  1067.  
  1068. ## Magento – get all ordered items of the order ##
  1069.  
  1070. ```php
  1071. <?php
  1072. $order_id = 2314; //use your own order id
  1073. $order = Mage::getModel("sales/order")->load($order_id); //load order by order id
  1074. $ordered_items = $order->getAllItems();
  1075. foreach($ordered_items as $item){ //item detail
  1076. echo $item->getItemId(); //product id
  1077. echo $item->getSku();
  1078. echo $item->getQtyOrdered(); //ordered qty of item
  1079. echo $item->getName(); // etc.
  1080. }
  1081. ?>
  1082.  
  1083. ```
  1084. ## Magento – change cancelled order to processing programmtially ##
  1085.  
  1086. ```php
  1087. <?php
  1088. $incrementId = '100057506'; //replace this with the increment id of your actual order
  1089. $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
  1090.  
  1091. $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING);
  1092. $order->setStatus('processing');
  1093. $order->setBaseDiscountCanceled(0);
  1094. $order->setBaseShippingCanceled(0);
  1095. $order->setBaseSubtotalCanceled(0);
  1096. $order->setBaseTaxCanceled(0);
  1097. $order->setBaseTotalCanceled(0);
  1098. $order->setDiscountCanceled(0);
  1099. $order->setShippingCanceled(0);
  1100. $order->setSubtotalCanceled(0);
  1101. $order->setTaxCanceled(0);
  1102. $order->setTotalCanceled(0);
  1103.  
  1104. foreach($order->getAllItems() as $item){
  1105. $item->setQtyCanceled(0);
  1106. $item->setTaxCanceled(0);
  1107. $item->setHiddenTaxCanceled(0);
  1108. $item->save();
  1109. }
  1110.  
  1111.  
  1112. $order->save();
  1113. ?>
  1114.  
  1115. ```
  1116.  
  1117.  
  1118. # Checkout Page #
  1119.  
  1120. ## Get all active payment methods ##
  1121.  
  1122. ```php
  1123. $activePayMethods = Mage::getModel('payment/config')->getActiveMethods();
  1124.  
  1125. ```
  1126.  
  1127. ## Get all active shipping carriers ##
  1128.  
  1129. ```php
  1130. $activeCarriers = Mage::getSingleton('shipping/config')->getActiveCarriers();
  1131.  
  1132. ```
  1133. ## Get order information on success.phtml ##
  1134.  
  1135. ```php
  1136. <?php
  1137. $_customerId = Mage::getSingleton('customer/session')->getCustomerId();
  1138. $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
  1139. $order = Mage::getSingleton('sales/order');
  1140. $order->load($lastOrderId);
  1141. $_totalData =$order->getData();
  1142. $_grand = $_totalData['grand_total'];
  1143. ?>
  1144.  
  1145. ```
  1146.  
  1147.  
  1148.  
  1149. # Admin Panel #
  1150.  
  1151. ## Magento: Mass Exclude/Unexclude Images ##
  1152.  
  1153. By default, Magento will check the 'Exclude' box for you on all imported images, making them not show up as a thumbnail under the main product image on the product view.
  1154.  
  1155. ```php
  1156. # Mass Unexclude
  1157. UPDATE`catalog_product_entity_media_gallery_value` SET `disabled` = '0' WHERE `disabled` = '1';
  1158. # Mass Exclude
  1159. UPDATE`catalog_product_entity_media_gallery_value` SET `disabled` = '1' WHERE `disabled` = '0';
  1160.  
  1161. ```
  1162.  
  1163.  
  1164. # RESET #
  1165.  
  1166. ## Reset Sales order ##
  1167. ```php
  1168.  
  1169. SET FOREIGN_KEY_CHECKS=0;
  1170. TRUNCATE `sales_flat_creditmemo`;
  1171. TRUNCATE `sales_flat_creditmemo_comment`;
  1172. TRUNCATE `sales_flat_creditmemo_grid`;
  1173. TRUNCATE `sales_flat_creditmemo_item`;
  1174. TRUNCATE `sales_flat_invoice`;
  1175. TRUNCATE `sales_flat_invoice_comment`;
  1176. TRUNCATE `sales_flat_invoice_grid`;
  1177. TRUNCATE `sales_flat_invoice_item`;
  1178. TRUNCATE `sales_flat_order`;
  1179. TRUNCATE `sales_flat_order_address`;
  1180. TRUNCATE `sales_flat_order_grid`;
  1181. TRUNCATE `sales_flat_order_item`;
  1182. TRUNCATE `sales_flat_order_payment`;
  1183. TRUNCATE `sales_flat_order_status_history`;
  1184. TRUNCATE `sales_flat_quote`;
  1185. TRUNCATE `sales_flat_quote_address`;
  1186. TRUNCATE `sales_flat_quote_address_item`;
  1187. TRUNCATE `sales_flat_quote_item`;
  1188. TRUNCATE `sales_flat_quote_item_option`;
  1189. TRUNCATE `sales_flat_quote_payment`;
  1190. TRUNCATE `sales_flat_quote_shipping_rate`;
  1191. TRUNCATE `sales_flat_shipment`;
  1192. TRUNCATE `sales_flat_shipment_comment`;
  1193. TRUNCATE `sales_flat_shipment_grid`;
  1194. TRUNCATE `sales_flat_shipment_item`;
  1195. TRUNCATE `sales_flat_shipment_track`;
  1196. TRUNCATE `sales_invoiced_aggregated`;
  1197. TRUNCATE `sales_invoiced_aggregated_order`;
  1198. TRUNCATE `sales_order_aggregated_created`;
  1199. TRUNCATE `sendfriend_log`;
  1200. TRUNCATE `tag`;
  1201. TRUNCATE `tag_relation`;
  1202. TRUNCATE `tag_summary`;
  1203. TRUNCATE `wishlist`;
  1204. TRUNCATE `log_quote`;
  1205. TRUNCATE `report_event`;
  1206. ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
  1207. ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
  1208. ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
  1209. ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
  1210. ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
  1211. ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
  1212. ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
  1213. ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
  1214. ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
  1215. ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
  1216. ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
  1217. ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
  1218. ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
  1219. ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
  1220. ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
  1221. ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
  1222. ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
  1223. ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
  1224. ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
  1225. ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
  1226. ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
  1227. ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
  1228. ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
  1229. ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
  1230. ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
  1231. ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
  1232. ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
  1233. ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
  1234. ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
  1235. ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
  1236. ALTER TABLE `tag` AUTO_INCREMENT=1;
  1237. ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
  1238. ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
  1239. ALTER TABLE `wishlist` AUTO_INCREMENT=1;
  1240. ALTER TABLE `log_quote` AUTO_INCREMENT=1;
  1241. ALTER TABLE `report_event` AUTO_INCREMENT=1;
  1242.  
  1243. SET FOREIGN_KEY_CHECKS=1;
  1244. ```
  1245.  
  1246. ## Reset Customer Table ##
  1247.  
  1248. ```php
  1249. SET FOREIGN_KEY_CHECKS=0;
  1250. -- reset customers
  1251. TRUNCATE customer_address_entity;
  1252. TRUNCATE customer_address_entity_datetime;
  1253. TRUNCATE customer_address_entity_decimal;
  1254. TRUNCATE customer_address_entity_int;
  1255. TRUNCATE customer_address_entity_text;
  1256. TRUNCATE customer_address_entity_varchar;
  1257. TRUNCATE customer_entity;
  1258. TRUNCATE customer_entity_datetime;
  1259. TRUNCATE customer_entity_decimal;
  1260. TRUNCATE customer_entity_int;
  1261. TRUNCATE customer_entity_text;
  1262. TRUNCATE customer_entity_varchar;
  1263. TRUNCATE log_customer;
  1264. TRUNCATE log_visitor;
  1265. TRUNCATE log_visitor_info;
  1266.  
  1267. ALTER TABLE customer_address_entity AUTO_INCREMENT=1;
  1268. ALTER TABLE customer_address_entity_datetime AUTO_INCREMENT=1;
  1269. ALTER TABLE customer_address_entity_decimal AUTO_INCREMENT=1;
  1270. ALTER TABLE customer_address_entity_int AUTO_INCREMENT=1;
  1271. ALTER TABLE customer_address_entity_text AUTO_INCREMENT=1;
  1272. ALTER TABLE customer_address_entity_varchar AUTO_INCREMENT=1;
  1273. ALTER TABLE customer_entity AUTO_INCREMENT=1;
  1274. ALTER TABLE customer_entity_datetime AUTO_INCREMENT=1;
  1275. ALTER TABLE customer_entity_decimal AUTO_INCREMENT=1;
  1276. ALTER TABLE customer_entity_int AUTO_INCREMENT=1;
  1277. ALTER TABLE customer_entity_text AUTO_INCREMENT=1;
  1278. ALTER TABLE customer_entity_varchar AUTO_INCREMENT=1;
  1279. ALTER TABLE log_customer AUTO_INCREMENT=1;
  1280. ALTER TABLE log_visitor AUTO_INCREMENT=1;
  1281. ALTER TABLE log_visitor_info AUTO_INCREMENT=1;
  1282. SET FOREIGN_KEY_CHECKS=1;
  1283.  
  1284. ```
  1285.  
  1286.  
  1287. ## Clean Log Tables ##
  1288. ```php
  1289. SET FOREIGN_KEY_CHECKS=0;
  1290. TRUNCATE `log_customer`;
  1291. TRUNCATE `log_visitor`;
  1292. TRUNCATE `log_visitor_info`;
  1293. TRUNCATE `log_visitor_online`;
  1294. TRUNCATE `log_quote`;
  1295. TRUNCATE `log_summary`;
  1296. TRUNCATE `log_summary_type`;
  1297. TRUNCATE `log_url`;
  1298. TRUNCATE `log_url_info`;
  1299. TRUNCATE `sendfriend_log`;
  1300. TRUNCATE `report_event`;
  1301. TRUNCATE `dataflow_batch_import`;
  1302. TRUNCATE `dataflow_batch_export`;
  1303. TRUNCATE `index_process_event`;
  1304. TRUNCATE `index_event`;
  1305. ALTER TABLE `log_customer` AUTO_INCREMENT=1;
  1306. ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
  1307. ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;
  1308. ALTER TABLE `log_visitor_online` AUTO_INCREMENT=1;
  1309. ALTER TABLE `log_quote` AUTO_INCREMENT=1;
  1310. ALTER TABLE `log_summary` AUTO_INCREMENT=1;
  1311. ALTER TABLE `log_url_info` AUTO_INCREMENT=1;
  1312. ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
  1313. ALTER TABLE `report_event` AUTO_INCREMENT=1;
  1314. ALTER TABLE `dataflow_batch_import` AUTO_INCREMENT=1;
  1315. ALTER TABLE `dataflow_batch_export` AUTO_INCREMENT=1;
  1316. ALTER TABLE `index_event` AUTO_INCREMENT=1;
  1317. SET FOREIGN_KEY_CHECKS=1;
  1318. ```
Add Comment
Please, Sign In to add comment