2015-04-24 102 views
3

我需要能夠有條件地將產品的一個添加到WooCommerce訂單/購物車中,如果購物車中的任何商品均來自一個特定的類別。 這是我嘗試過的代碼,但它鎖定了結帳和購物車頁面,返回沒有woocommerce信息,或任何後(沒有頁腳,沒有額外的頁面元素等....只是一切,直到WooCommerce數據前端,之後有一個大的空白空間)。請,如果有人能指出我正確的方向,它將非常感激!如果其他購物車商品屬於特定類別,請將產品添加到woocommerce購物車

下面是我在我functions.php文件:

 /** 
* Checks the cart to verify whether or not a product from a Category is in the cart 
* @param $category Accepts the Product Category Name, ID, Slug or array of them 
* @return bool 
*/ 
function qualifies_basedon_product_category($category) { 
    foreach(WC()->cart->cart_contents as $key => $product_in_cart) { 
     if(has_term($category, 'fee', get_id_from_product($product_in_cart, false))) { 
      return true; 
     } 
    } 
    // Return false in case anything fails 
    return false; 
} 

/** 
* Adds a specific product to the cart 
* @param $product_id Product to be added to the cart 
*/ 
function add_incentive_to_cart($product_id) { 
    // Check the cart for this product 
    $cart_id = WC()->cart->generate_cart_id($product_id); 
    $prod_in_cart = WC()->cart->find_product_in_cart($cart_id); 
    // Add the product only if it's not in the cart already 
    if(! $prod_in_cart) { 
     WC()->cart->add_to_cart($product_id); 
    } 
} 



add_action('woocommerce_check_cart_items', 'qualifies_for_incentive'); 
function qualifies_for_incentive() { 
    // Incentive product 
    $incentive_product_id = 506; 

    if(qualifies_basedon_product_category('fee')) { 
     add_incentive_to_cart($incentive_product_id); 
    } 
} 
+0

這與我試圖實現的'類似',但與產品,而不是費用.... http://stackoverflow.com/questions/26240591/add-a-fee-to-woocommerce-per-product-based-on-category 此外,在任何人建議之前,我已經購買了Woo Force Sells擴展,但問題就是它會爲該類別中的每個產品自動添加附加產品,而不僅僅是每個購物車一次......所以如果有另一種方法使用強制銷售來處理它,我也會對此敞開......只是需要以便能夠將售出物品的物品在購物車中出現的次數限制爲一次。 –

回答

1

你可以調查以下代碼 -

add_action('woocommerce_add_to_cart', 'my_woocommerce_add_to_cart', 10, 6); 

function my_woocommerce_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data){ 
    $incentive_product_id = 506; 
    $category = 'fee'; 
    $taxonomy = 'fee'; 

    if(has_term($category, $taxonomy, $product_id)){  
     $cart_id = WC()->cart->generate_cart_id($incentive_product_id); 
     $prod_in_cart = WC()->cart->find_product_in_cart($cart_id); 

     // Add the product only if it's not in the cart already 
     if(! $prod_in_cart) { 
      WC()->cart->add_to_cart($incentive_product_id); 
     } 
    } 
} 

希望這可以幫助你。

+0

非常感謝你。我試過了,但它給了我一個內部服務器錯誤... 在這一點上,我決定拋出這個想法,並以不同的方式接近它,但我真的很感謝你的努力,並花時間嘗試和幫助! –

+0

上面的代碼中有一個輸入錯誤,我錯過了第一個「IF」語句的右括號。現在這段代碼應該可以正常工作如果不行不通,你可以在這裏通知它,因爲我可能會學到一件新事物嗎?謝謝。 – maksbd19

0

到functions.php將這個:

function df_add_ticket_surcharge($cart_object) { 
global $woocommerce; 
$specialfeecat = 20763; // category id you want to check for 
$spfee = 0.00; // initialize special fee 
$spfeeperprod = 0.00; //special fee per product 
$found = $false; //required to make the code later only add the item once, also stop loops. 
$product_id = 38607; //product id of the one you want to add 

foreach ($cart_object->cart_contents as $key => $value) { 

    $proid = $value['product_id']; //get the product id from cart 
    $quantiy = $value['quantity']; //get quantity from cart 
    $itmprice = $value['data']->price; //get product price 

//check if the desired product is already in cart. 
if (sizeof($woocommerce->cart->get_cart()) > 0) { 
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      $_product = $values['data']; 
      if ($_product->id == $product_id) 
       $found = true; 
     } 
    } 

//check for the category and add desired product 
    $terms = get_the_terms($proid, 'product_cat'); //get taxonomy of the products 
    if ($terms && ! is_wp_error($terms)) : 
     foreach ($terms as $term) { 
      $catid = $term->term_id; 
      if($specialfeecat == $catid && ! $found) { 
       WC()->cart->add_to_cart(38607); 
      } 
     } 
    endif; 
    } 
} 
add_action('woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge'); 

我修改從現有代碼的代碼,這就是爲什麼spfee和spfeeperprod仍然在那裏,但你並不真的需要他們這就是爲什麼我它們設置爲0。後來我發現少了什麼,使這個代碼工作是這一部分:

if (sizeof($woocommerce->cart->get_cart()) > 0) { 
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      $_product = $values['data']; 
      if ($_product->id == $product_id) 
       $found = true; 
     } 
    } 

這種檢查要添加哪些被指定爲「$ PRODUCT_ID」項目的產品ID。希望這有幫助(我昨天掙扎了很多,但今天早上終於有了突破!)

相關問題