-
-
Save kartikparmar/e899ee7a7b8ee11ce56391d06815ee33 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
* Automatically adding the product to the cart. | |
*/ | |
function aaptc_add_product_to_cart( $item_key, $product_id ) { | |
$product_category_id = 123; // cricket bat category id | |
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' ); | |
if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) { | |
$free_product_id = 12989; // Product Id of the free product which will get added to cart | |
$found = false; | |
//check if product already in cart | |
if ( sizeof( WC()->cart->get_cart() ) > 0 ) { | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->get_id() == $free_product_id ) | |
$found = true; | |
} | |
// if product not found, add it | |
if ( ! $found ) | |
WC()->cart->add_to_cart( $free_product_id ); | |
} else { | |
// if no products in cart, add it | |
WC()->cart->add_to_cart( $free_product_id ); | |
} | |
} | |
} | |
add_action( 'woocommerce_add_to_cart', 'aaptc_add_product_to_cart', 10, 2 ); |
Hi kartikparmar,
Thanks very much for your sharing of the code and it is really helpful.
However, may I ask a very newbie question about --- $product_category_id = 123; // cricket bat category id
I''m just wondering if I can add multiple product_category_id (Rather than only a single category ID like"123")?
If yes, what is the correct way to do it?
Much appreciated in advance!
Regards
GrahamDid you found a solution for multiply categories?
Yes. Please check out the below link and let me know if that works for you or not.
Link: https://gist.github.com/kartikparmar/409969d947edb06a91387b6d840d7c01
Tamas
Hi Tamas,
Sorry for the late response.
Below is the link which contains the code as per your requirements. Please let me know if that works for you or not.
https://gist.github.com/kartikparmar/e632be7cd1be8a6c06e3ea608c153721
Hi kartikparmar,
Thanks very much for your sharing of the code and it is really helpful.
However, may I ask a very newbie question about --- $product_category_id = 123; // cricket bat category id
I''m just wondering if I can add multiple product_category_id (Rather than only a single category ID like"123")?
If yes, what is the correct way to do it?
Much appreciated in advance!
Regards
GrahamDid you found a solution for multiply categories?
Yes. Please check out the below link and let me know if that works for you or not.
Link: https://gist.github.com/kartikparmar/409969d947edb06a91387b6d840d7c01
Hi. I'm trying to implement your code to another case but can't figure out what am I doing wrong.
I need to add extra cost (with $multiplier) to user selected shipping method based on product catogory.
So, I need to check if any of products in cart is on $product_category_id
And if so then I need to multiply selected shipping cost to $multiplier value.
This $multiplier should be applied to total shipping cost calculated by shipping method (not to every item in cart).
Can you please help?
Thanks.
<?php
add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
global $woocommerce;
if ( get_field( 'multiplier', 'option' ) ) :
$multiplier = get_field( 'multiplier', 'option' );
else :
$multiplier = 1;
endif;
$product_category_id = array( 86, 21, 47 );
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
$product_category_id_check = false;
foreach( $products as $cart_item ){
$product_id = $cart_item['product_id'];
}
foreach ( $product_category_id as $key => $value ) {
if ( in_array( $value, $product_cats_ids ) ) {
$product_category_id_check = true;
}
}
if ( ! is_admin() && $product_category_id_check ) {
// If Shiptor 1
if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) {
$rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier;
}
// If Shiptor 2
if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) {
$rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier;
}
//If anothee shipping method
if ( isset( $rates['rpaefw_post_calc:16'] )) {
$rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier;
}
}
return $rates;
}
Hi kartikparmar,
Thanks very much for your sharing of the code and it is really helpful.
However, may I ask a very newbie question about --- $product_category_id = 123; // cricket bat category id
I''m just wondering if I can add multiple product_category_id (Rather than only a single category ID like"123")?
If yes, what is the correct way to do it?
Much appreciated in advance!
Regards
GrahamDid you found a solution for multiply categories?
Yes. Please check out the below link and let me know if that works for you or not.
Link: https://gist.github.com/kartikparmar/409969d947edb06a91387b6d840d7c01Hi. I'm trying to implement your code to another case but can't figure out what am I doing wrong.
I need to add extra cost (with $multiplier) to user selected shipping method based on product catogory.
So, I need to check if any of products in cart is on $product_category_id
And if so then I need to multiply selected shipping cost to $multiplier value.This $multiplier should be applied to total shipping cost calculated by shipping method (not to every item in cart).
Can you please help?
Thanks.<?php add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 ); function overwrite_shipping_cost($rates, $package ) { global $woocommerce; if ( get_field( 'multiplier', 'option' ) ) : $multiplier = get_field( 'multiplier', 'option' ); else : $multiplier = 1; endif; $product_category_id = array( 86, 21, 47 ); $product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' ); $product_category_id_check = false; foreach( $products as $cart_item ){ $product_id = $cart_item['product_id']; } foreach ( $product_category_id as $key => $value ) { if ( in_array( $value, $product_cats_ids ) ) { $product_category_id_check = true; } } if ( ! is_admin() && $product_category_id_check ) { // If Shiptor 1 if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) { $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier; } // If Shiptor 2 if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) { $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier; } //If anothee shipping method if ( isset( $rates['rpaefw_post_calc:16'] )) { $rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier; } } return $rates; }
Thank you for sharing the code.
Please check the code at the below link. I have modified it based on your requirements but I haven't tested it. Hopefully, that will work for you. Please let me know the result.
https://gist.github.com/kartikparmar/5f8a969d9261e010b0645a8ace9f500e
hi, could you tell me, how to implement this code to my source code with woocommerce ? thank you so much. I really like this code
hi, could you tell me, how to implement this code to my source code with woocommerce ? thank you so much. I really like this code
You can add this in your currently active theme's functions.php file and change the product ids according to your requirements.
This one is also added in the same link. :)
Link: https://gist.github.com/kartikparmar/409969d947edb06a91387b6d840d7c01