Woocmmerce如何只允许某种类型的购物车中的一种产品

时间:2016-02-15 15:16:13

标签: wordpress woocommerce

对我来说这听起来很奇怪,在woocommerce中,无论如何都有一个特定的产品,可以通过产品ID控制,只有一个在购物车中,所以在我们的场景中他们选择一个或另一个,如果他们试图重新添加到购物车,它只需更换或交换购物车中的该项目。 我们的主要产品不会受到这条规则的影响。

1 个答案:

答案 0 :(得分:0)

知道我已经在某个地方贴了这个,但是我找不到它,所以我会重新发布它。这是一个完整的插件(因此将其作为文件添加到wp-content/plugins),这将1.在产品信息元框中添加一个复选框,并且2.如果选中该复选框,则将购物车限制为仅限该项目。

<?php
/*
Plugin Name: WooCommerce Restrict Items in Cart
Description: Forces cart to remove certain items if other items are added
Version: 1.0
Author: Kathy Darling
Author URI: http://kathyisawesome.com
Requires at least: 4.1.0
Tested up to: 4.1.0

Copyright: © 2015 Kathy Darling.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html

*/



/**
 * The Main WC_Restrict_Item_in_Cart class
 **/
if ( ! class_exists( 'WC_Restrict_Item_in_Cart' ) ) :

class WC_Restrict_Item_in_Cart {


    /**
     * WC_Restrict_Item_in_Cart init
     *
     * @access public
     * @since 1.0
     */

    public static function init() { 

        // product meta
        add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'add_to_wc_metabox' ) );
        add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_wc_meta_box' ), 1, 2 );

        // validation - ensure product is never in the cart with other products
        add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'maybe_remove_items' ), 10, 3 );

    }

    /*-----------------------------------------------------------------------------------*/
    /* Product Write Panels */
    /*-----------------------------------------------------------------------------------*/


    /*
    * Add text inputs to product metabox
    * @since 1.0
    */
    public static function add_to_wc_metabox(){
        global $post;

        echo '<div class="options_group">';

        echo woocommerce_wp_checkbox( array(
            'id' => '_only_item_in_cart',
            'label' => __( 'Only Item In Cart' ) ,
            'description' => __( 'For special items that need to be purchased individually.' )
            )
        );

        echo '</div>';

    }


    /*
     * Save extra meta info
     * @since 1.0
     */
    public static function process_wc_meta_box( $post_id, $post ) {

        if ( isset( $_POST['_only_item_in_cart'] ) ) {
            update_post_meta( $post_id, '_only_item_in_cart', 'yes' );
        } else {
            update_post_meta( $post_id, '_only_item_in_cart', 'no' );
        }

    }


    /*-----------------------------------------------------------------------------------*/
    /* Check Cart for presence of certain items */
    /*-----------------------------------------------------------------------------------*/


    /**
     * When an item is added to the cart, remove other products
     * based on WooCommerce Subscriptions code
     */
    public static function maybe_remove_items( $valid, $product_id, $quantity ) {

        if ( self::is_item_special( $product_id ) && WC()->cart->get_cart_contents_count() > 0 ){

            self::remove_specials_from_cart();

        } 

        return $valid;
    }

    /*-----------------------------------------------------------------------------------*/
    /* Helper methods */
    /*-----------------------------------------------------------------------------------*/

    /*
     * I've added a custom field 'only_item_in_cart' on items on 'special' products
     * check for this field similar to how Subscriptions checks cart for subscription items
     */

    public static function check_cart_for_specials() {

        $contains_special = false;

        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( self::is_item_special( $cart_item['product_id'] ) ) {
                $contains_special = true;
                break;
            }
        }

        return $contains_special;
    }

    /**
    * Removes all special products from the shopping cart.
    */
    public static function remove_specials_from_cart(){

        foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
            if ( self::is_item_special( $cart_item['product_id'] ) ){
                WC()->cart->set_quantity( $cart_item_key, 0 );
                $product_title = $cart_item['data']->get_title();

                wc_add_notice( sprintf( __( '&quot;%s&quot; has been removed from your cart. Due to shipping calculations, it cannot be purchased in conjunction with other products.', 'wc_Restrict_Item_in_cart' ), $product_title ), 'error' );
            }

        }

    }

    /*
     * check if an item has custom field
     */
    public static function is_item_special( $product_id ){

        if ( 'yes' == get_post_meta( $product_id, '_only_item_in_cart', true ) ){ 
            return TRUE;
        } else {
            return false;
        }
    }

} //end class: do not remove or there will be no more guacamole for you

endif; // end class_exists check

// Launch the whole plugin
WC_Restrict_Item_in_Cart::init();
相关问题