Support » Plugin: WooCommerce » Add duplicate product item to cart with modified price

  • Resolved CubicInfinity

    (@cubicinfinity)


    I’m trying to implement a buy one get one, bogo, deal on a product category by adding a duplicate of the item to the cart when the product is added or the cart is modified. I have this working with synchronized quantities, but I’m stuck on how to make the free items free while the originals maintain their normal price.

    An example of a cart with this working (the bogo is applied to Category B):
    Product 5, Category A, quantity 1, $9.99
    Product 2, Category A, quantity 3, $6.50
    Product 16, Category B, quantity 8, $2.99
    Product 12, Category B, quantity 1, $10.99
    Product 16, Category B, quantity 8, $0.00
    Product 12, Category B, quantity 1, $0.00

    When I use $cart_item->set_price(), where $cart_item is an item in WC()->cart->get_cart() or WC()->cart->cart_contents, the price appears to be set for all products in the cart with the same product/variation ID. I’ve tried lots of ways to modify only the free ones, for which I set meta data to help differentiate, but they either don’t work or run into this problem of setting all of the items belonging to the same product.
    I’ve considered manipulating $cart_item['data']['changes']['price'] more directly and with different ways of indexing, but set_prop() is a protected function of the product itself and I’m not sure how to get around that, if it would even work.
    I’ll need to continue learning more about PHP and WooCommerce, but this problem keeps getting more complicated the deeper I go into it, so I’m asking for help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • @cubicinfinity

    I understand you’re trying to implement a BOGO feature.

    We have a solution for this here: https://woocommerce.com/products/buy-one-get-one-free/

    For more details on how it works, please contact us at WooCommerce.com > My Account > Support. You may need to create an account before you can access that page.

    Please include a link to this forum thread, so that we can keep track of what’s already been done.

    We will be able to help you further there.

    Thread Starter CubicInfinity

    (@cubicinfinity)

    But what if I want to make something as efficient as possible that does just what I want it to do and nothing more?

    Side rant: I wish they didn’t make the WooCommerce topic “offtopic” on the WordPress StackExchange, because this type of support is inferior. Whatever happened to users just helping each other out?

    • This reply was modified 10 months, 1 week ago by CubicInfinity.
    Thread Starter CubicInfinity

    (@cubicinfinity)

    I’m reading the source for the plugin you referenced because it should help me.

    Thread Starter CubicInfinity

    (@cubicinfinity)

    Honestly, this plugin is pretty decent. I’m just going to use it. Sure it’s a lot bigger than the single php file I wanted to make, but at least this is user friendly and I’m out of time to keep messing with this.

    @cubicinfinity

    The BOGO feature is an extensible feature not provided by the default plugin. So to extend it yourself needs some custom development or an extra plugin from the WooCommerce marketplace or community.

    In this case, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.`

    Thread Starter CubicInfinity

    (@cubicinfinity)

    Right; by default plugin, I assume you mean WooCommerce. I just wish I had time to be a real developer.
    Sure, if someone has insight to making a bogo snippet, that would be awesome. Every time I or someone else makes something we learn more about WC and can do things with it more easily in the future.

    Plugin Support Mirko P.

    (@rainfallnixfig)

    Hi @cubicinfinity,

    This forum is specifically focused on the default features of WooCommerce core and the support staff does not provide assistance for custom coding in line with our Support Policy.

    Let’s see if developers can chime in to help you out but otherwise kindly consider using an extension like the one mentioned above or posting in more development-oriented channels.

    All the best.

    Plugin Support Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution and the above resources for developers were helpful. If you have further questions, please feel free to open a new topic.

    Thanks.

    Thread Starter CubicInfinity

    (@cubicinfinity)

    You do that.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add duplicate product item to cart with modified price’ is closed to new replies.