根据高度和宽度计算价格

Calculate the price based on height and width

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

假设您想在产品页面的产品 ID N° 1 上执行此操作: 首先在 product.tpl 中添加两个输入字段(或在 product.tpl 中调用的 module.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);

并且在订单完成后您输入正常价格,因此覆盖 override/controllers/front/OrderConfirmationController 中的 orderConfirmation 控制器。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 添加常量,希望对您有所帮助