Untitled
unknown
plain_text
2 years ago
6.0 kB
5
Indexable
add_filter('b2bking_go_to_offers_text', function($text){ return '-> Add Offer to Cart'; }, 10, 1); add_filter('b2bking_offers_link_final', function($link, $offerid){ $link = admin_url( 'admin-ajax.php' ).'?action=addofferajax&offer='.$offerid; return $link; }, 10, 2); add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3 ); function custom_add_to_cart_validation( $valid, $product_id, $quantity ) { global $b2bking_public; if ( $b2bking_public->user_has_offer_in_cart() === 'yes' ) { // If testfunction returns true, prevent the product from being added to the cart wc_add_notice( __( 'Sorry, this product cannot be added to quote while you already have an offer in cart.', 'woocommerce' ), 'error' ); return false; } // Otherwise, allow the product to be added to the cart return $valid; } function addofferajax() { WC()->cart->empty_cart( apply_filters( 'b2bking_empty_cart_on_switch', true ) ); // Do something! // If nonce verification didn't fail, run further $offer_id = sanitize_text_field($_GET['offer']); // Add offer to cart $offer_details = get_post_meta(apply_filters( 'wpml_object_id', $offer_id, 'post' , true), 'b2bking_offer_details', true); $products = explode ('|', $offer_details); $cart_item_data['b2bking_offer_id'] = $offer_id; $cart_item_data['b2bking_offer_name'] = get_the_title(apply_filters( 'wpml_object_id', $offer_id, 'post' , true)); $cart_item_data['b2bking_numberofproducts'] = count($products); $i = 1; foreach($products as $product){ $details = explode(';',$product); // if item is in the form product_id, change title $isproductid = explode('_', $details[0]); if ($isproductid[0] === 'product'){ // it is a product+id, get product title $newproduct = wc_get_product($isproductid[1]); $details[0] = $newproduct->get_name(); //if product is a variation with 3 or more attributes, need to change display because get_name doesnt // show items correctly if (is_a($newproduct,'WC_Product_Variation')){ $attributes = $newproduct->get_variation_attributes(); $number_of_attributes = count($attributes); if ($number_of_attributes > 2){ $details[0].=' - '; foreach ($attributes as $attribute){ $details[0].=$attribute.', '; } $details[0] = substr($details[0], 0, -2); } } if (isset($cart_item_data['b2bking_products_stock'])){ $temp_stock = $cart_item_data['b2bking_products_stock'].$isproductid[1].':'.$details[1].';'; } else { $temp_stock = $isproductid[1].':'.$details[1].';'; // id:quantity;id:quantity } $cart_item_data['b2bking_products_stock'] = $temp_stock; } $cart_item_data['b2bking_product_'.$i.'_name'] = $details[0]; $cart_item_data['b2bking_product_'.$i.'_quantity'] = $details[1]; $cart_item_data['b2bking_product_'.$i.'_price'] = $details[2]; $i++; } $cart_item_data = apply_filters('b2bking_before_add_offer_to_cart', $cart_item_data); // Create B2B offer product if it doesn't exist $offer_id = intval(get_option('b2bking_offer_product_id_setting', 0)); if ( !get_post_status ( $offer_id ) ) { $offer = array( 'post_title' => 'Offer', 'post_status' => 'customoffer', 'post_type' => 'product', 'post_author' => 1, ); $product_id = wp_insert_post($offer); //Set product hidden: $terms = array( 'exclude-from-catalog', 'exclude-from-search' ); wp_set_object_terms( $product_id, $terms, 'product_visibility' ); wp_set_object_terms( $product_id, 'simple', 'product_type' ); update_post_meta( $product_id, '_visibility', 'hidden' ); update_post_meta( $product_id, '_stock_status', 'instock'); update_post_meta( $product_id, '_regular_price', '' ); update_post_meta( $product_id, '_sale_price', '' ); update_post_meta( $product_id, '_purchase_note', '' ); update_post_meta( $product_id, '_product_attributes', array() ); update_post_meta( $product_id, '_sale_price_dates_from', '' ); update_post_meta( $product_id, '_sale_price_dates_to', '' ); update_post_meta( $product_id, '_price', '1' ); update_post_meta( $product_id, '_sold_individually', '' ); // set option to product id update_option( 'b2bking_offer_product_id_setting', $product_id ); $offer_id = intval(get_option('b2bking_offer_product_id_setting', 0)); } $offer_id = apply_filters('b2bking_offer_id_before_add_offer_to_cart', $offer_id, $cart_item_data['b2bking_offer_id']); WC()->cart->add_to_cart( $offer_id, 1, 0, array(), $cart_item_data); do_action('b2bking_after_add_offer_to_cart', $offer_id); // Maybe redirect back to where they came from? wp_redirect( get_permalink( wc_get_page_id( 'cart' ) ) ); exit; } add_action( 'wp_ajax_nopriv_addofferajax', 'addofferajax' ); add_action( 'wp_ajax_addofferajax', 'addofferajax' ); add_action('wp', function(){ if (!is_user_logged_in()){ global $b2bking_public; if (intval(get_option('b2bking_enable_offers_setting', 1)) === 1){ add_action( 'woocommerce_before_calculate_totals', array($b2bking_public, 'b2bking_offer_change_price_cart') ); // Add content to offers endpoint add_action( 'woocommerce_account_'.get_option('b2bking_offers_endpoint_setting','offers').'_endpoint', array($b2bking_public, 'b2bking_offers_endpoint_content') ); // Change product price in the minicart for offers add_filter('woocommerce_cart_item_price', array($b2bking_public, 'b2bking_offer_change_price_minicart'), 10, 3); // Add offer item metadata to order (checkout + backend) add_action( 'woocommerce_checkout_create_order_line_item', array($b2bking_public,'b2bking_add_item_metadata_to_order'), 20, 4 ); // Display offer item metadata in cart add_filter('woocommerce_cart_item_name', array($b2bking_public, 'b2bking_display_metadata_cart'),1,3); // if offer has 1 item, set that item to be offer image add_filter( 'woocommerce_cart_item_thumbnail', array($b2bking_public,'filter_offer_image'), 10, 3); } } });
Editor is loading...
Leave a Comment