[英]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&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&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.