admin管理员组文章数量:1124688
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 '/?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 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
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
Share Improve this question edited Mar 1, 2024 at 9:24 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Mar 1, 2024 at 0:17 Martin OrešanskýMartin Orešanský 275 bronze badges1 Answer
Reset to default 0To 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');
本文标签:
版权声明:本文标题:theme development - WooCommerce custom add to cart buttons does not add products to cart if you're logged out 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736632427a1945805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论