根据高度和宽度计算价格

时间:2017-09-22 20:55:40

标签: prestashop

我需要制作一个允许用户输入宽度和高度的模块,然后根据高度和宽度计算价格。 提前谢谢

1 个答案:

答案 0 :(得分:1)

假设您要在产品页面的产品ID N°1上执行此操作: 首先在product.tpl中添加两个输入字段(或在product.tpl中调用的module.tpl中)

new.vec

现在你需要在你的module.php中获取它来动态地改变价格:

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

如果你想将它添加到购物车

$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();

在订单完成后你输入正常价格,所以覆盖override / controllers / front / OrderConfirmationController.php中的orderConfirmation控制器:

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

订单完成后,你必须用id_product清空购物车并删除dbb中的规则:

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);
        }
    }
}

这只是一个例子,不能在没有变量测试的情况下使用,也许可以将constante用于您的产品ID,希望有所帮助