• Magento执行SQL操作数据库

    一、使用Magento  resource 连接数据库,进行操作

    1. $db = Mage::getResourceSingleton(‘core/resource’)->getConnection(‘core_write’);
    2. $result = $db->query(‘SELECT `entity_id` FROM `catalog_product_entity`’);

    3. if(!$result) {
    4.     return FALSE;
    5. }

    6. $rows = $result->fetch(PDO::FETCH_ASSOC);

    7. if(!$rows) {
    8.     return FALSE;
    9. }

    10. print_r($rows);

    二、针对模块,采用zend 框架模式操作模块对应数据表格

    1. $resource = Mage::getSingleton(‘core/resource’);
    2. $read = $resource->getConnection(‘core_read’);
    3. $categoryProductTable=$read->getTableName(‘category/category_product’)
    4. $select = $read->select()->from(array(‘cp’=>$categoryProductTable))->where(‘cp.category_id=?’, ’3′);
    5. $products=$read->fetchAll($select);
    6. foreach($products as $row)
    7. {
    8. $product = Mage::getModel(‘catalog/product’)->load($row['product_id']);
    9. echo $product->getName();
    10. }

     

  • Magento调用mini登录框问题

    通过前面的调用边栏高级搜索的文章,回头再来看一下 Magento已有的mini login form。

    可以在customer.xml中发现一下调用代码

    <!--
    Load this update on every page when customer is logged out
    -->
    
        <customer_logged_out>
            <!---<reference name="right">
                <block type="customer/form_login" name="customer_form_mini_login" before="-" template="customer/form/mini.login.phtml"/>
            </reference>-->
            <reference name="top.links">
                <action method="addLink" translate="label title" module="customer"><label>Log In</label><url helper="customer/getLoginUrl"/><title>Log In</title><prepare/><urlParams/><position>100</position></action>
            </reference>
            <remove name="reorder"></remove>
        </customer_logged_out>

    会发现在右边栏调用的mini login form 被注释了,去除注释,进入包含右边栏的子页面之后,发现mini登录框确实被调用了,但是头部的title也变成 Customer Login了,这样可不太好。

    仿照之前调用高级搜索解决面包屑问题的方法,我们也去看一下默认的代码块

    /**
     * Customer login form block
     *
     * @category   Mage
     * @package    Mage_Customer
     * @author      Magento Core Team <core@magentocommerce.com>
     */
    class Mage_Customer_Block_Form_Login extends Mage_Core_Block_Template
    {
        private $_username = -1;
    
        protected function _prepareLayout()
        {
            $this->getLayout()->getBlock('head')->setTitle(Mage::helper('customer')->__('Customer Login'));
            return parent::_prepareLayout();
        }

    发现在调用的时候,title被重设了

    于是在我们自定义的模块中重写此方法

    <customer_logged_out>
            <reference name="right">
                <block type="wjtheme/login" name="customer_form_mini_login" before="-" template="customer/form/mini.login.phtml"/>
            </reference>

    测试成功,我们还可以在mini.login.phtml中添加需要的链接,如注册  忘记密码等

    OK 就记到这边 :-)

     

     

  • Magento 使用 api实现track Order 订单跟踪

    Magento 使用 api实现track Order  订单跟踪 主要是针对1.5及以下的版本,因为在1.6中已经集成了订单查询的功能了。

    先介绍下使用说明吧(涉及一个代码文件,稍后贴出来)

    进入后台,
    System——>Web Service
    新建一个api用户 (记住用户名密码)
    (用户角色赋予 Order相关的权限)

    新建一个cms页面用于Order查询
    调用代码
    {{block type=”core/template” template=”cms/order-status-api.phtml”}}

    修改order-status-api.phtml 文件中的
    $proxy = new SoapClient(‘http://www.magentoeasy.com/index.php/api/soap?wsdl’);
    域名为目标网站域名

    修改如下代码中的用户名密码为新建的api 用户名密码
    //change to your API username/password
    $sessionId = $proxy->login(‘orderviewer’, ‘orderviewer’);
    OK至于cms页面的链接要怎么放,样式修改,以及新建api用户新建页面清理缓存之类的这边就略过了,需要的可以参考下早期的博文

    下面是 order-status-api.phtml 文件的源码

    <style type="text/css">
    .orderStatusTable {
        border:1px solid #CCCCCC;
        font-size:11px;
    }
    .track-order-table td { padding:3px;}
    .orderStatusTable td {
    padding:8px;
    }
    .currentStatus {
        font-size: 11px;
        background-color: #eee;
        padding: 10px;
        margin: 0 0 15px 0;
        line-height: 15px;
        border: 1px solid #ccc;
        color: #333;
    }
    .currentStatus span {
        font-size: 14px;
        line-height: 23px;
        color: #000;
    }
    </style>
    <div>
        <h3>Check My Order Status</h3>
    </div>
     
    <p>Please enter your order number and email address to see the status of your order.</p>
     
    <form name="" action="<?php echo $this->getUrl('track-order');?>" method="get">
    <table class="track-order-table" border="0" cellspacing="0" cellpadding="0">
        <tr style="padding:3px 0;display:block;">
            <td><strong>Order Number:</strong></td>
            <td><input type="text" name="order_id" id="order_id" value="<?php echo (isset($_GET['order_id'])) ? $_GET['order_id'] : ''; ?>" /></td>
        </tr>
        <tr style="padding:3px 0;display:block;">
            <td><strong>Email Address:</strong></td>
            <td><input name="email_address" type="text" id="email_address" value="<?php echo (isset($_GET['email_address'])) ? $_GET['email_address'] : ''; ?>" size="30" /></td>
        </tr>
        <tr style="padding:5px 0;display:block;clear:both;margin-top:10px;">
            <td> </td>
            <td><button class="button" title="Subscribe" type="submit"><span><span>Submit</span></span></button></td>
        </tr>
    </table>
     
    </form>
     
    <div></div>
     
    <?php
     
        $live = true; //determines verbosity of errors
     
        $error = '';
        $statusMessage = '';
        $trackingNumberMessage = '';
        $shippingMessage = '';
     
        $orderID = '';
        $emailAddress = '';
     
        if(isset($_GET['order_id'])) {
     
            //$orderID = trim(preg_replace('/[^0-9]*/', '', $_GET['order_id']));
            $orderID = trim(preg_replace('/[^0-9a-zA-Z]*/', '', $_GET['order_id']));
            $emailAddress = trim($_GET['email_address']);
            try {
     
                ini_set("soap.wsdl_cache", "0");
                ini_set("soap.wsdl_cache_enabled", "0");
     
                //******************************************************************/
     
                // change to match your domain name
                //$siteapiurl= $this->getUrl('index.php/api/soap?wsdl');
                //$proxy = new SoapClient($siteapiurl);
                $proxy = new SoapClient('http://www.magentoeasy.com/index.php/api/soap?wsdl');
                //change to your API username/password
                $sessionId = $proxy->login('orderviewer', 'orderviewer');
     
                //******************************************************************/
     
                //find all orders related to this id
                $orderById = $proxy->call($sessionId, 'sales_order.info', $orderID);
                //print_r($orderById);
                //echo "<hr>";
                $items = $orderById['items'];
                //print_r($items);
                if($orderById['customer_email'] == $emailAddress) {
                    //we are setting this variable for use later
                    $orderLookup = "success";
                    if (strtolower($orderById['status']) == "holded") {
                        $orderById['status'] = "On Hold";
                    }
                    $statusMessage = '<span>Your order status is: <strong>'.ucwords(str_replace('_', ' ', $orderById['status'])).'</strong></span>';
     
                    if(ucwords(str_replace('_', ' ', $orderById['status'])) == "Processing"){
                        $statusMessage .= '<br/><br/><strong>What does this mean?</strong><br/>Processing Time is the time it takes from when you submit your order to when the product leaves the Distribution Center.';
                    }
     
                } else {
                    $orderLookup = "failure";
                    echo "We were unable to find your order information. Please verify your Order Number and Email Address are correct.";
                }
                //echo $orderById['status']."<hr>";
                //if the order status is complete we look up shipping information
                if(strtolower($orderById['status']) == "complete" && $orderLookup == "success") {
                    //we look for all shipments related to this order id using internal magento order id
                    $findShipments = $proxy->call($sessionId, 'sales_order_shipment.list', array(array('order_id'=>array('like'=>$orderById['order_id']))));
                    //print_r($findShipments);
                    if (count($findShipments) < 1) { //if $findShipments is not an array
     
                        echo "There was an unknown error and your shipment information could not be found. Please contact Customer Service to get the current status of your order.";
     
                    } else {
     
                        //we pull the increment id for the shipment
                        $thisShipmentID = $findShipments[0]['increment_id'];
     
                        //now we pull all shipment info that specific shipment id
                        if(!$proxy->call($sessionId, 'sales_order_shipment.info', $thisShipmentID)){
                            $trackingNumberMessage = "Shipment ID: <strong>".$thisShipmentID."</strong>";
                            $shippingMessage = "Your order was shipped on " . $findShipments[0]['created_at'] . ".<br/><br/>";
                        }
                        else {
                            $getShipmentInfo = $proxy->call($sessionId, 'sales_order_shipment.info', $thisShipmentID);
                            //print_r($getShipmentInfo);
                                //set each variable
                            $shipDate = $getShipmentInfo['created_at'];
                            $defaultTimeZone = date_default_timezone_get();
                            date_default_timezone_set('EST');
                            //and echo the data to screen
                            $shippingMessage = "Your order was shipped on " . date("l, F jS, Y \\a\\t g:i:s a T", strtotime($shipDate . ' ' . $defaultTimeZone)) .".<br/>";
                            if(count($getShipmentInfo['tracks']) > 0){
                                foreach($getShipmentInfo['tracks'] as $temp_track)
                                $shippingMessage .= "Shipped By : ".$temp_track['title']."  ,Tracking Number :".$temp_track['number']."<br/>"; }
                                $shippingMessage .= 'Check your shipment state by tracking number~.<br>By Fedex : <a target="_blank" href="http://www.fedex.com/Tracking">Fedex Express</a><br/>
    By DHL : <a target="_blank" href="http://www.dhl.com/en/express/tracking.html">DHL</a><br/>
    By UPS : <a target="_blank" href="http://www.ups.com/tracking/tracking.html">UPS</a><br/>';
                            }
                            $shippingMessage .= "<br/>";
                        }
     
                    } //no errors
     
                }
     
                if($orderLookup != "failure"){
     
                    echo '<p style="padding: 10px; background:#eee; margin: 10px 0;">'.$statusMessage.'<br/>'.$trackingNumberMessage.'</p>';
     
                    echo $shippingMessage;
     
                    echo "<h4>Products in your order:</h4><ul>";
                            foreach($items as $item){
                                $temp_sku = $item['sku'];
                                if(strpos($temp_sku,'-'))
                                {
                                    $temp_sku=substr($temp_sku,0,strpos($temp_sku,'-'));
                                }
                                echo "<li>".number_format($item['qty_invoiced'], 0) . " x <strong>" . strtoupper($temp_sku) . "</strong> " . $item['name'] . "</li>";
                            }
                    echo "</ul>";
                }
     
            } catch (SoapFault $fault) {
                //this is a basic implementation of error checking. I am using try to stop the error from showing a nasty magento error page
                if($fault->faultcode == "100") {
                    echo "That Order Number was not found in our system.";
                } elseif ($fault->faultcode == "http") {
                    echo "Request timed out. Please try again later.";
                } else {
                    //leave this on for testing so we can see SOAP status codes; turn off for live
                    if ($live == false) {
                        echo "Error $fault->faultcode: $fault->faultstring";
                    } else {
                        echo "Error $fault->faultcode: $fault->faultstring"."<hr>";
                        //echo "There was an unknown error. Please try again later, or contact us.";
                    }
                }
            }
     
        } // end if
     
    ?>
    <p><br /><br /><em>For detailed information regarding the status of your order, please contact our helpful Customer Service Experts.</em></p>

    暂记如此,欢迎留言交流 :-)

  • Magento 不同浏览器后台登录及下单问题笔记

    最近N个站都出现了 IE  谷歌浏览器正常但是火狐不行,或者另外的可以 ie不行, 而且之前这种问题都没出现,网站都可以正常运行。

    但是碰到这个就得想办法去解决咯,最开始是直接修改了Session的控制代码,类似于最早解决本地安装环境的方法,修改app/code/core/Mage/Core/Model/Session/Abstract/Varian.php文件

    session_set_cookie_params(
    $this->getCookie()->getLifetime(),
    $this->getCookie()->getPath()
    $this->getCookie()->getDomain(),
    $this->getCookie()->isSecure(),
    $this->getCookie()->getHttponly()
    );
     
    To this part:
    session_set_cookie_params(
    $this->getCookie()->getLifetime(),
    $this->getCookie()->getPath()
    //$this->getCookie()->getDomain(),
    //$this->getCookie()->isSecure(),
    //$this->getCookie()->getHttponly()

    );

     

    部分网站经过此修改就没什么问题了。但是三两天之后发现这个修改过的也无效了。再找其他的突破口,将后台有效时间无限扩大,终于在设置成 186400之后  所有浏览器又恢复常态,可以登录了。

    但是还是不清楚问题究竟出在什么地方,检查过服务器的时间,也没发现什么端倪。。。  菜鸟又悲哀了。。。

    如有高人路过还请指点一二 :-)

  • 关于Magento Order Invoice Shipment等编号的修改

    Magento支持定义不同的 订单发票及货运单号信息,但是在后台往往是找不到设置的。社区版的这个功能应该被阉割了吧,不过我们还是可以通过数据库的方式进行修改(暂时还没发现好的 插件),进入Magento对应的数据库, 浏览eav_entity_type 表会发现, order     invoice     shipment 这几列数据,一般对应id不会改变,分别为4  18   24
    浏览表结构,有三个对应的号码生成设置项, increment_per_store   increment_pad_length   increment_pad_char  ,第一个为抬头的编号,遗憾的是这边只能填数字。第二个为填充长度,第三个为填充的字符串, 修改效果大家可以本地测试看看。

    实际的单号生成代码可以查看下app/code/core/Mage/Eav/Model/Entity/Increment/Abstract.php   及 Numeric.php

    (注:简单的只修改订单等前缀的 可以直接通过修改 eav_entity_store 表中的列值即可 )

    如果你不想修改数据库,也可以在这边通过修改代码进行控制,不过测试的时候一定要注意  设置的默认值及生成单号的数据类型不能与数据库中的字段数据类型发生冲突,避免出现严重错误。

    好了  需要的朋友可以修改测试 去感受一下咯

click
©2010-2024 Jeen All Rights Reserved.Powered by emlog 京ICP备15058100号-1