如何在prestahop 1.7中以编程方式向购物车添加额外价格

时间:2017-06-19 05:02:59

标签: customization price prestashop-1.7

是prestashop 1.7的新手。我想在prestashop 1.7中添加到购物车时为定制产品增加额外费用。请任何人帮我。 将自己的自定义数据添加到购物车。

$idProduct = Tools::getValue('pid'); // for me it's always one
    $qty=Tools::getValue('qty'); // always add one item
    $token = Tools::getValue('token');
    $customData_front = Tools::getValue('front_view');
    $customData_back = Tools::getValue('back_view');
    $customData = Tools::getValue('customdata');
    $attribute =Tools::getValue('id_product_attribute')
    if (is_null($this->context->cart->id)) {
     $this->context->cart->add();
     $this->context->cookie->__set('id_cart', $this->context->cart->id);
    }

    // get cart id if exists
    if ($this->context->cookie->id_cart)
    {
        $cart = new Cart($this->context->cookie->id_cart);
    }

    // create new cart if needed
     if (!isset($cart) OR !$cart->id)
    {
        $cart = new Cart($this->context->cookie->id_cart);
        $cart->id_customer = (int)($this->context->cookie->id_customer);
        $cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
        $cart->id_address_invoice = $cart->id_address_delivery;
        $cart->id_lang = (int)($this->context->cookie->id_lang);
        $cart->id_currency = (int)($this->context->cookie->id_currency);
        $cart->id_carrier = 1;
        $cart->recyclable = 0;
        $cart->gift = 0;
        $cart->add();
        $this->context->cookie->id_cart = (int)($cart->id);
    }
      $product = new Product((int)$idProduct);
        if ($attribute > 0) {
            $minimal_quantity = (int)Attribute::getAttributeMinimalQty($this->id_product_attribute);
        } else {
            $minimal_quantity = (int)$product->minimal_quantity;
        }
    // add customizatated text
        $check =serialize($customData);
        $id_address_delivery = $cart->id_address_delivery;
        $customization = $this->context->cart->addTextFieldToProduct((int)($idProduct), 9, Product::CUSTOMIZE_TEXTFIELD, serialize($customData));
        $exising_customization = Db::getInstance()->executeS('SELECT id_customization FROM '._DB_PREFIX_.'customized_data ORDER BY id_customization DESC LIMIT 0,1');
        $customization = $exising_customization[0]['id_customization'];
        Db::getInstance()->execute('UPDATE ps_customization SET in_cart = 1, id_product_attribute = '.$attribute.',id_address_delivery='.$id_address_delivery.',quantity='.$minimal_quantity.' WHERE id_customization = ' .$customization);


    // get product to add into cart
        $productToAdd = new Product((int)($idProduct), true, (int)($this->context->cookie->id_lang));
        $cart->update();

        $this->context->cart->updateQty((int)($qty),(int)($idProduct),(int)($attribute),$customization,'up',(int)($id_address_delivery));
        $cart->update();

因为我希望每次定制都增加额外费用。

0 个答案:

没有答案