首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Opencart向opencart添加产品选项

Opencart向opencart添加产品选项
EN

Stack Overflow用户
提问于 2014-06-10 10:12:29
回答 1查看 2.3K关注 0票数 1

我的手推车似乎在工作,除了产品的选择。当我单击add cart按钮时,项目将被添加,但没有添加任何选项。我真的不明白为什么会发生这种情况,因为我按照函数的要求,使用option_idoption_value_id将选项作为数组提交

单击按钮时调用JavaScript

代码语言:javascript
复制
$('#button-cart').on('click', function() {
    var model_select = $('#model option:selected').val();

    alert("working");
    $.ajax({
        url: '<?php echo $action?>',
        type: 'post',
        data: {'option' : $('#network option:selected').val(),'product_id': model_select, 'ajax':'1'},
        success: function(json) {
            $('.success, .warning, .attention, information, .error').remove();

            if (json['error']) {
                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                    }
                }
            } 

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({ scrollTop: 0 }, 'slow'); 
            }   
        }
    });
    });

PHP

代码语言:javascript
复制
if (isset($_REQUEST['product_id']) && isset($_REQUEST['option'])) {
            $product_id = $_REQUEST['product_id'];
            $option=array("13" => (int)$_REQUEST['option']);
            var_dump($option);
            $this->cart->add($product_id,$quantity=1,$option);
            print_r($this->session->data['cart']);

        }  

下面是选项数组的var_dump

代码语言:javascript
复制
array(1) { [13]=> int(60) }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-11 10:58:14

First Option($key=>value),其中您通过了$key => 13,它应该是有效的密钥。

Option($key=>$Value)数组中,$key表示product_option_id$value表示product_option_value表的Product_option_value_id,因此这些表应该是有效的,在将选项分配给product而不是静态id时应该是动态分配的。

**秒*只使用opencart的默认方法,这也将处理其他输入类型。

代码语言:javascript
复制
$('#button-cart').bind('click', function() {
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
        dataType: 'json',
        success: function(json) {
            $('.success, .warning, .attention, information, .error').remove();

            if (json['error']) {
                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                    }
                }

                if (json['error']['profile']) {
                    $('select[name="profile_id"]').after('<span class="error">' + json['error']['profile'] + '</span>');
                }
            } 

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({ scrollTop: 0 }, 'slow'); 
            }   
        }
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24138334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档