0

Help me pls. I need update database table in Drupal 8. Exemple on image. enter image description here

When user click on button "ADD PRODUCT TO CART", cart value must update on page (its not a problem), cart value and user ip-address must be write to database. Im try use ajax for this logic, but values do not write to database. Js code:

jQuery('.cart button').on('click', function () {
var price = jQuery('.price').text();
var priceValue = parseInt(price.substring(0, price.length - 1));
var cart = jQuery('#cart');
var cartValue = parseInt(cart.text().substring(0, cart.text().length - 1));
cart.text(cartValue + priceValue + '$');

jQuery.getJSON("http://jsonip.com/?callback=?", function (data) {
    var ip = data.ip.toString();

    jQuery.ajax({
        type: "POST",
        url: "../myupdate.php",
        data: ["ip="+ip, "cart="+jQuery('#cart').text()]
    }).done(function () {
        console.log('done');
    }).error(function () {
        console.log('error');
    });

});

});

php code:

<?php
$ip = $_POST[ip];
$cart = $_POST[cart];
db_insert('abweb')
    ->fields(array('ip' => $ip, 'product' => 'some product', 'price' => $cart))
    ->execute();

This logic is implemented in the custom module. HELP PLS!!

1 Answer 1

0

Here is way to do this. use line item type and create your filed. Hook_commerce_cart_product_add() call in custom module.

  function mymodule_commerce_cart_product_add($order, $product, $quantity, $line_item) {

         $commerce_line_item = commerce_line_item_load($line_item->line_item_id);

         $field_main = $product->field_main['und']['0']['value'];

         $line_item->field_update[LANGUAGE_NONE][0]['value'] = $field_main;

         commerce_line_item_save($line_item);
     }

When user click on button "ADD PRODUCT TO CART", cart value add/update your commerce line item field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.