R
R
Rosomaha6662020-12-16 01:54:00
WordPress
Rosomaha666, 2020-12-16 01:54:00

"Adventures of Dunno in Wordpress", which does not understand - Why is the product added to the cart when choosing a category?

If you want to read only on the case, the water ends after the heading: THE ESSENCE OF THE QUESTION.

Hello advanced developers! I'm not speaking out of laziness, but out of stupidity. I searched everything, but my mind is not able to understand what is happening ( although I am sure that everything is very simple ).

I can only hope for the literacy of the users of this site.

I am making a website (yes, yes, you are not mistaken the 1st first site)).

ESSENCE OF THE QUESTION

Task
Displaying goods in a list and tiles, with the ability to switch the option for displaying goods.

Solution The
solution was found here

What did I do from the source

1. Copy file from "/wp-content/plugins/woocommerce/templates/content-product.php" to "/ChildTheme/woocommerce/content-product.php"
Created file "/ChildTheme/woocommerce/content-tables.php" and copied the following code into it:

<?php
/**
 * Шаблон табличного отображения товаров в категории
 */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
global $product;
 
// Ensure visibility
if (empty($product) || !$product->is_visible()) {
    return;
}
if (stripos(woocommerce_get_product_thumbnail(), 'placeholder.png') !== FALSE) {
    $styleImage = 'product-table-image-no';
} else {
    $styleImage = 'product-table-image';
}
?>
<tr class="productDescription">
    <td class="<?php echo $styleImage; ?>"><?php echo woocommerce_get_product_thumbnail(); ?></td>
    <td class="product-table-title"><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></td> 
<!--    <td class="product-table-manufactur"><?php echo ($product->get_attribute('manufacture')); ?></td> -->
    <td class="product-table-price"><?php do_action('woocommerce_after_shop_loop_item_title'); ?></td> 
    <td class="product-table-button"><?php do_action('woocommerce_after_shop_loop_item'); ?></td> 
</tr>


3. Created a file "/ChildTheme/woocommerce/archive-product.php" and pasted the code:

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.0
 */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
get_header('shop');
?>
 
<?php
/**
 * woocommerce_before_main_content hook.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 */
do_action('woocommerce_before_main_content');
?>
 
<?php if (apply_filters('woocommerce_show_page_title', true)) : ?>
 
    <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
 
<?php endif; ?>
 
<?php
/**
 * woocommerce_archive_description hook.
 *
 * @hooked woocommerce_taxonomy_archive_description - 10
 * @hooked woocommerce_product_archive_description - 10
 */
do_action('woocommerce_archive_description');
?>
 
 
 
<?php if (have_posts()) : ?>
 
    <?php
    /**
     * woocommerce_before_shop_loop hook.
     *
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action('woocommerce_before_shop_loop');
    ?>
    <div class="templateBlockSelector">
        <a href="<?php echo add_query_arg(array('cat_templete' => 'list')); ?>"><i class="fa fa-list fa-2x" aria-hidden="true"></i></a>
        <a href="<?php echo add_query_arg(array('cat_templete' => 'table')); ?>"><i class="fa fa-table fa-2x" aria-hidden="true"></i></a>
    </div>
    <?php woocommerce_product_loop_start(); ?>
 
    <?php woocommerce_product_subcategories(); ?>
 
    <?php
    global $VIEW_TEMPLATE;
    ?>
    <?php if ($VIEW_TEMPLATE == 'table') { ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php wc_get_template_part('content', 'product'); ?>
        <?php endwhile; // end of the loop. ?>
    <?php } else { ?>
        <table class="productTemplateTables">
            <?php while (have_posts()) : the_post(); ?>
                <?php wc_get_template_part('content', 'tables'); ?>
            <?php endwhile; // end of the loop. ?>
        </table>
    <?php } ?>
 
    <?php woocommerce_product_loop_end(); ?>
 
    <?php
    /**
     * woocommerce_after_shop_loop hook.
     *
     * @hooked woocommerce_pagination - 10
     */
    remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
    do_action('woocommerce_after_shop_loop');
    ?>
 
<?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
 
    <?php wc_get_template('loop/no-products-found.php'); ?>
 
<?php endif; ?>
 
<?php
/**
 * woocommerce_after_main_content hook.
 *
 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
 */
do_action('woocommerce_after_main_content');
?>
 
<?php
/**
 * woocommerce_sidebar hook.
 *
 * @hooked woocommerce_get_sidebar - 10
 */
do_action('woocommerce_sidebar');
?>
 
<?php get_footer('shop'); ?>


+ played around with CSS in the theme settings via the Additional Styles checkbox and got the following result:
5fd9393f86477490637385.jpeg

Everything is super, at this stage I was delighted!!!!

The adventure started on.

I inserted a hook and a function (according to the advice from the source above), to switch the display mode of products (table/tile), namely in "/ChildTheme/functions.php" I inserted this:

add_action('woocommerce_before_shop_loop', 'setTempleteCategory');
 
/**
 * Устанавливает режим просмотра категорий товаров
 * @global type $VIEW_TEMPLATE
 */
function setTempleteCategory() {
    global $VIEW_TEMPLATE;
 
    if (isset($_REQUEST['cat_templete'])) {
        if ($_REQUEST['cat_templete'] == 'list') {
            $VIEW_TEMPLATE = 'list';
            setcookie("view_template_product", "", time() - 3600);
            setcookie('view_template_product', 'list', time() + 60 * 60 * 24 * 30);
        } elseif ($_REQUEST['cat_templete'] == 'table') {
            $VIEW_TEMPLATE = 'table';
            setcookie("view_template_product", "", time() - 3600);
            setcookie('view_template_product', 'table', time() + 60 * 60 * 24 * 30);
        } else {
            $VIEW_TEMPLATE = 'table';
        }
    } else {
        if (isset($_COOKIE['view_template_product'])) {
            if ($_COOKIE['view_template_product'] == 'list') {
                $VIEW_TEMPLATE = 'list';
            } elseif ($_COOKIE['view_template_product'] == 'table') {
                $VIEW_TEMPLATE = 'table';
            }
        } else {
            $VIEW_TEMPLATE = 'table';
        }
    }
}


The switcher worked and works properly, but:

at first, this error came out.

5fd93ca7cefdd144095454.jpeg

Without hesitation, I deleted the lines from the function.php that woocommers did not like (I will say in advance that the main error arose even before they were deleted), namely
setcookie("view_template_product", "", time() - 3600);
            setcookie('view_template_product', 'list', time() + 60 * 60 * 24 * 30);

and
setcookie("view_template_product", "", time() - 3600);
            setcookie('view_template_product', 'table', time() + 60 * 60 * 24 * 30);

from the code that I added to the .php function to select the display mode.

THE MAIN PROBLEM!!!

I calmly switch templates from table to tile and back, everything works fine, BUT as soon as I add an item to the cart, the DOOMSDAY MECHANISM starts, which does not let me sleep!

After we have added any product to the cart (regardless of which table or tile template), each template switch reacts by ADDING the product that we added manually to the cart. And this effect does not end. How many times we switch the mode, so many times the product will be added to the basket.

If we open the basket and remove, for example, 1 product from there, the mechanism will be deactivated and we can again easily switch modes until we add something else to the basket.

I also found out by typing: If you add a product to the basket and then refresh the page, then:

1. when you click on the basket (not the ADD TO CART button, just the basket at the top), the goods will always open in a tiled layout. Removing the cart plugin and installing it myself through the code did not fix the situation, so I returned the code.
2. adding goods is buggy, they are added every other time.

I also want to note that there are only 3 plugins on the site: Advanced search, Cart menu, Pagination styler. I deleted all these plugins, nothing changed. WordPress installed today, no more changes (except for CSS styles and search output through the addition of an additional widget zone - it's probably the code found in the source).

Without the mode switching feature, the site works fine in tabular form. All problems arise precisely in this thing in the .php function But unfortunately, what is written in it is like a Martian manuscript for me ...)))
SITE: https://automechanic24.rf SHOP
tab

thank.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question