Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. function initAddToCart() {
  2.  
  3. function addToCart() {
  4. console.log('adding to cart...');
  5. window.wct.pageLoader.show();
  6.  
  7. var imagesArray = window.wct.state.images;
  8. var quantity = window.wct.state.quantity;
  9.  
  10. // quantity = 1;
  11. // imagesArray = [
  12. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  13. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  14. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  15. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  16. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  17. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  18. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  19. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  20. // 'http://woodcaketoys.greeto/wp-content/uploads/wctuploads/default-crop-f728ab1c6303ddd21cba1a5b14b05683_wct-grid-image.jpeg',
  21. // ];
  22.  
  23. return $.ajax({
  24. type: 'post',
  25. url: ajaxurl,
  26. dataType: 'json',
  27. data: { action: 'wct_add_product_to_cart', images: imagesArray, quantity: quantity },
  28. success: function (data) {
  29. // console.log(data);
  30. window.wct.pageLoader.hide();
  31. },
  32. error: function (data) {
  33. console.error(data);
  34. },
  35. });
  36.  
  37. }
  38.  
  39. window.wct.addToCart = addToCart;
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. add_action( 'wp_head', 'add_ajax_library' );
  50. function add_ajax_library() {
  51. $html = '<script type="text/javascript">';
  52. $html .= 'var ajaxurl = "'.admin_url('admin-ajax.php').'"';
  53. $html .= '</script>';
  54. echo $html;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. add_action( 'wp_ajax_nopriv_wct_add_product_to_cart', 'wct_add_product_to_cart' );
  61. add_action( 'wp_ajax_wct_add_product_to_cart', 'wct_add_product_to_cart' );
  62. function wct_add_product_to_cart() {
  63.  
  64. $images = $_REQUEST['images'];
  65. $quantity = $_REQUEST['quantity'];
  66.  
  67. // select ID
  68. $product_id = 14;
  69.  
  70. $custom_data = array();
  71. $custom_data['images'] = json_encode($images);
  72.  
  73. // if no products in cart, add it
  74. WC()->cart->add_to_cart( $product_id, $quantity, 0, array(), $custom_data );
  75.  
  76. $result = array(
  77. 'status' => 'ok',
  78. );
  79.  
  80. $result = json_encode($result);
  81. echo $result;
  82.  
  83. exit();
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement