Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // add item to cart on visit
- add_action( 'gens_raf_checkout_check', 'add_product_to_cart' );
- function add_product_to_cart() {
- if (is_admin()) {
- return false;
- }
- $user_info = get_userdata(get_current_user_id());
- $user_email = $user_info->user_email;
- $args = array(
- 'posts_per_page' => -1,
- 'post_type' => 'shop_coupon',
- 'post_status' => 'publish',
- 'meta_query' => array(
- 'relation' => 'AND',
- array(
- 'key' => 'customer_email',
- 'value' => $user_email,
- 'compare' => 'LIKE'
- ),
- array(
- 'relation' => 'OR',
- array(
- 'key' => 'usage_count',
- 'value' => '0',
- 'compare' => 'LIKE'
- ),
- array(
- 'key' => 'usage_count',
- 'compare' => 'NOT EXISTS'
- )
- )
- ),
- );
- $coupons = get_posts( $args );
- if(empty($coupons)) {
- return false;
- }
- $coupon_title = get_the_title($coupons[0]->ID);
- $product_id = 4275;
- $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->id == $product_id )
- $found = true;
- }
- // if product not found, add it
- if ( !$found ){
- WC()->cart->add_to_cart( $product_id );
- WC()->cart->add_discount( $coupon_title );
- }
- } else {
- // if no products in cart, add it
- WC()->cart->add_to_cart( $product_id );
- WC()->cart->add_discount( $coupon_title );
- }
- }
Add Comment
Please, Sign In to add comment