0

I'm actually doing custom template for my client. My client want to have few specific products to show with their add to cart button on homepage. On internet I didn't found the answer for this specific behavior of WooCommerce. I know, basicly add to cart buttons on single pages are <button> elements but I think for my use will be better use <a> element, so I found how looks that construction of add to cart URL.

After this I picked IDs of products which client wants to offer on homepage and did this link: <a href="<?php echo 'https://betlive.cz/?add-to-cart='.get_the_ID().'&quantity=1'; ?>" class="add_to_cart_button btn btn-outline-primary btn-lg">Přidat do košíku</a>

But after applying this link I could not add product to the cart (I've got redirected and getting message that cart is empty) On this discussion on wordpress.org I found that I should add class: add_to_cart to my button which I've done, after this I can successfully add product to cart if I'm logged in as admin, but when I try add any product from homepage in anonymous window through that button, I can't, I'm get redirected to the cart and cart is empty.

Do you have, please any ideas how to debug this stuff? PS: get_the_ID() is working for getting ID of products from my query, I think that query which I'm using to loop through products to show on homepage is right because the button works, but only if I'm logged in as admin.

On the site is not any active WP cache plugin, I've also tried deactivate Wordfence but it still not help, actually I'm using these plugins on the site: WooCommerce 8.5.2, Regenerate Thumbnails 3.1.6, WooCommerce Stripe Gateway 8.0.0 and Font Awesome 4.0.0

1 Answer 1

0

To display specific products with their add-to-cart buttons on the homepage of a Wp site

you can use a shortcode to display specific products.

you can use the [display_specific_products ids="1,2,3"] shortcode in your home pages, or widgets to display the specified products with their add-to-cart buttons.

Replace 1,2,3 with the actual IDs of the products you want to display or change scenario as per your need.

// Add Shortcode
function display_specific_products_shortcode($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts(array(
        'ids' => '', // Comma-separated list of product IDs
    ), $atts);

    // Retrieve product IDs from shortcode attributes
    $product_ids = explode(',', $atts['ids']);

    // Initialize output variable
    $output = '';

    // Loop through product IDs
    foreach ($product_ids as $product_id) {
        // Get product object
        $product = wc_get_product($product_id);

        // Check if product exists and is in stock
        if ($product && $product->is_in_stock()) {
            // Generate HTML output for product
            $output .= '<div class="product">';
            $output .= '<h2>' . $product->get_name() . '</h2>';
            $output .= '<img src="' . $product->get_image() . '" alt="' . $product->get_name() . '">';
            $output .= '<p>' . $product->get_price_html() . '</p>';
            $output .= '<form class="cart" action="' . esc_url(wc_get_cart_url()) . '" method="post" enctype="multipart/form-data">';
            $output .= '<input type="hidden" name="add-to-cart" value="' . esc_attr($product_id) . '">';
            $output .= '<button type="submit" class="button alt">' . __('Add to cart', 'woocommerce') . '</button>';
            $output .= '</form>';
            $output .= '</div>';
        }
    }

    // Return HTML output
    return $output;
}
add_shortcode('display_specific_products', 'display_specific_products_shortcode');
1
  • Hi, I think this solution is not for me. My problem is that add to cart works for admins but not for other unlogged in users and I cannot figure out why. Commented Mar 1 at 8:30

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.