xxxxxxxxxx
add_action( 'wp_footer', 'change_qty_script' );
function change_qty_script() {
?>
<script>
jQuery(document).ready(function ($) {
$('#remove_one_item').click(function () {
var current_qty = parseInt($(this).attr('data-in-cart-qty'));
var id = $(this).attr('data-product-id');
var cat_item_key = $(this).attr('data-cart-item-key');
var data = {
product_id: id,
quantity: current_qty - 1,
cat_item_key : cat_item_key
};
var url = wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'update_qty');
$.post(url, data, function (response) {
if (!response) {
return;
}
if (response) {
location.reload();
}
});
});
$('#add_to_cart').click(function () {
var id = $(this).attr('data-product-id');
var cat_item_key = $(this).attr('data-cart-item-key');
var data = {
product_id: id,
quantity: 1,
};
var url = wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart');
$.post(url, data, function (response) {
if (!response) {
return;
}
if (response) {
location.reload();
}
});
});
});
</script>
<?php
}
xxxxxxxxxx
/*
* Add promo literature to cart under certain conditions;
* initially, add LFS booklet to physical orders not already containg the LFS book itself
*/
function add_promo_lit_to_cart() {
global $woocommerce;
$promo_id = 6619; // Product Id of the free product which will get added to cart
$book_id = 6123;
$include = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
// check at least one Central Books item is on the order
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( has_term( 'Central', 'product_cat', $_product->get_id() ) ) {
$include = true;
break;
}
}
// remove promo if its book is on the order
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $book_id) {
$include = false;
break;
}
}
if ( $include == true ) {
WC()->cart->add_to_cart( $promo_id );
}
}
// reset qty to 1 as this function seems to get called twice each time by the hook
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ($items as $cart_item_key => $item) {
if (($item['product_id']) == 6619) {
WC()->cart->set_quantity( $cart_item_key, 1 );
break;
}
}
}
}
add_action( 'woocommerce_cart_totals_after_order_total', 'add_promo_lit_to_cart' );
/*
* Ensure the promo leaflet does not show on the cart page,
* by removing it if the user goes back there after visiting the checkout
*/
function remove_promo_from_cart() {
$basket_page = 65;
if ( is_page($basket_page) ) {
$promo_id = 6619;
$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() == $promo_id ) {
WC()->cart->remove_cart_item($cart_item_key);
break;
}
}
}
}
}
add_action( 'template_redirect', 'remove_promo_from_cart' );
xxxxxxxxxx
add_action( 'woocommerce_after_add_to_cart_button', 'remove_product' );
function remove_product() {
global $product;
$cart_item_key = WC()->cart->generate_cart_id( $product->get_ID() );
$in_cart = WC()->cart->find_product_in_cart( $cart_item_key );
$cart_item_remove_url = wc_get_cart_remove_url( $cart_item_key );
?>
<div class="col s4">
<a class="addtocart_link"
id ="add_to_cart"
title="add_to_cart"
data-product-id="<?php echo $product->get_ID(); ?>"
data-cart-item-key="<?php echo $cart_item_key; ?>">
<span class="action_box fa fa-plus"></span></a>
</div>
<?php
if ( $in_cart ) {
$quantities = WC()->cart->get_cart_item_quantities();
foreach ( $quantities as $key => $quantity ) {
if ( $product->get_ID() == $key ) {
if ( $quantity > 1 ) {
?>
<div class="col s4">
<a id="remove_one_item" class="remove_from_cart" href="#"
data-product-id="<?php echo $product->get_ID(); ?>"
data-in-cart-qty="<?php echo $quantity; ?>"
data-cart-item-key="<?php echo $cart_item_key; ?>"
title="remove_from_cart ">
<span class=" action_box fa fa-minus "></span></a>
</div>
<?php
return;
}
}
}
?>
<div class="col s4">
<a class="remove_from_cart" href="<?php echo esc_url( $cart_item_remove_url ); ?>" title="remove_from_cart ">
<span class=" action_box fa fa-minus "></span></a>
<?php
}
}
xxxxxxxxxx
add_action( 'wc_ajax_update_qty', 'update_qty' );
function update_qty() {
ob_start();
$product_id = absint( $_POST['product_id'] );
$product = wc_get_product( $product_id );
$quantity = $_POST['quantity'];
$cat_item_key = $_POST['cat_item_key'];
WC()->cart->set_quantity( $cat_item_key, $quantity, true );
wp_send_json( 'done' );
}
xxxxxxxxxx
<?php
$cart_item_key = WC()->cart->generate_cart_id( $product->get_ID() );
$in_cart = WC()->cart->find_product_in_cart( $cart_item_key );
if ( $in_cart ) {
$cart_item_remove_url = wc_get_cart_remove_url( $cart_item_key );
?>
<div class="col s4">
<a class="remove_from_cart" href="<?php echo esc_url( $cart_item_remove_url ); ?>" title="remove_from_cart ">
<span class=" action_box fa fa-minus "></span></a>
</div>
<?php
}