I new in subscription payments need help regarding to subscription payments. I am using
WC()->cart->add_to_cart( $parent_id );
for adding product to the cart.
How can i add product to cart for monthly or yearly periodically payment.
what parameter should i send ?
WC()->cart->add_to_cart( $parent_id, 'yearly' );
or what ?
The subscription interval is set in the product page.
If you want a subscription to have different intervals and want to choose them grammatically you must create a variable subscription and change the period in each variation.
Then when you add the product to cart you choose what variation are you adding
add_to_cart( $product_id, $quantity, $variation_id);
This is all available parameters of add_to_cart function:
/**
* Add a product to the cart.
*
* #param int $product_id contains the id of the product to add to the cart
* #param int $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 string|bool $cart_item_key
*/
public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
[...]
}
See full Docs in the Woocommerce Subscriptions FAQ
Related
I tried this code, and it works. But it automatically adds the credit to the person who sells the product. So, is it possible to add automatic credit to the person who buys the product, not the person who sells it? I tried to change this code but failed. It may seem easy to you, but my attempts were unsuccessful. I'm a bit new to this field, please excuse me.
/**
* Authomatically Top-up Multi vendor Woocommerce Seller on Complete order
* #param int $order_id
*/
function izzycart_auto_topup_user_on_product_purchase( $order_id ) {
$order = new WC_Order($order_id);
/**
* You may need order total and customer_id to compose
* Transaction message for top, which is why it is added
* below as optional (if needed)
*/
$total = $order->get_total(); // <=== Total order Price (if needed)
$customer_id = $order->get_customer_id(); // <=== Customer ID (if needed)
$order_items = $order->get_items(); // <=== Grab all items in order
$item_details = [];
foreach ( $order_items as $item ) {
// Get the ID of each item in Order
$product_id = $item->get_product_id();
$item_details[] = [
'product_name' => $item->get_name(),
'total' => $item->get_total(),
'author' => get_post($product_id)->post_author // <=== The product/item author ID
];
}
//Loop through all product/items author and add topup their wallet
foreach ($item_details as $item_detail) {
$wallet = new Woo_Wallet_Wallet();
// Get Administrator's percentage
$adminsCut = 0.2 * $item_detail['total']; // <=== Admin Takes 20%
// Get Author's Percentage
$authorsCut = $item_detail['total'] - (0.2 * $item_detail['total']); // <=== Author Takes 80%
//Top-Up Admin
if( $item_detail['author'] != 1 ) {
/**
* By Default Administrator has an ID of 1
* if Author of product is not Admin
*/
$topUpAdmin = $wallet->credit(1, $adminsCut, "Top-Up cut from {$item_detail['product_name']} sales");
//Top-Up Author of Product
$topUpAuthor = $wallet->credit($item_detail['author'], $authorsCut, "Top-Up from {$item_detail['product_name']} purchase");
}else {
// Author of product is Admin. Give admin all the money
$topUpAdmin = $wallet->credit(1, $item_detail['total'], "Top-Up from {$item_detail['product_name']} sales");
}
}
}
add_action( 'woocommerce_order_status_completed', 'izzycart_auto_topup_user_on_product_purchase', 10, 1 );
the code from
I have created 3 tags 1.
Backorder (id 111)
In stock (id 112),
Out of stock (id 113)
I would like these tags to be auto-assigned based on availability.
If I am able to get this working, I'll be using the woocommerce_admin_process_product_object, woocommerce_product_quick_edit_save, woocommerce_product_set_stock, woocommerce_variation_set_stock to capture the possible scenarios for stock status changes.
Below is what I have tried but it gives me a fatal error. If I do not use the if condition, then the tags get assigned but it does not meet my objective. Any advice?
add_action( 'woocommerce_admin_process_product_object', 'mycode_woocommerce_backorder_tag', 10, 2 );
function mycode_woocommerce_backorder_tag ($wc_get_product, $product) {
if ($product->managing_stock() && $product->is_on_backorder(1)) {
$wc_get_product->set_tag_ids(array(111));
$wc_get_product->save();
//wp_set_object_terms ($post_id, 'onbackorder', 'product_tag');
}
}
woocommerce_admin_process_product_object hook has only 1 argument, which is $product
$product->save(); is also not necessary, because this happens automatically
Copied from admin/meta-boxes/class-wc-meta-box-product-data.php
/**
* Set props before save.
*
* #since 3.0.0
*/
do_action( 'woocommerce_admin_process_product_object', $product );
$product->save();
So to answer your question, you get:
// When product is saved in WooCommerce backend
function action_woocommerce_admin_process_product_object( $product ) {
// managing_stock() - returns whether or not the product is stock managed.
// on_backorder() – check if a product is on backorder.
if ( $product->managing_stock() && $product->is_on_backorder(1) ) {
// Product set tag ids
$product->set_tag_ids( array( 111 ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
Hopefully some legend can point me in the right direction.
I am wanting to group users who use certain coupon codes in woocommerce when they purchase. I realise there are plugins such as woocommerce groups which allow you to restrict coupons to user groups but I need this the other way.
Eg: someone uses a coupon code "cricket" when they purchase and this will then add the user to the group "cricket-users".
Thanks V much!
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'cricket' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id()
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
}
}
}
I want to force sell based on the product quantity if the customer adds 5 product 1 accessories should add. if they add more then 5, 2 accessories should add.
so the accessories should add in the multiple of five.
Try this:
add_action( 'woocommerce_add_to_cart', 'prefix_add_additional_product', 10, 6 );
/**
* Adds additional product to cart based on the quantity of an added product
*/
function prefix_add_additional_product( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Get the cart
$cart = WC()->cart->get_cart();
// Nothing, if the cart is empty
if ( 0 == count( $cart ) ) {
return;
}
// Enter the free product ID
$additional_product_id = 96;
// Don't add accessory when adding the same accessory
$added_product_id = 0 < $variation_id ? $variation_id : $product_id;
if ( $added_product_id == $additional_product_id ) {
return;
}
// You can add checks for specific product ID or some other requirements here
$additional_product_quantity = (int) ( $quantity / 5 );
WC()->cart->add_to_cart( $additional_product_id, $additional_product_quantity );
}
https://gist.github.com/vanbo/f42cfa8bdf593013f77dda605a9876bb
The code should add the $additional_product_id to cart with a quantity that matches the amount of times the added product to cart quantity can be devided by 5.
The code is hooked to the add to cart hook.
I follow the solution of this post to get the custom price from the input field with a cookie and it works properly except mini-cart.
My products are added to the cart with AJAX and the mini-cart doesn't load the new value of the cookie. It seems to be cached to the previous value of the input field until you reach to the cart page and after hard reload.
For example, I visit the page where I enter the custom price. The first time I put 20 euros, I press the add to cart button, my product is added to the cart via AJAX and it works well.
If I remove this product with the custom price and try to add it again with a different price at this time, the mini-cart keeps the previous price(20 euros).
So, the question is if there is a way to keep the mini-cart updated with the last inserted price?
Gratuitous self-promotion, but my Name Your Price should automatically work with the mini-cart.
But I think your question is actually asking why the item isn't being considered as unique... and therefore added a second time. The answer is that a different price will render a unique $cart_id see source. With a unique ID the item is not found in the cart, and so it is added again.
To force a 'sold individually' item with different prices to be truly sold individually, you need to change the way the cart ID is generated, by filtering woocommerce_cart_id. Here is how I do it to work with my Name Your Price plugin. You would need to adapt it to your own code.
<?php
/**
* Plugin Name: WooCommerce Name Your Price Sold Individually
* Plugin URI: https://gist.github.com/helgatheviking/a8802255167751a5dd746f83cdfc8716
* Description: Double check enforcement of "Sold Individually" for NYP items
* Version: 1.1.0
* WC requires at least: 2.6.3
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com/
*
* Copyright: © 2016 Kathy Darling
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
function wc_nyp_force_sold_individually( $cart_id, $product_id, $variation_id, $variation, $cart_item_data ) {
// Get the product
$product = wc_get_product( $variation_id ? $variation_id : $product_id );
if ( $product->is_sold_individually() && WC_Name_Your_Price_Helpers::is_nyp($product) ){
$id_parts = array( $product_id );
if ( $variation_id && 0 != $variation_id ) {
$id_parts[] = $variation_id;
}
if ( is_array( $variation ) && ! empty( $variation ) ) {
$variation_key = '';
foreach ( $variation as $key => $value ) {
$variation_key .= trim( $key ) . trim( $value );
}
$id_parts[] = $variation_key;
}
$cart_id = md5( implode( '_', $id_parts ) );
}
return $cart_id;
}
add_filter( 'woocommerce_cart_id', 'wc_nyp_force_sold_individually', 10, 5 );