根据 WooCommerce 中另一种产品的库存状态确定是否可以购买一种产品

发布于 2025-01-14 16:42:59 字数 1403 浏览 2 评论 0原文

我有一个 WooCommerce 商店,有 3 种不同的产品(ID 6241、6242 和 6243),并且我试图仅在前一个产品缺货时才可以购买该产品。

归结为:

  • 虽然 ID 6241 有库存 > ID 6242 不可购买
  • 当 ID 6241 缺货时 > 当ID 6242 有库存时,ID 6242 变得可购买
  • > ID 6243 不可购买
  • 当 ID 6242 缺货时 ID 6243 变为可购买
  • 等等..

我成功地用 2 种不同的产品做到了这一点,但现在我正在尝试制作一个循环,以便我可以通过多个产品应用它(在最后,我应该有 6 种不同的产品)。

所以我写了一个循环来浏览我当前的 3 个产品:

add_filter('woocommerce_is_purchasable', '_is_stage_purchasable', 10, 2);
function _is_stage_purchasable($is_purchasable, $product) {
        $product_id = array(6241,6242,6243); //array with product IDs
        foreach($product_id as $id){ //loop through products
            if( $product->id==$id && $product->get_stock_quantity() == 0 ){ //if product = product N and product N stock = 0
                $value = $id + 1; //value = next product ID
                $product = new WC_Product( $value );
                return true; //set new product as purchasable
            }else{ //if product = product N and product N stock > 0
                $value = $id + 1; //value = next product ID
                $product = new WC_Product( $value );
                return false; //set new product as not purchasable
            }
        }      
}

但我面临一些问题。如果 6241 缺货,使用此代码将使产品 6242 和 6243 无法购买。我不明白为什么?有人可以给我一些关于这个问题的提示吗?

I have an WooCommerce shop with 3 different products (IDs 6241, 6242 and 6243) and I'm trying to make a product purchasable only if the previous product is out of stock.

Which comes down to:

  • While ID 6241 is in stock > ID 6242 is not purchasable
  • When ID 6241 is out of stock > ID 6242 become purchasable
  • While ID 6242 is in stock > ID 6243 is not purchasable
  • When ID 6242 is out of stock ID 6243 become purchasable
  • Et cetera..

I succeed to do that with 2 different products, but now I'm trying to make a loop so I can apply that through multiple products (at the end, I should have 6 different products).

So I write a loop to go through my 3 current products:

add_filter('woocommerce_is_purchasable', '_is_stage_purchasable', 10, 2);
function _is_stage_purchasable($is_purchasable, $product) {
        $product_id = array(6241,6242,6243); //array with product IDs
        foreach($product_id as $id){ //loop through products
            if( $product->id==$id && $product->get_stock_quantity() == 0 ){ //if product = product N and product N stock = 0
                $value = $id + 1; //value = next product ID
                $product = new WC_Product( $value );
                return true; //set new product as purchasable
            }else{ //if product = product N and product N stock > 0
                $value = $id + 1; //value = next product ID
                $product = new WC_Product( $value );
                return false; //set new product as not purchasable
            }
        }      
}

But I'm facing some issues. Using this code makes the products 6242 and 6243 not purchasable if 6241 is out of stock. I don't understand why? Could someone give me some hints on that issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-01-21 16:42:59

您的代码包含一些错误:

  • 没有必要循环遍历您的产品 ID 数组,因为钩子是为每个产品单独执行的
  • $product->id 被替换为 $product-> ;get_id() 自 WooCommerce 3.0+ 起
  • 您使用 $value = $id + 1 而它实际上是关于获取以前的产品对象,因此 -1 似乎是正确的方式
  • Via get_stock_status() 您可以确定先前产品的状态,并据此使当前产品可购买,

因此您得到:

// Is purchasable
function filter_woocommerce_is_purchasable( $purchasable, $product ) {
    // Array with product IDs
    $product_ids = array( 6241, 6242, 6243 );

    // Get current product ID
    $current_product_id = $product->get_id();

    // Only for specific products
    if ( in_array ( $current_product_id, $product_ids ) ) {
        // By default NOT purchasable
        $purchasable = false;

        // Calculate previous product ID
        $previous_product_id = $current_product_id - 1;

        // Get previous product object
        $previous_product = wc_get_product( $previous_product_id );
        
        // Is a WC product 
        if ( is_a( $previous_product, 'WC_Product' ) ) {
            // Get stock status
            if ( $previous_product->get_stock_status() == 'outofstock' ) {
                // Make purchasable
                $purchasable = true;
            }
        }
    }

    return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 );

Your code contains some mistakes:

  • It is not necessary to loop through your products ID's array, as the hook is executed separately for each product
  • $product->id is replaced by $product->get_id() since WooCommerce 3.0+
  • You use $value = $id + 1 while it is actually about obtaining the previous product object, so -1 seems the right way
  • Via get_stock_status() you can determine the status of the previous product and based on that make the current product purchasable

So you get:

// Is purchasable
function filter_woocommerce_is_purchasable( $purchasable, $product ) {
    // Array with product IDs
    $product_ids = array( 6241, 6242, 6243 );

    // Get current product ID
    $current_product_id = $product->get_id();

    // Only for specific products
    if ( in_array ( $current_product_id, $product_ids ) ) {
        // By default NOT purchasable
        $purchasable = false;

        // Calculate previous product ID
        $previous_product_id = $current_product_id - 1;

        // Get previous product object
        $previous_product = wc_get_product( $previous_product_id );
        
        // Is a WC product 
        if ( is_a( $previous_product, 'WC_Product' ) ) {
            // Get stock status
            if ( $previous_product->get_stock_status() == 'outofstock' ) {
                // Make purchasable
                $purchasable = true;
            }
        }
    }

    return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文