How add product to cart woocomerce by hook? - wordpress

I'm trying to implement the following solution:
add_action('woocommerce_calculate_totals' , 'buy3');
function buy3(WC_Cart $cart){
global $woocommerce;
$woocommerce->cart->add_to_cart(6373,1,7444);
}
but the problem after execute the code all quantity the product add To cart not one quantity

Can you Confirm that Sold individually is un-checked? Because WooCommere add_to_cart() Force the quantity to 1 if sold individually and it also check for existing item in cart
Also Parameters for add_to_cart() function are as below :
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id,$quantity,$variation_id, $variation,$cart_item_data);
According to the code you provided, you have supplied quantity = 1;

Related

Woocommerce Get a particular coupon autogenerated over a product purchase

A particular coupon settings assume a coupon instance auto-generation if a customer purchases a certain product. Actually a new coupon instance was generated as a purchase result. I tried to get that coupon instance object, but got an initial coupon instance which was designed as a template for new coupons auto-generation.
a code snippet I used
$coupon_titles = get_post_meta( $download['product_id'], '_coupon_title', true );
for($i=0; $i<count($coupons); $i++ ){
if($coupons[$i]->post_title == $coupon_titles[0]){
$theCoupon = $coupons[$i];
}
}
When I tried to use $coupon_titles[1] array element in assumption that the first index is for the coupon template, I've got an error stating the absence of such the array element. Appreciate for any clue.

Update ACF custom fields for new WooCommerce order

I'm trying to update 2 ACF custom fields every time an order is created.
We have orders coming from our own website and orders coming from an external marketplace integration. My goal is to differentiate the 2 type of orders using custom fields in the order.
The data for the custom fields is provided by an external marketplace integration. The first field has to be filled with the ordernumber (if it it exists) and the other field is just a simple true/false field.
For website orders we're using "Free shipping". For external orders, the provided shipping details are as follows:
Shipping "Verzending voor bol(xxxxx)"
I'm trying to get the shipping method using the woocomerce_new_order action, and using $order->get_shipping_method()
However, when a new order is created, the custom fields won't update.
This is what I've tried so far:
add_action( 'woocommerce_new_order', 'vliegwerk_bolcom_order_filter', 99, 2);
function vliegwerk_bolcom_order_filter( $order_id, $order ) {
$order = wc_get_order( $order_id );
$verzendmethode = $order->get_shipping_method();
$bol_ordernummer = (int) filter_var($verzendmethode, FILTER_SANITIZE_NUMBER_INT);
if ($bol_ordernummer > 0) {
update_field('field_61fa977c8dff9', $bol_ordernummer, $order_id );
update_field('field_61fa95f594b13', 1, $order_id );
}
}
I already tested the woocommerce_new_order trigger, it works as expected.
I've also tried updating the fields using a foreach loop, and triggering the function with a shortcode. This also works as expected.
However, when combining it all together, the fields won't update. Any help would be appreciated!

How to add custom total of an individual product on order backend page

Basically, on the order edit page, I want to calculate the total of an individual product as price * product_height * quantity_of_products.
reference image
I have used the following hook but this hook is not getting executed.
add_filter( 'woocommerce_data_get_total', 'change_product_total_on_order_edit_page', 10, 2);
function change_product_total_on_order_edit_page ($value, $abc ) {
var_dump($value);
die();
return 15;
}
Use 'woocommerce_order_item_get_total' filter instead of 'woocommerce_data_get_total' because this filter is created dynamically by Woocommerce. The data in the hook is object type. When order items are fetched on orders edit page the object type is order_item.

How to force remove products from WooCommerce cart?

I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

Resources