Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.3 kB
2
Indexable
Never
add_filter( 'woocommerce_coupons_enabled', function($val){
    return true;
}, 100, 1);
add_action('wp', function(){
    add_action('woocommerce_cart_actions', function(){
        $user_id = get_current_user_id();
        $user_id = b2bking()->get_top_parent_account($user_id);
        if (user_has_offer_in_cart() !== 'yes'){
            if (get_transient('b2bking_replace_prices_quote_user_'.$user_id) === 'yes'){
                ?>
                    <img class="b2bking_loader_hidden" src="<?php echo plugins_url('../includes/assets/images/loader.svg', __FILE__); ?>">

                    <button type="button" class="b2bking_add_cart_to_purchase_list_button button">
                        <?php echo apply_filters('b2bking_save_as_purchase_list_text', esc_html__( 'Save as purchase list', 'b2bking' )); ?>
                    </button>
                <?php
            }
        }
    });
}); 
function user_has_offer_in_cart(){
    $has_offer = 'no';
    $offer_id = intval(get_option('b2bking_offer_product_id_setting', 0));
    
    if (is_object( WC()->cart )){
        foreach(WC()->cart->get_cart() as $cart_item){
            if ($cart_item['product_id'] === $offer_id){
                $has_offer = 'yes';
            }
        }
    }
    return $has_offer;
}

add_action('wp_loaded', function(){
    global $b2bking_public;
    remove_filter( 'woocommerce_is_purchasable', array($b2bking_public, 'b2bking_disable_purchasable_except_offers'), 999, 2);
    remove_filter( 'woocommerce_variation_is_purchasable', array($b2bking_public, 'b2bking_disable_purchasable_except_offers'), 999, 2);
}); 
add_action('wp_footer', function(){
    $has_offer = false;
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item){
        $offer_id = intval(get_option('b2bking_offer_product_id_setting', 0));
        if ($cart_item['product_id'] === $offer_id ){
            $has_offer = true;
        }
    }
    if ($has_offer){
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item){
            $offer_id = intval(get_option('b2bking_offer_product_id_setting', 0));
            if ($cart_item['product_id'] !== $offer_id && !isset($cart_item['b2bking_is_offer_child'])){
                // Remove item here
                WC()->cart->remove_cart_item($cart_item_key);
            }
        }
    }
});
Leave a Comment