Opencart add product options to opencart

心已入冬 提交于 2019-12-31 04:09:08

问题


My cart appears to be working except the product options. When I click add cart button then the item gets added, but no options are added with it. I really don't understand why this is happening as I have submitted the options as an array just as the function requires, using option_id and option_value_id

JavaScript called when button is clicked

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

        }  

Here is the var_dump of the options array

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

回答1:


First Option($key=>value) where you have passed $key => 13 which should be valid key

in array of Option($key=>$Value) where $key represents product_option_id and $value represents Product_option_value_id of product_option_value table so these should be valid which is assigned dynamically when you assign option to product rather than static id.

**Second** Just use the default method of opencart, this will handle other input type as well

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


来源:https://stackoverflow.com/questions/24138334/opencart-add-product-options-to-opencart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!