2017-09-22 78 views

回答

0

讓我們說,你想這樣做的ID N°1從產品頁面的產品: 先在你product.tpl添加兩個輸入字段(或在您的module.tpl叫產品。 TPL)

<input name="height"><br> 
<input name="width"> 

現在你需要得到它在你的module.php改價格動態地:

$id_product = 1;  
$newPrice = 0; 
$height = Tools::getValue('height'); 
$width = Tools::getValue('width'); 

//Price rules (change with your rules) 
if($height > 10 && $width > 10) $newPrice = 10; 

    $specific_price = new SpecificPrice(); 
    $specific_price->price = $newPrice; 
    $specific_price->id_cart = (int) $this->context->cart->id; 
    $specific_price->id_shop = 0; 
    $specific_price->id_shop_group = 0; 
    $specific_price->id_currency = 0; 
    $specific_price->id_country = 0; 
    $specific_price->id_group = 0; 
    $specific_price->id_customer = (int) $this->context->customer->id; 
    $specific_price->id_product = (int)$id_product; 
    $specific_price->id_product_attribute = 0; 
    $specific_price->from_quantity = 1; 
    $specific_price->reduction = 0; 
    $specific_price->reduction_type = 'amount'; 
    $specific_price->from = '0000-00-00 00:00:00'; 
    $specific_price->to = '0000-00-00 00:00:00'; 
    $specific_price->add(); 

,如果你想將其添加到購物車

$cart->id_lang = (int)($this->context->cookie->id_lang); 
    $cart->id_currency = (int)($this->context->cookie->id_currency); 
    $cart->updateQty($qte, $id_product); 

和訂單後完成你把正常價格,所以覆蓋重寫/控制器/前面的orderConfirmation控制器/ OrderConfirmationController.php:訂單完成

class OrderConfirmationController extends OrderConfirmationControllerCore 
{ 
    public $ssl = true; 
    public $php_self = 'order-confirmation'; 
    public $id_cart; 
    public $id_module; 
    public $id_order; 
    public $reference; 
    public $secure_key; 

    public function getPriceBack($id_product){ 

     $normalPrice = Product::getPriceStatic($id_product); 

     $specific_price = new SpecificPrice(); 
     $specific_price->price = $normalPrice; 
     $specific_price->id_cart = (int) $this->context->cart->id; 
     $specific_price->id_shop = 0; 
     $specific_price->id_shop_group = 0; 
     $specific_price->id_currency = 0; 
     $specific_price->id_country = 0; 
     $specific_price->id_group = 0; 
     $specific_price->id_customer = (int) $this->context->customer->id; 
     $specific_price->id_product = (int)$id_product; 
     $specific_price->id_product_attribute = 0; 
     $specific_price->from_quantity = 1; 
     $specific_price->reduction = 0; 
     $specific_price->reduction_type = 'amount'; 
     $specific_price->from = '0000-00-00 00:00:00'; 
     $specific_price->to = '0000-00-00 00:00:00'; 
     $specific_price->add(); 

    } 

    public function init() 
    { 
     parent::init(); 

     $this->id_cart = (int)(Tools::getValue('id_cart', 0)); 
     $is_guest = false; 

     /* get normal price back */ 
     $id_product = 1; 
     self::getPriceBack($id_product); 

     /* check if the cart has been made by a Guest customer, for redirect link */ 
     if (Cart::isGuestCartByCartId($this->id_cart)) { 
      $is_guest = true; 
      $redirectLink = 'index.php?controller=guest-tracking'; 
     } else { 
      $redirectLink = 'index.php?controller=history'; 
     } 

     $this->id_module = (int)(Tools::getValue('id_module', 0)); 
     $this->id_order = Order::getOrderByCartId((int)($this->id_cart)); 
     $this->secure_key = Tools::getValue('key', false); 
     $order = new Order((int)($this->id_order)); 
     if ($is_guest) { 
      $customer = new Customer((int)$order->id_customer); 
      $redirectLink .= '&id_order='.$order->reference.'&email='.urlencode($customer->email); 
     } 
     if (!$this->id_order || !$this->id_module || !$this->secure_key || empty($this->secure_key)) { 
      Tools::redirect($redirectLink.(Tools::isSubmit('slowvalidation') ? '&slowvalidation' : '')); 
     } 
     $this->reference = $order->reference; 
     if (!Validate::isLoadedObject($order) || $order->id_customer != $this->context->customer->id || $this->secure_key != $order->secure_key) { 
      Tools::redirect($redirectLink); 
     } 
     $module = Module::getInstanceById((int)($this->id_module)); 
     if ($order->module != $module->name) { 
      Tools::redirect($redirectLink); 
     } 
    } 
} 

後必須清空購物車與id_product和DBB刪除規則:

$this->context->cart->deleteProduct(1); 

    Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'specific_price WHERE id_customer='.(int)$this->context->customer->id); 

這只是一個例子,不變量的測試不能使用,也許把對康斯坦特您的產品ID而言,希望它有助於

+0

爲了改變價格動態,我應該更新cart.php updateQty函數嗎? –

+0

是的,這是一個簡單的方法來做到這一點 – Melvita

+0

這就是我面臨的問題: 比方說,我們有一個基準價格的產品:100 $ 用戶訂購具有自定義高度和寬度的產品,並且讓我們說價格成爲100 $ + 20 $ = 120 $ ,並且由於新價格是按照特定價格進行計算和添加的,所以在下一個訂單中基準價格將變爲120 $,並且我無法找到使價格返回到原始金額 –