Woocommerce-通过JS或PHP以编程方式将产品添加到购物车
-
-
紧密投票为"太本地化".您是否尝试过WooThemes支持?Close-voted as **too localized**. Have you tried WooThemes support?
- 0
- 2012-05-25
- Chip Bennett
-
@Chip Bennett我有,不幸的是,他们收取125美元.这是在Wordpress和Woocommerce的支持下-本身在这里是有效的标签,因此肯定有效吗?除非我误会"太本地化"?@Chip Bennett I have yes, they charge $125 dollars just to join unfortunately. This comes under the bracket of both Wordpress and Woocommerce - itself a valid tag here, so is surely valid? Unless I am misunderstanding 'too localized'?
- 3
- 2012-05-25
- Larry B
-
这个问题与WordPress无关.它仅涉及WooCommerce插件本身-插件功能和自定义插件功能/代码.我本来可以投票否定主题,但是对于有关商业产品或具有商业支持选项的产品,我通常会过于本地化.This question has nothing to do with **WordPress** specifically. It involves only the WooCommerce Plugin itself - Plugin functionality and custom Plugin functions/code. I could have close-voted as **off-topic**, but I usually use **too localized** for questions about commercial products, or products that have commercial support options.
- 0
- 2012-05-25
- Chip Bennett
-
@ChipBennett Woocommerce仅适用于Wordpress,因此与wordpress无关,似乎奇怪.如果不发布有关Woocommerce的问题,人们将如何使用Woocommerce标签?无论如何,我已经找到了答案,以防万一其他人遇到同样的问题.@ChipBennett Woocommerce is only for Wordpress so seems odd to catagorise as nothing to do with wordpress. How would one use the Woocommerce tag if not to post a question about Woocommerce? At any rate I have found the answer which I shall post now in case anyone else has the same problem.
- 12
- 2012-05-25
- Larry B
-
我不是说* WooCommerce *与WordPress无关,而是**您的特定问题**与WordPress无关.两者之间的差异至关重要.I didn't say *WooCommerce* had nothing to do with WordPress, but rather that **your specific question** has nothing to do with WordPress. The difference between the two is critical.
- 0
- 2012-05-25
- Chip Bennett
-
@ChipBennett好吧,我不得不不同意.如果不问有关Woocommerce的问题,为什么会有Woocommerce标签.Woocommerce在任何方面都不是独立的,因此与Woocommerce有关的任何内容都与Wordpress有关.但这当然只是我的意见.@ChipBennett Well I'd have to disagree. Why is there a Woocommerce tag if not to ask questions about Woocommerce. Woocommerce is not a standalone in any way so anything relating to Woocommerce is pertinent to Wordpress. But that's just my opinion of course.
- 0
- 2012-05-25
- Larry B
-
请阅读[有关WPSE问题范围的常见问题解答](http://wordpress.stackexchange.com/faq#questions):" *请注意,我们**不处理**问题... **并非特定于WordPress**(即使它们在其上下文中发生).*""仅仅因为WooCommerce在WordPress上下文中运行并不意味着有关WooCommerce的每个问题都适用于WPSE.Please read [the FAQ regarding scope of questions for WPSE](http://wordpress.stackexchange.com/faq#questions): "*Note that we **do not handle** questions... **not specific to WordPress** (even if they happen in its context).*" Just because WooCommerce runs in the context of WordPress does not mean that every single question about WooCommerce is in-scope for WPSE.
- 0
- 2012-05-25
- Chip Bennett
-
请以**答案**的形式发布答案,而不是以原始问题的*编辑形式发布答案.[**请阅读常见问题解答**](http://wordpress.stackexchange.com/faq),以便您了解StackExchange问答网站的工作方式.Please post answers as **answers**, not as *edits to the original question*. [**Please read the FAQ**](http://wordpress.stackexchange.com/faq) so that you understand how StackExchange Q & A sites work.
- 0
- 2012-05-25
- Chip Bennett
-
我必须等待rep <10才能等待8个小时,于是会这样.I have to wait 8 hours as rep<10 so will then.
- 0
- 2012-05-25
- Larry B
-
@GHarping谢谢您提出这个问题,即使您对此一无所知.:)@GHarping Thank you for asking this question, even though you got a bunch of crap for it. :)
- 13
- 2013-01-09
- Ryan
-
2 个回答
- 投票数
-
- 2012-05-28
好的,这就是我最后解决它的方法.一个快速而肮脏的示例使用了JQuery.
<a id="buy" href="#">Buy this!</a> <script> $('#buy').click(function(e) { e.preventDefault(); addToCart(19); return false; }); function addToCart(p_id) { $.get('/wp/?post_type=product&add-to-cart=' + p_id, function() { // call back }); } </script>
这只是向购物车网址发出AJAX GET请求
/wp/?post_type=product&add-to-cart=[PRODUCT_ID]
OK so here's how I solved it in the end. A quick and dirty example, uses JQuery.
<a id="buy" href="#">Buy this!</a> <script> $('#buy').click(function(e) { e.preventDefault(); addToCart(19); return false; }); function addToCart(p_id) { $.get('/wp/?post_type=product&add-to-cart=' + p_id, function() { // call back }); } </script>
This just makes an AJAX GET request to the cart url
/wp/?post_type=product&add-to-cart=[PRODUCT_ID]
-
这不再起作用了(wc v2.1.12).找到了一个解决方案并在[此处]共享了它(https://wordpress.stackexchange.com/questions/86967/woocommerce-add-variation-product-to-cart-from-custom-link/159390#159390)this is not working anymore (wc v2.1.12). found a solution and shared it [here](https://wordpress.stackexchange.com/questions/86967/woocommerce-add-variation-product-to-cart-from-custom-link/159390#159390)
- 7
- 2014-08-28
- honk31
-
我使用了本教程,可以帮助我实现所需的知识: https://wpharvest.com/add-product-to-cart-programmatically-in-woocommerce/I've used this tutorial which helped me achieve what I needed: https://wpharvest.com/add-product-to-cart-programmatically-in-woocommerce/
- 0
- 2020-03-22
- Dragos Micu
-
- 2013-08-30
在PHP中,我设法这样做:
global $woocommerce; $woocommerce->cart->add_to_cart($product_id);
该方法在woocommerce/classes/class-wc-cart.php中:
/** * Add a product to the cart. * * @param string $product_id contains the id of the product to add to the cart * @param string $quantity contains the quantity of the item to add * @param int $variation_id * @param array $variation attribute values * @param array $cart_item_data extra cart item data we want to pass into the item * @return bool */ public function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
In PHP I managed to do it this way:
global $woocommerce; $woocommerce->cart->add_to_cart($product_id);
The method is in woocommerce/classes/class-wc-cart.php:
/** * Add a product to the cart. * * @param string $product_id contains the id of the product to add to the cart * @param string $quantity contains the quantity of the item to add * @param int $variation_id * @param array $variation attribute values * @param array $cart_item_data extra cart item data we want to pass into the item * @return bool */ public function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
-
我如何通过ajax调用该方法?How could I call that method via ajax?
- 3
- 2013-12-11
- Luca Reghellin
-
@Stratboy通过使用在Wordpress中添加AJAX函数的推荐方法:https://codex.wordpress.org/AJAX_in_Plugins-基本上,您只需在wordpress Codex的第一个php示例中从答案中插入3行即可.@Stratboy by using the recommended way of adding AJAX functions in Wordpress: https://codex.wordpress.org/AJAX_in_Plugins - basically you just insert the 3 lines from the answer in the first php example from the wordpress codex.
- 1
- 2016-11-14
- CodeBrauer
我正在使用Woocommerce插件来促进网站的一小部分电子商务,并且需要通过一些调用或功能将产品添加到购物车中,而不是使用其自己的"添加到购物车"按钮.
这基本上是指,例如,向Woocommerce发送SKU和数量,并进行购物车更新.
等
我已经阅读了文档,似乎找不到对这种东西的引用.有人可以建议我如何实现这一目标吗?