e commerce - YITH Wishlist plugin displays the same product id for every product

admin2025-01-08  4

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed last month.

Improve this question

Hello fellow stack exchangers! I seem to be having a problem using the YITH Wishlist plugin for adding a wishlist feature to my products in a custom theme I am developing. When I hover over the heart icon on each product (i.e. the wishlist icon), I see the same product id (66) for every product in my in the URL at the bottom of the browser. Here is the code I used for making this table:

<table class="specs-table">
  <tr class="t-row spec-headings">
    <th class="t-header spacer"></th>
    <!-- ...Other Headers... -->
    <th class="t-header"></th>
  </tr>
  <?php                         
                            
    $connection = mysqli_connect("localhost", "root", "root", "local");

    $sql = "
            SELECT * FROM wp_posts
            WHERE post_status = 'publish'
            AND post_type = 'product'
        ";
    
    $result = mysqli_query($connection, $sql);

    if(mysqli_num_rows($result) > 0) {
       while ($row = mysqli_fetch_assoc($result)) {
       // Retrieve ACF custom field from the database directly
         $post_id = $row['ID'];
        //The WooCommerce Product Object
         $product = wc_get_product( $post_id );
                                
        // Product Specs
        $partNumber = get_post_meta( $post_id, 'acf_part_number', true);
        /* ...More specs... */

    ?>
    <tr class="t-row specs">
      <td class="t-value p_thumb"><img class="listing-image" 
      <!-- ...More Table Columns... -->                                         
      <td class="t-value pa">
      <!-- Add to Cart -->
      <?php 
        echo apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        $product->is_purchasable() ? 'add_to_cart_button' : '',
        esc_attr( $product->get_type() ),
        esc_html( $product->add_to_cart_text() )
        ),
        $product );
     ?>
     <!-- Add to Wishlist -->
     <div class="wishlist-shortcode">                                                   
        <?php echo do_shortcode("[yith_wcwl_add_to_wishlist wishlist_url='/my-wishlist/']"); 
        ?> 
     </div>
   </td>
 </tr>
                                            
  <?php
  }
} else {
  ?>
<tr>
 <td colspan="3">Data is not available</td>
</tr>
  <?php
   }
?>

</table>

More code available here.

I tried a couple of things in order to fix this problem. One of them was studying the engineering of the plugin, another was googling. I also tried using this PHP function (which of course only yielded text):

<?php 
  $wishlist = file_get_contents(
        "content-single-product.php",
        true,
        null,
        1910,
        112
  );
  echo $wishlist;
?>

I realise that not using this plugin's shortcode within the product loop probably isn't the standard way of using it. However, the situation didn't leave me with any other choice (unless of course I develop a wishlist feature myself). Any advise here will be greatly appreciated.

Here's some basic info about the environment:

  • Website Builder: WordPress 6.7.1 + WooCommerce
  • Server: Nginx (using Local)
  • Programming Language: PHP 8.1.23
  • PLugin used: YITH Wishlist
  • Operating System: ZorinOS Lite 16.3
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed last month.

Improve this question

Hello fellow stack exchangers! I seem to be having a problem using the YITH Wishlist plugin for adding a wishlist feature to my products in a custom theme I am developing. When I hover over the heart icon on each product (i.e. the wishlist icon), I see the same product id (66) for every product in my in the URL at the bottom of the browser. Here is the code I used for making this table:

<table class="specs-table">
  <tr class="t-row spec-headings">
    <th class="t-header spacer"></th>
    <!-- ...Other Headers... -->
    <th class="t-header"></th>
  </tr>
  <?php                         
                            
    $connection = mysqli_connect("localhost", "root", "root", "local");

    $sql = "
            SELECT * FROM wp_posts
            WHERE post_status = 'publish'
            AND post_type = 'product'
        ";
    
    $result = mysqli_query($connection, $sql);

    if(mysqli_num_rows($result) > 0) {
       while ($row = mysqli_fetch_assoc($result)) {
       // Retrieve ACF custom field from the database directly
         $post_id = $row['ID'];
        //The WooCommerce Product Object
         $product = wc_get_product( $post_id );
                                
        // Product Specs
        $partNumber = get_post_meta( $post_id, 'acf_part_number', true);
        /* ...More specs... */

    ?>
    <tr class="t-row specs">
      <td class="t-value p_thumb"><img class="listing-image" 
      <!-- ...More Table Columns... -->                                         
      <td class="t-value pa">
      <!-- Add to Cart -->
      <?php 
        echo apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        $product->is_purchasable() ? 'add_to_cart_button' : '',
        esc_attr( $product->get_type() ),
        esc_html( $product->add_to_cart_text() )
        ),
        $product );
     ?>
     <!-- Add to Wishlist -->
     <div class="wishlist-shortcode">                                                   
        <?php echo do_shortcode("[yith_wcwl_add_to_wishlist wishlist_url='http://wj-parts.local/my-wishlist/']"); 
        ?> 
     </div>
   </td>
 </tr>
                                            
  <?php
  }
} else {
  ?>
<tr>
 <td colspan="3">Data is not available</td>
</tr>
  <?php
   }
?>

</table>

More code available here.

I tried a couple of things in order to fix this problem. One of them was studying the engineering of the plugin, another was googling. I also tried using this PHP function (which of course only yielded text):

<?php 
  $wishlist = file_get_contents(
        "content-single-product.php",
        true,
        null,
        1910,
        112
  );
  echo $wishlist;
?>

I realise that not using this plugin's shortcode within the product loop probably isn't the standard way of using it. However, the situation didn't leave me with any other choice (unless of course I develop a wishlist feature myself). Any advise here will be greatly appreciated.

Here's some basic info about the environment:

  • Website Builder: WordPress 6.7.1 + WooCommerce
  • Server: Nginx (using Local)
  • Programming Language: PHP 8.1.23
  • PLugin used: YITH Wishlist
  • Operating System: ZorinOS Lite 16.3
Share Improve this question edited Nov 22, 2024 at 19:45 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Nov 22, 2024 at 12:31 BrigantiumXIXBrigantiumXIX 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I shouldn't really be answering this, as third party plugins are off topic for this stack, but it's a pretty simple fix.

The reason it's happening is as you surmised because you're using it outside of the Loop. You could solve it by running setup_postdata($post_id) before you echo your products, but that doesn't solve the deeper issue: if you're running MySQL queries in a template you're doing something very wrong (especially raw, unparameterized queries which are a huge security hole). I see no reason in the code above why you couldn't use the WP_Query class to fetch your data, which would be much safer and take care of setting up Post objects for you.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267219a1180.html

最新回复(0)