簡體   English   中英

如何在prestashop中以編程方式在購物車中添加產品

[英]how to add product in cart programmatically in prestashop

我正在使用prestashop 1.5.3 ,我正在開發支付網關的問題是我找不到如何在購物車中以編程方式添加產品並訂購添加支付費用

請有人幫幫我

以下是以編程方式添加多個產品的代碼。 也可以用來添加一個產品。 將這些代碼放在您的網站根目錄下名為test.php的文件中,然后像/test.php??products_ids=11,9,10那樣運行它,其中11,9,10是3個產品ID。 希望這可以幫助。

<?php
require(dirname(__FILE__).'/config/config.inc.php');

$context=Context::getContext();//new Cart();
$id_cart=$context->cookie->__get('id_cart');

$products_ids=$_GET['products_ids']; // comma seprated products id example : test.php?products_ids=1,2,3

$products_ids_array=explode(",",$products_ids);

if(count($products_ids_array)>0){
    $cart=new Cart($id_cart);
    $cart->id_currency=2;
    $cart->id_lang=1;
    foreach($products_ids_array as $key=>$id_product){
        $cart->updateQty(1, $id_product);
    }
}
?>

您可以將此代碼放在Root目錄中的php文件中,並使用一個簡單的表單指向包含產品ID和數量的頁面。

只是改變:

<?php    
$idProduct= 19825 to $idProduct=$_POST["txtproductid"]
$qty=5 to $qty=$_POST["txtqty"]; 

$useSSL = true;

include('/config/config.inc.php');

include('/header.php');
global $params; 
$errors = array();

$idProduct =19825;
$qty=5; 

if ($cookie->isLogged())
{
    /* Cart already exists */
    if ((int)$cookie->id_cart)
    {
        $cart = new Cart((int)$cookie->id_cart);
    }
    if (!isset($cart) OR !$cart->id)
    {
        $cart = new Cart();
        $cart->id_customer = (int)($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)($cookie->id_lang);
        $cart->id_currency = (int)($cookie->id_currency);
        $cart->id_carrier = 1;
        $cart->recyclable = 0;
        $cart->gift = 0;
        $cart->add();
        $cookie->id_cart = (int)($cart->id);    
    }


/* get product id and product attribure id */
        $data = explode(",", $product);
        $idProduct = $data[0];  */
        $idProductAttribute = $data[1]; 

        if ($qty != '') 
        {  

 $producToAdd = new Product((int)($idProduct), true, (int)($cookie->id_lang));

 if ((!$producToAdd->id OR !$producToAdd->active) AND !$delete)
/* Product is no longer available, skip product */ 
                continue;

            /* Check the quantity availability  */
if ($idProductAttribute > 0 AND is_numeric($idProductAttribute))
            {
if (!$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty((int)$idProductAttribute, (int)$qty))
                { 
/* There is not enough product attribute in stock - set customer qty to current stock on hand */ 
            $qty = getAttributeQty($idProductAttribute); 
                } 
            }
            elseif (!$producToAdd->checkQty((int)$qty))
                /* There is not enough product in stock - set customer qty to current stock on hand */ 
             $qty = $producToAdd->getQuantity(idProduct); 


$updateQuantity = $cart->updateQty((int)($qty), (int)($idProduct), (int)($idProductAttribute), NULL, 'up');
           $cart->update();

        }


    /* redirect to cart 
    if (!sizeof($errors)) */

    Tools::redirect('order.php');


}
else
{
 Tools::redirect('/index.php');
}

$smarty->assign(array(
'id_customer' => (int)($cookie->id_customer),
'errors' => $errors
));

include_once('/footer.php');

如果您開發支付模塊,您應首先檢查如何制作其他支付模塊,例如Ogone或Paypal模塊。 你可以在這里找到它們: https//github.com/PrestaShop/PrestaShop-modules

prestashop中用於從購物車添加/刪除產品的方法是Cart-> updateQty()(在文件類/ Cart.php中)。

<script>
        $(document).ready(function(){
            $.ajax({
            type: 'POST',
            headers: { "cache-control": "no-cache" },
            url: 'yourshopurl',
            async: true,
            cache: false,
            dataType: 'json',
                        //( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): '')
                        data: 'controller=cart&add=1&ajax=true&qty=1&id_product=247&token=' + static_token ,
            success: function(jsonData)
            {
                console.log("products added");
            }
        });
        });
    </script>

現在只需添加產品ID ...或任何組合(評論)

// Add cart if no cart found
if (!$this->context->cart->id) {
    if (Context::getContext()->cookie->id_guest) {
        $guest = new Guest(Context::getContext()->cookie->id_guest);
        $this->context->cart->mobile_theme = $guest->mobile_theme;
    }
    $this->context->cart->add();
    if ($this->context->cart->id) {
        $this->context->cookie->id_cart = (int)$this->context->cart->id;
    }
}

// Add product
$Cart = $this->context->cart;
$Cart->updateQty(1, 13, 1, false);

/* Optional parameters  */
// classes/cart.php
// public function updateQty(
//     $quantity,
//     $id_product,
//     $id_product_attribute = null,
//     $id_customization = false,
//     $operator = 'up',
//     $id_address_delivery = 0,
//     Shop $shop = null,
//     $auto_add_cart_rule = true,
//     $skipAvailabilityCheckOutOfStock = false
// )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM