2017-09-14 166 views
0

在我的網站上,我使用WooCommerce v3.1 +和Woocommerce訂閱。我的產品包含攝像機和訂閱計劃等項目。我想這樣做的是:基於Woocommerce中產品數量訂購的普通產品

  • 購買相機和訂閱計劃時,在購物車頁面(結帳前),認購計劃的數量必須等於攝像機的數量,
  • 如果另一個相機被添加到購物車中,必須出現錯誤按摩......或類似的東西。

例如,如果我的購物車中有2個特定產品,如相機(數量爲2)和訂購計劃(數量爲1),則客戶將無法結帳,如果訂購計劃數量不與相機數量相匹配。

我相信我必須修改兒童主題中的functions.php。任何幫助,將不勝感激

See example image


編輯:我加了一段在functions.php的代碼,但是這將在購物車中添加X2總筆數:

add_action('woocommerce_check_cart_items', 'wh_wc_minimum_order_amount'); 
function wh_wc_minimum_order_amount() { 
    $minimum = 2; 
    if (WC()->cart->get_cart_contents_count() % $minimum != 0) { 
     // wc_clear_notices(); 
     wc_add_notice(sprintf('<strong>Error: check product quantity</strong>', $minimum), 'error'); 
    } 
} 

所以這是行不通的。

+0

或許這將幫助:https://docs.woocommerce.com/document/minmax-quantities/(注意,這裏沒有人會編寫代碼你最好的選擇是嘗試一些東西,並詢問你碰到的具體問題。) – jdv

+0

我只想要一個提示/鏈接/ plugin.anything,可以提供幫助。謝謝 –

+0

我設法在functions.php中添加一段代碼,但是這會在購物車add_action('woocommerce_check_cart_items','wh_wc_minimum_order_amount')中添加x2項總數。 函數wh_wc_minimum_order_amount() { $ minimum = 2; (WC() - > cart-> get_cart_contents_count()%$ minimum!= 0) { // wc_clear_notices(); wc_add_notice(sprintf('錯誤:查看產品數量',$ minimum),'error'); } } –

回答

0

這是一個完整的解決方案,當正常產品計數與訂閱計劃不匹配時,將顯示自定義通知,輕輕提醒客戶。如果產品不符合您的要求,客戶將從結帳重定向到(避開結賬)

代碼(評論)

// Replacing add to cart button link on shop and archives 
add_filter('woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2); 
function replacing_add_to_cart_button($button, $product) { 
    if($product->is_type('variable-subscription') || $product->is_type('variable')) return $button; 

    $button_text = __('View product', 'woocommerce'); 
    $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>'; 
    return $button; 
} 

// Counting cart items (utility function) 
function counting_cart_items($subscriptions_count = 0, $products_count = 0, $wc_cart = '') { 
    $cart_obj = $wc_cart != '' ? $wc_cart : WC()->cart; 
    foreach($cart_obj->get_cart() as $cart_item){ 
     if ($cart_item['data']->is_type('subscription')) 
      $subscriptions_count += intval($cart_item['quantity']); 
     else 
      $products_count += intval($cart_item['quantity']); 
    } 
    return array('subscrip' => $subscriptions_count, 'product' => $products_count); 
} 

// Displaying Notification messages (utility function) 
function conditionally_display_notice($subscriptions_count, $products_count) { 
    $count = $subscriptions_count - $products_count; 
    if($subscriptions_count < $products_count) 
     $txt = sprintf(_n('%s subscription plan', '%s subscriptions plans', -$count, 'woocommerce'), -$count); 
    elseif($subscriptions_count > $products_count) 
     $txt = sprintf(_n('%s product', '%s products', $count, 'woocommerce'), $count); 
    if($subscriptions_count != $products_count) 
     wc_add_notice(sprintf(__("You need to add %s, to be able to checkout", "woocommerce"), $txt), "notice"); 
} 

// Checking and notifying when products are added to cart 
add_filter('woocommerce_add_to_cart_validation', 'conditionally_check_add_to_cart', 10, 3); 
function conditionally_check_add_to_cart($passed, $product_id, $quantity) { 
    $items_count = counting_cart_items(); // Counting cart items 
    $product = wc_get_product($product_id); // Get an instance of the WC_Product object 

    // Get the total live count 
    if($product->is_type('subscription')) 
     $items_count['subscrip'] += intval($quantity); 
    else 
     $items_count['product'] += intval($quantity); 

    // Notification messages 
    conditionally_display_notice($items_count['subscrip'], $items_count['product']); 
    return $passed; 
} 

// Conditionally checking and adding your subscription when a product is added to cart 
add_action('woocommerce_before_main_content', 'display_notice_on_shop_archives'); 
function display_notice_on_shop_archives() { 
    if(is_product()) return; 
    $items_count = counting_cart_items(); // Counting cart items 

    // Notification messages 
    conditionally_display_notice($items_count['subscrip'], $items_count['product']); 
    wc_print_notices(); 
} 
/* 
// Conditionally checking and adding your subscription when a product is added to cart 
add_action('woocommerce_add_to_cart', 'conditionally_check_add_to_cart', 10, 6); 
function conditionally_check_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { 
    $items_count = counting_cart_items(); // Counting cart items 
    $product = wc_get_product($product_id); // Get an instance of the WC_Product object 

    // Get the updated count 
    if($product->is_type('subscription')) 
     $items_count['subscrip'] += intval($quantity); 
    else 
     $items_count['product'] += intval($quantity); 

    // Notification messages 
    conditionally_display_notice($items_count['subscrip'], $items_count['product']); 
} 
*/ 
// Checking and validating when updating cart item quantities 
add_filter('woocommerce_update_cart_validation', 'conditionally_check_cart_update', 10, 4); 
function conditionally_check_cart_update($passed, $cart_item_key, $values, $updated_quantity) { 
    $items_count = counting_cart_items(); // Counting cart items 

    // Get the updated count 
    if($values['data']->is_type('subscription')) 
     $items_count['subscrip'] += intval(-$values['quantity'] + $updated_quantity); 
    else 
     $items_count['product'] += intval(-$values['quantity'] + $updated_quantity); 

    // Notification messages 
    conditionally_display_notice($items_count['subscrip'], $items_count['product']); 
    return $passed; 
} 

// Checking and validating when updating cart item quantities 
add_filter('woocommerce_cart_item_removed', 'conditionally_check_removed_cart_item', 10, 2); 
function conditionally_check_removed_cart_item($cart_item_key, $wc_cart) { 
    $items_count = counting_cart_items(0, 0, $wc_cart); // Counting cart items 

    // Notification messages 
    conditionally_display_notice($items_count['subscrip'], $items_count['product']); 
} 

// Checking and conditionally redirecting to shop page 
add_action('template_redirect', 'conditional_checkout_redirection'); 
function conditional_checkout_redirection(){ 
    if (is_checkout()) { 
     $items_count = counting_cart_items(); // Counting cart items 

     if ($items_count['subscrip'] != $items_count['product']) { 
      // redirecting to shop page 
      wp_redirect(get_permalink(woocommerce_get_page_id('shop'))); 
      exit(); // always exit 
     } 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

此代碼對Woocommerce 3+測試和工程

+0

謝謝先生!代碼工作完美 –