【问题标题】:how to add product in cart programmatically in prestashop如何在 prestashop 中以编程方式将产品添加到购物车中
【发布时间】:2013-06-28 16:07:00
【问题描述】:

我正在使用prestashop 1.5.3,我正在开发一个支付网关问题是我找不到如何以编程方式在购物车中添加产品并订购添加支付费用

请任何人帮助我

【问题讨论】:

  • 我不明白你的意思,产品添加程序?如果您想通过单击任何按钮添加产品​​。你可以看到 ajax-cart.js 是 blockcart 模块的 js ,通过使用它你可以添加产品。
  • 不,我需要知道服务器端 prestashop 类或函数,以便在购物车中添加单个产品以收取费用

标签: payment-gateway prestashop


【解决方案1】:

以下是以编程方式添加多个产品的代码。也可用于添加一种产品。将这些代码放在网站根目录下名为 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);
    }
}
?>

【讨论】:

  • 不工作,伙计...你如何运行它以将产品放入购物车?我尝试只运行 php 并从 header.tpl 运行 ajax ...
【解决方案2】:

您可以将此代码放在根目录中的 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');

【讨论】:

    【解决方案3】:

    如果您开发一个支付模块,您应该首先检查其他支付模块是如何制作的,例如 Ogone 或 Paypal 模块。你可以在这里找到它们:https://github.com/PrestaShop/PrestaShop-modules

    prestashop 中用于从购物车中添加/删除产品的方法是 Cart->updateQty()(在文件 classes/Cart.php 中)。

    【讨论】:

      【解决方案4】:
      <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...或任何组合(已注释)

      【讨论】:

      • @SajidAnwar 将其添加到带有参数“productId”的函数中,然后在“for”循环中调用它
      • 我没有足够的时间来编写具体的代码,但我的意思是:Array product_ids = [123,562,653]; foreach(product_ids){ addProduct } function addProduct(productId){ this function }
      • @SajidAnwar 这个论坛不是为了给你确切的代码,而是为了让你朝着正确的方向前进,这是给想要学习的人的。如果您想要确切的解决方案,我可以帮助您,但它不会是免费的..
      【解决方案5】:
      // 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
      // )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多