【问题标题】:Opencart add product options to opencartOpencart 将产品选项添加到 opencart
【发布时间】:2014-07-31 01:25:11
【问题描述】:

我的购物车似乎可以正常工作,但产品选项除外。当我单击添加购物车按钮时,该项目被添加,但没有添加任何选项。我真的不明白为什么会发生这种情况,因为我使用option_idoption_value_id 将选项作为函数要求作为数组提交

点击按钮时调用的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

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

array(1) { [13]=> int(60) }

【问题讨论】:

    标签: javascript php opencart options cart


    【解决方案1】:

    First Option($key=>value) 你传递了 $key => 13 这应该是有效的键

    Option($key=&gt;$Value) 的数组中,其中$key 代表product_option_id$value 代表product_option_value 表的Product_option_value_id,因此这些应该是有效的,当您将选项分配给产品而不是静态ID 时动态分配。

    **Second**只要使用opencart的默认方法,其他输入类型也会处理

    $('#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'); 
                }   
            }
        });
    });
    

    【讨论】:

    • 这是使用选项数组的唯一方法。例如,我有一个 $option[$option_id] = $value 的选项数组,它返回类似 Array ( [14] => 358762050821738 ) 并且我的购物车会话数据是 Array ( [60:YToxOntpOjE0O3M6MTU6IjM1ODc2MjA1MDgyMTczOCI7fQ==:] => 1 )该选项是使用来自用户的文本框输入的文本类型。
    • 已解决,谢谢,选项数组必须是 $option[$product_option_id] = $value;我必须使用选项 ID 和产品 ID 从数据库中调用产品选项 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多