简体   繁体   English

使用wordpress woocommerce创建自定义“添加到购物车”

[英]Create custom 'Add to cart' with wordpress woocommerce

I fetch a specific product in a page that is outside of woocommerce template and I need to add the 'Add to cart' button. 我在一个不在woocommerce模板中的页面中获取特定产品,我需要添加“添加到购物车”按钮。 I looked at the code and I saw woocommerce doing it this way, 我查看了代码,我看到了woocommerce这样做,

<a href="/?product_cat=icecream&amp;add-to-cart=77" rel="nofollow" data-product_id="77" data-product_sku="" class="button add_to_cart_button product_type_simple">Add to cart</a>

I tried to change the product_id and everything to be fit to my needs like this, 我尝试更改product_id以及所有符合我需求的内容,

<a href="/?product_cat=icecream&amp;add-to-cart=<?php echo $id; ?>" rel="nofollow" data-product_id="<?php echo $id; ?>" data-product_sku="" class="button add_to_cart_button product_type_simple">
    <img src="<?php echo get_bloginfo('template_directory'); ?>/images/add_to_cart.png" alt="Add to cart" />
</a>

but it dosen't get saved for some reason (when I get into cart page its empty). 但由于某些原因它不会被保存(当我进入购物车页面时它是空的)。 I'm trying also to lose the "View Cart" button that gets near when the item is added, if someone can guide me how can I create that button and where to shall I redirect it, it would be perfect (: 我也试图在添加项目时丢失接近的“查看购物车”按钮,如果有人可以指导我如何创建该按钮以及在哪里重定向它,那将是完美的(:

thanks ! 谢谢 !

Well, you can do it with ajax, you can redirect it to - '?add-to-cart=' and the item will get into the cart. 好吧,你可以用ajax做,你可以将它重定向到 - '?add-to-cart =',该项目将进入购物车。 so you will be able to design you'r button however you want .. 所以你可以随心所欲地设计你的按钮..

It only works in shop page this way. 它只适用于商店页面。 Try this for other pages: 试试其他页面:

$product = get_product(77);
echo "<a href='" . $product->add_to_cart_url() ."'>add to cart</a>";

You can try using the add to cart shortcode. 您可以尝试使用添加到购物车的短代码。

<?php echo do_shortcode( '[add_to_cart id=' . $id . ']' ) ?>

Assuming you already have $id. 假设你已经拥有$ id。

Shortcode already gives you the AJAX functionality. Shortcode已经为您提供了AJAX功能。

Try this: 尝试这个:

<?php
global $product;
$pid = $product->get_id();
?>
<a href="<?php echo do_shortcode( '[add_to_cart_url id=' . $pid . ']' ) ?>" class="your-classes-here">Add to cart</a>

You'll find more info here: https://docs.woocommerce.com/document/woocommerce-shortcodes/ 您可以在此处找到更多信息: https//docs.woocommerce.com/document/woocommerce-shortcodes/

Hope you'd find this useful. 希望你会发现这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress WooCommerce - 带有附加参数的自定义添加到购物车 URL - WordPress WooCommerce - Custom Add-To-Cart URL with Add ons parameter 在WooCommerce中将自定义产品添加到购物车 - Add custom product to cart in WooCommerce WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart WooCommerce:使用 WordPress 自定义字段为特定页面添加添加到购物车旁边的自定义按钮 - WooCommerce: Add for specific pages a custom button next to Add to Cart using WordPress Custom Fields 从Wordpress外部将产品添加到woocommerce中的购物车 - add product to cart in woocommerce from outside Wordpress 在 WordPress Woocommerce 中隐藏添加到购物车按钮 - Hide Add To cart Button in WordPress Woocommerce 在 Woocommerce 中添加到购物车操作时,将自定义选择的日期添加到购物车项目 - Add a custom chosen date to cart item on add to cart action in Woocommerce 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce 自定义Woocommerce添加到购物车不在Firefox中,但在Chrome中 - Custom Woocommerce add to cart not working in Firefox but in Chrome 通过add_to_cart的WooCommerce自定义字段 - WooCommerce Custom Fields through add_to_cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM