How to create add for comparison in Wordpress?

I do not want to use any plugin. I tried to create a comparison page using a cookie, but still a problem. Can someone please tell me how can I create Compare Products in Wordpress?

Using Loop I get the following output, where I added an “ add to compare ” link or button

<table>
<tr>
  <td> Product ID</td>
  <td>Product Name</td>
  <td><a href="#">Add To Compare</a></td>
</tr>
<tr>
  <td> 123</td>
  <td>P-one</td>
  <td><a href="#">Add To Compare</a></td>  
</tr>
<tr>
  <td>234</td>
  <td>P-2</td>
  <td><a href="#">Add To Compare</a></td>
</tr>
<tr>
  <td>345</td>
  <td>P-3</td>
  <td><a href="#">Add To Compare</a></td>
</tr>
<tr>
  <td>456</td>
  <td>P-4</td>
  <td><a href="#">Add To Compare</a></td>
</tr>
  </table>
Run codeHide result

My logic is not clear. I do not know what I need to do to add a link to compare the product page. But I tried the following steps to create a product product page:


I created two custom message types:

  • Main product
  • Additional product

- . , - .

- Sub Products - , , .

BARATA (: P), :

​​ , WIDTH, LENGTH, HEIGHT.

,

<table>
            <thead>
                <tr>
                    <th><h4>Product Name</h4></th>
                    <th><h4>Any Info</h4></th>
                    <th><h4>Select to Compare</h4></th>

                </tr>
            </thead>
            <tbody>
<?php
$childargs = array(


'orderby' => 'post_title',
'order' => 'ASC',
);
$compi = 1;
 $child_posts = types_child_posts("subproducts",$childargs);
foreach ($child_posts as $child_post) {  

$variid = $child_post->ID;
$variname = "compareid".$compi++;
?>


            <tr>
                    <td><?php echo $child_post->post_title; ?></td>
                    <td><?php echo get_post_meta($child_post->ID, 'any-info-cutomfield', TRUE);  ?></td>
                    <th><a href="" onclick="setting_my_first_cookie('<?php echo $variid; ?>','<?php echo $variname; ?>')">Compare</a>

<a href="" onclick="setting_my_first_cookie_delete('<?php echo $variid; ?>','<?php echo $variname; ?>')">Remove</a>
</th>

                </tr>
            </tbody>
<?php
// Accessing an individual cookie value
echo $_COOKIE[$variid];
echo $_COOKIE[$variname];

?>

  <?php 
} 

?>

Add . $variid $variname.

Onclick javascript cookie.

JavaScript:

<script type="text/javascript"> 

function setting_my_first_cookie(variid,variname) {
 var d = new Date();
    d.setTime(d.getTime() + (30*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = variname + "=" + variid + "; " + "expires; path=/; domain=.domain.com";
}
</script>

<script type="text/javascript"> 

function setting_my_first_cookie_delete(variid,variname) {
   document.cookie = variname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/; domain=.domain.com';
}
</script>

, . . , , .

, . ? ? , .

+4
2

$_SESSION, . / ( ):

$_SESSION['barata_products'] = [];

function add_products(varid) {
    array_push($_SESSION['barata_products'],varid);
}

function remove_product(varid) {
    $product_index = array_search(varid,$_SESSION['barata_products']);
    unset($_SESSION['barata_products'][$product_index]);
}

function remove_all_products() {
    $_SESSION['barata_products'] = [];
}

//helper function to check if the session array is greater than your desired amount, where x is number of products within the array.
function more_than_x(session,x) {
    $_SESSION['barata_products'].count() > x ? true : false
}

Wordpress,

0

ajax , :

$(document).ready(function(){
  $('.product').click(function(){
    var product_id = $(this).attr('id');
     $.ajax({
     method: 'POST',
     url : "YOUR PHP FILE NAME",
     data : {product_id:product_id},
     success : function(resp){
     alert("Product is added to be compared");
     }
     });
  });
});

PHP FILE CODE:

 <?php 
  session_start();
  $product_id = $_POST['product_id'];
  if(!is_array($_SESSION['ids']))
  {
   $_SESSION['ids'] = array();
  }
  else
  {
    array_push($_SESSION['ids'], $product_id);
  }
 ?>

$_SESSION, .

0

Source: https://habr.com/ru/post/1624472/


All Articles