根据Woocommerce中的项目数量,将普通产品与订阅配对

时间:2017-09-14 20:12:28

标签: php wordpress woocommerce cart woocommerce-subscriptions

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

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

例如,如果我在购物车中有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');
    } 
} 

所以这不起作用。

1 个答案:

答案 0 :(得分: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+上测试并正常工作