Snippets Not Working
-
Hey there,
I’ve recently been trying to add the following code to the snippet plugin however it doesn’t seem to work. When I add this directly to the functions.php file it’s working fine – can you please advise on what is going wrong here./*
* Automatically add the deposit to the cart when a product in the category ‘pitch’ is added to the cart.
*/function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 79; // pitch 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 = 1991; // Product id of the depoist 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 );/*
* Checking and validating when products are added to cart: max 2 products
*/add_filter( ‘woocommerce_add_to_cart_validation’, ‘only_two_items_allowed_add_to_cart’, 10, 3 );
function only_two_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {
$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;if( $cart_items_count >= 2 || $total_count > 2 ){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( “Sorry, you can only order one pitch at a time.”, “woocommerce” ), “error” );
}
return $passed;
}Thanks
EmmaThe page I need help with: [log in to see the link]
The topic ‘Snippets Not Working’ is closed to new replies.