Woocommerce: agregue un producto al carrito mediante programación a través de JS o PHP
-
-
Cerrado votado como ** demasiado localizado **.¿Hasprobadoel soporte de WooThemes?Close-voted as **too localized**. Have you tried WooThemes support?
- 0
- 2012-05-25
- Chip Bennett
-
@Chip Bennett Tengo que sí,lamentablemente cobran $ 125 dólares solopor unirse.Esto vienebajoel corchete de Wordpress y Woocommerce,en símismo unaetiqueta válida aquí,¿entonces seguramentees válida?¿Amenos queestémalinterpretando 'demasiado localizado'?@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
-
Estapreguntanotiene nada que ver con ** WordPress **específicamente.Soloinvolucraelpropio complemento WooCommerce:funcionalidad del complemento yfunciones/código de complementopersonalizado.Podría haber votado cerca como **fuera detema **,perogeneralmente uso ** demasiado localizado **parapreguntas sobreproductos comerciales oproductos quetienen opciones de soporte comercial.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 Woocommercees solopara Wordpress,por lo quepareceextraño catalogarlo comonada que ver con wordpress.¿Cómo se usaría laetiqueta Woocommerce sinofueraparapublicar unapregunta sobre Woocommerce?En cualquier caso,heencontrado la respuesta quepublicaré ahoraen caso de que alguienmástengaelmismoproblema.@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
-
No dije que * WooCommerce *notuvieranada que ver con WordPress,sino que **tupreguntaespecífica **notiene nada que ver con WordPress.La diferenciaentre los dosesfundamental.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 Bueno,tendría queestaren desacuerdo.¿Por qué hay unaetiqueta de Woocommerce sinoespara hacerpreguntas sobre Woocommerce?Woocommercenoesindependiente deningunamanera,por lo quetodo lo relacionado con Woocommerceespertinentepara Wordpress.Peroesaes solomi opinión,por supuesto.@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
-
Lea [laspreguntasfrecuentes sobreel alcance de laspreguntaspara WPSE] (http://wordpress.stackexchange.com/faq#questions): "* Tengaen cuenta que **nomanejamos **preguntas ... **noespecíficas de WordPress** (incluso si ocurrenen su contexto). * "El hecho de que WooCommerce seejecuteen el contexto de WordPressno significa quetodas laspreguntas sobre WooCommerceestén dentro del alcance de 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
-
Publique las respuestas como ** respuestas **,no como *ediciones de lapregunta original *.[** Lea laspreguntasfrecuentes **] (http://wordpress.stackexchange.com/faq)para comprender cómofuncionan los sitios depreguntas y respuestas de 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
-
Tengo queesperar 8 horas ya que la repeticiónes <10 y luego lo haré.I have to wait 8 hours as rep<10 so will then.
- 0
- 2012-05-25
- Larry B
-
@GHarping Graciaspor hacerestapregunta,apesar de quetiene unmontón debasuraporello.:)@GHarping Thank you for asking this question, even though you got a bunch of crap for it. :)
- 13
- 2013-01-09
- Ryan
-
2 respuestas
- votos
-
- 2012-05-28
Bien,asíes como lo resolví alfinal.Unejemplo rápido y sucio,usa 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>
Esto solo realiza una solicitud AJAX GET a la URL del carrito
/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]
-
esto yanofunciona (wc v2.1.12).encontré una solución y la compartí [aquí] (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
-
He usadoestetutorial queme ayudó a lograr lo quenecesitaba: https://wpharvest.com/add-product-to-cart-programmatic-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
En PHP logré hacerlo deestamanera:
global $woocommerce; $woocommerce->cart->add_to_cart($product_id);
Elmétodoestáen 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() ) {
-
¿Cómopodría llamar aesemétodo através de ajax?How could I call that method via ajax?
- 3
- 2013-12-11
- Luca Reghellin
-
@Stratboy usando laforma recomendada de agregarfunciones AJAXen Wordpress: https://codex.wordpress.org/AJAX_in_Plugins:básicamente,soloinserta las 3 líneas de la respuestaen elprimerejemplo dephp del códice de wordpress.@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
Estoy usandoel complemento Woocommerceparafacilitar unapequeñaparte de comercioelectrónico de un sitio ynecesito agregarproductos a su carrito através de alguna llamada ofunciónen lugar de usar suspropiosbotones de 'agregar al carrito'.
Conestome refierobásicamente aenviar a Woocommerce un SKU y una cantidad,porejemplo,y hacer que se actualiceel carrito.
etc
Revisé la documentación yparece quenopuedoencontrar una referencia aestetipo de cosas.¿Alguienpuede sugerir cómopodría lograresto?