2013-09-22 60 views
0

我已經在自定義傳送模塊上工作以匹配我的商務模塊傳送需求。我已經注意到,模塊在很多頁面上都慢了很多,儘管我基本上是我的模塊應該掛鉤到drupal商務結賬。自定義模塊減慢Drupal

這裏是我的模塊代碼:

function myips_commerce_shipping_method_info() { 

    $shipping_methods = array(); 

    $shipping_methods['IPS'] = array(
    'title' => t('IPS'), 
    'description' => t('Quote rates from IPS'), 
); 

    return array($shipping_methods); 
} 

function myips_commerce_shipping_service_info() { 

    $shipping_services = array(); 
    $shipping_services['myips_shipping_service'] = array(//arbitrary name w/ 'service' in there 
     'title' => t('Custom Shipping Service'), //title for the interface 
     'description' => t('Variable rates based on the book origin '), 
     'display_title' => t('IPS Shipping'), 
     'shipping_method' => 'IPS', 
     'price_component' => 'shipping', //from commerce_shipping 
     'callbacks' => array(
     'rate' => 'myips_service_rate_order'), 
); 

    return $shipping_services; 

} 

function myips_service_rate_order($shipping_service, $order) { 

    $order_number=$order->order_number; 
    $currency_code='USD'; 
    $shipping_total=getordershipmentvalue($order_number); 
    $rates['myips_shipping_service']=array(
    'amount' => commerce_currency_decimal_to_amount($shipping_total, $currency_code), 
    'currency_code' =>$currency_code, 
    'data' => array(),           
    ); 
    return $rates[$shipping_service['name']]; 
} 

function getcustomershippingcountry($order_id) { 

    $order=commerce_order_load($order_id); 
    $profile_id=$order->commerce_customer_shipping['und']['0']['profile_id']; 
    $profile=commerce_customer_profile_load($profile_id); 
    $country= $profile->commerce_customer_address[LANGUAGE_NONE]['0']['country']; 

    return $country; 
} 

function getordershipmentvalue($order_id) { 

    $order=commerce_order_load($order_id); 
    $total=0; 
    foreach($order->commerce_line_items[LANGUAGE_NONE] as $line) { 
     $line_item_id=$line['line_item_id']; 
     $total=$total+getitemshipmentvalue($line_item_id); 
    } 
    return $total; 
} 

function getitemshipmentvalue($line_item_id){ 

    $line_item=commerce_line_item_load($line_item_id); 
    $line_quantity=$line_item->quantity; 
    $order_id= $line_item->order_id; 

    $destination= getcustomershippingcountry($order_id); 
    $product_id=$line_item->commerce_product[LANGUAGE_NONE]['0']['product_id']; 
    $product=commerce_product_load($product_id); 
    $product_weight=$product->field_book_weight[LANGUAGE_NONE]['0']['weight']; 
    $product_origin=$product->field_book_origin[LANGUAGE_NONE]['0']['value']; 
    $line_total=getshippingrates($destination, $product_origin, $product_weight*$line_quantity); 
    return $line_total; 
} 

function calculateshippment($shipping_type, $zone, $product_weight) { 

    $query=new EntityFieldQuery(); 
    $query->entityCondition('entity_type', 'node'); 
    $query->entityCondition('bundle', $shipping_type); 
    $query->fieldCondition('field_up_to_weight', 'value',$product_weight,'>='); 
    $query->fieldCondition('field_zone_number', 'value',$zone); 
    $query->fieldorderby('field_up_to_weight','value','ASC'); 
    $query->range(0,1); 
    $result=$query->execute(); 
    if(!empty($result)){ 
     $nodes=node_load_multiple(array_keys($result['node'])); 

     foreach($nodes as $node) { 
      $price=$node->field_shipment_price[LANGUAGE_NONE][0]['value']; 
     } 
    } 

    return $price; 
} 


function getdestinationzone($destination, $shipping_type) { 

    $query=new EntityFieldQuery(); 
    $query->entityCondition('entity_type', 'node'); 
    $query->entityCondition('bundle', 'countries_zones'); 
    $query->fieldCondition('field_country_abbreviation', 'value',$destination); 
    $result=$query->execute(); 
    if(!empty($result)) { 
     $nodes=node_load_multiple(array_keys($result['node'])); 

     foreach($nodes as $node) { 
      if($shipping_type=='out_us_zone_prices') { 
       $zone=$node->field_us_zone[LANGUAGE_NONE]['0']['value']; 
      }elseif($shipping_type=='mail_zone_prices') { 
       $zone=$node->field_mail_zone[LANGUAGE_NONE]['0']['value']; 
      }elseif($shipping_type=='dhl_zone_prices') { 
       $zone=$node->field_dhl_zone[LANGUAGE_NONE]['0']['value']; 
      } 
     } 
    } 

    return $zone; 
} 

function getshippingrates($destination, $product_origin, $product_weight) { 

    if($product_origin=='US') { 
    if($destination==$product_origin) { 
     $shipping_type='us_zone_prices'; 
    }else { 
     $shipping_type='out_us_zone_prices'; 
    } 

    /* End of Product Origin US */ 
    }elseif($product_origin=='LB'){ 

    if($destination==$product_origin) { 
     $shipping_type='mail_zone_prices'; 
    }else { 
     $shipping_type='dhl_zone_prices'; 
    } 
    } 
    $zone=getdestinationzone($destination,$shipping_type); 
    return calculateshippment($shipping_type, $zone, $product_weight); 

} 

爲什麼我的模塊放緩Drupal的表現?

+0

你怎麼知道你的模塊正在減慢網站速度?您是否使用XDEBUG分析了您的網站? –

+0

是否在所有頁面上顯示用戶購物車的價格,包括運費。 –

回答

0

我覺得你的模塊的主要問題是,你的數據庫查詢是在foreach循環(在getordershipmentvalue()

好吧你可能不能馬上看到,但如果你看看後面getitemshipmentvalue() -call ,你可以看到,那裏還有很多其他的函數調用。

並且在多個角落將會調用功能calculateshippment()getdestinationzone()。在這些函數中有數據庫查詢。

多數民衆贊成這就是爲什麼你的模塊是如此之慢的原因:因爲在多個角落你查詢循環數據庫。這與數據庫服務器上的DOS攻擊類似(對於很多「commerce_line_items」數組,這是一個更大的問題)。

解決方案:考慮一下你的代碼設計。試着把你的數據庫查詢代碼放在foreach循環之外。

我也希望在第一次請求後緩存數據。 (例如用variable_set()

+0

儘管我同意你的看法,但這並不能解釋爲什麼他網站的其他網頁正在放緩。 –

+0

哦。你是對的。我沒有正確閱讀,我認爲他只是意味着模塊頁面(在Drupal環境中)。 – twity1337