Php 订单历史记录中的OpenCart“再次订购”功能

Php 订单历史记录中的OpenCart“再次订购”功能,php,opencart,Php,Opencart,我感兴趣的是为OpenCart实现一个“再次订购”功能,在“帐户/订单”下的“订单历史记录”列表中有一个按钮。此按钮将获取此特定订单的所有产品及其相关选项,并将它们全部添加回购物车 下面的代码允许我根据订单ID循环浏览我的产品及其相应的产品选项 $products = $this->model_account_order->getOrderProducts( $this->request->get['order_id'] ); foreach ($product

我感兴趣的是为OpenCart实现一个“再次订购”功能,在“帐户/订单”下的“订单历史记录”列表中有一个按钮。此按钮将获取此特定订单的所有产品及其相关选项,并将它们全部添加回购物车

下面的代码允许我根据订单ID循环浏览我的产品及其相应的产品选项

$products = $this->model_account_order->getOrderProducts(
    $this->request->get['order_id']
);

foreach ($products as $product) {
    $options = $this->model_account_order->getOrderOptions(
        $this->request->get['order_id'], 
        $product['order_product_id']
    );

    foreach ($product['option'] as $option) {
        // Implement custom logic
    }
}

我目前没有任何问题来获得我已经订购的产品。我不知道该做什么,需要帮助的是-如何使用此[如果这是向购物车添加商品的方法]将商品添加回购物车?

通过了解产品ID、数量和选项(如果有),您可以拨打:

$this->cart->add($product_id, $quantity, $options); // use array() instead of $options if there are none
分别适用于每种产品$options是一个数组,要查看它必须具有什么结构,请在向购物车添加带有选项的产品时查看AJAX请求。我认为它是一个数组,其中product_option_id为键,option_value为值。

试试这个


这实际上已经在购物车的客户端实现了。下面是包含在/catalog/controller/account/order.php中的代码,在这种特殊情况下,它正是您想要的1.5.5.1

    if (isset($this->request->get['order_id'])) {
        $order_info = $this->model_account_order->getOrder($this->request->get['order_id']);

        if ($order_info) {
            $order_products = $this->model_account_order->getOrderProducts($this->request->get['order_id']);

            foreach ($order_products as $order_product) {
                $option_data = array();

                $order_options = $this->model_account_order->getOrderOptions($this->request->get['order_id'], $order_product['order_product_id']);

                foreach ($order_options as $order_option) {
                    if ($order_option['type'] == 'select' || $order_option['type'] == 'radio') {
                        $option_data[$order_option['product_option_id']] = $order_option['product_option_value_id'];
                    } elseif ($order_option['type'] == 'checkbox') {
                        $option_data[$order_option['product_option_id']][] = $order_option['product_option_value_id'];
                    } elseif ($order_option['type'] == 'text' || $order_option['type'] == 'textarea' || $order_option['type'] == 'date' || $order_option['type'] == 'datetime' || $order_option['type'] == 'time') {
                        $option_data[$order_option['product_option_id']] = $order_option['value'];  
                    } elseif ($order_option['type'] == 'file') {
                        $option_data[$order_option['product_option_id']] = $this->encryption->encrypt($order_option['value']);
                    }
                }

                $this->session->data['success'] = sprintf($this->language->get('text_success'), $this->request->get['order_id']);

                $this->cart->add($order_product['product_id'], $order_product['quantity'], $option_data);
            }

            $this->redirect($this->url->link('checkout/cart'));
        }
    }

经过调查并在OpenCart网站上发布,结果表明重新排序已经是OpenCart中的一项功能,但我们从未启用过它[或者几个月前有人禁用过它]。我们必须通过添加一个按钮重新启用它,并将重新订购链接归于/catalog/controller/account/order.php下

我们还在我们的中添加了一些自定义逻辑:

if ($order_option['type'] == 'select' || $order_option['type'] == 'radio') {
    $option_data[$order_option['product_option_id']] = $order_option['product_option_value_id'];
} elseif ($order_option['type'] == 'checkbox') {
    $option_data[$order_option['product_option_id']][] = $order_option['product_option_value_id'];
} elseif ($order_option['type'] == 'text' || $order_option['type'] == 'textarea' || $order_option['type'] == 'date' || $order_option['type'] == 'datetime' || $order_option['type'] == 'time') {
    $option_data[$order_option['product_option_id']] = $order_option['value'];
} elseif ($order_option['type'] == 'file') {
    $option_data[$order_option['product_option_id']] = $this->encryption->encrypt($order_option['value']);
}
因为我们有一个名为checkbox+quantity的自定义选项类型。对于将来的用户,请确保将自定义选项类型逻辑添加到此逻辑语句中


谢谢Jay指出这一点。

谢谢!昨晚我发现了,哈哈。我们不得不在elseif中对选项类型做一些修改,因为我们有一个名为checkbox+quantity的自定义类型。但这是微不足道的。非常感谢,杰。
if ($order_option['type'] == 'select' || $order_option['type'] == 'radio') {
    $option_data[$order_option['product_option_id']] = $order_option['product_option_value_id'];
} elseif ($order_option['type'] == 'checkbox') {
    $option_data[$order_option['product_option_id']][] = $order_option['product_option_value_id'];
} elseif ($order_option['type'] == 'text' || $order_option['type'] == 'textarea' || $order_option['type'] == 'date' || $order_option['type'] == 'datetime' || $order_option['type'] == 'time') {
    $option_data[$order_option['product_option_id']] = $order_option['value'];
} elseif ($order_option['type'] == 'file') {
    $option_data[$order_option['product_option_id']] = $this->encryption->encrypt($order_option['value']);
}