0

在我的网站上,我使用 WooCommerce v3.1+ 和 Woocommerce 订阅。我的产品包含摄像机和订阅计划等项目。我想做的是:

  • 购买相机和订阅计划时,在购物车页面(结帐前),订阅计划的数量必须等于相机的数量,
  • 如果将另一台相机添加到购物车中,则必须出现错误消息……或类似的东西。

例如,如果我的购物车中有 2 种特定产品,如相机(数量 2)和订阅计划(数量 1),如果订阅计划数量与相机数量不匹配,客户将无法结帐。

我相信我必须修改functions.phpin child 主题。任何帮助,将不胜感激

查看示例图片


编辑:我在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');
    } 
} 

所以这行不通。

4

1 回答 1

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+ 上经过测试并且有效

于 2017-09-15T07:16:04.253 回答