2016-02-15 2 views
0

Звучит странно для меня, чтобы спросить об этом, в woocommerce есть все равно, чтобы иметь конкретный продукт, который можно контролировать с помощью идентификатора продукта, чтобы иметь только один в корзине , поэтому в нашем сценарии они выбирают тот или другой, если они пытаются повторно добавить в корзину, он просто заменяет или свопит этот предмет в корзину. Наше основное изделие не будет зависеть от этого правила.Woocmmerce как разрешить только один товар в корзине определенного типа

ответ

0

I знаю, что я уже размещал это где-то, но не могу найти его, поэтому я просто отправлю его. Это полный плагин (поэтому добавьте его как файл в 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(); 
+0

Большое спасибо за вашу помощь, к сожалению, это позволяет этот специальный продукт в корзину за один раз, когда я добавить другие товары из магазина снимает специальный продукт, я уверен, что код здесь, что специальная продукт также может содержать простые продукты. –

+0

Я урезал код больше для моих потребностей, почти делает то, что мне нужно для этого. –

+0

* Когда элемент добавлен в корзину, удалить другие продукты *, основанный на коде WooCommerce подписок */ общественности статической функции maybe_empty_cart ($ действительно, $ product_id, $ количество) { если (само :: is_item_special ($ product_id) && WC() -> cart-> get_cart_contents_count()> 10000) { –

Смежные вопросы