允许管理员在前端重置 WooCommerce 产品库存

问题描述 投票:0回答:1

仅当管理员连接时,我才会在商店页面的产品上添加一个按钮。按下此按钮会将产品库存重置为零 (0)。有时,使用产品图片从网站上清理缺货产品比使用 SKU 搜索更容易。我基于 @LoicTheAztec 和 @Víctor González SO answer 编写了这段代码,以从这个意义上更新数据库表,它的工作方式就像一个魅力。下面是工作代码

function createStockZeroButton(){
    global $product;
    $theId = $product-> get_id();
    if(current_user_can( 'administrator' )){
        echo "<form action='' method='POST'>
                    <button type='submit' class='stockZeroButtonClass' name="."name"."$theId".">set Stock To 0 for id".$theId."</button>
             </form>";
        if(isset($_POST["name".$theId])){
            $con = mysqli_connect($server, $username, $password, $database);
            $database_request = "UPDATE wp_postmeta pm
            INNER JOIN wp_wc_product_meta_lookup pml
                ON pm.post_id =  pml.product_id
            SET pm.meta_value = '0', pml.stock_quantity = '0'
            WHERE pm.meta_key = '_stock' AND pm.post_id = '$theId'";
            $database_query = mysqli_query($con, $database_request);
            $database_request ="UPDATE wp_postmeta pm
                INNER JOIN wp_wc_product_meta_lookup pml
                    ON pm.post_id =  pml.product_id
                SET pm.meta_value = 'outofstock', pml.stock_status = 'outofstock'
                WHERE pm.meta_key = '_stock_status' AND pm.post_id = '$theId'";
            $database_query = mysqli_query($con, $database_request);
            $database_query = "INSERT IGNORE INTO wp_term_relationships (object_id, term_taxonomy_id)
            SELECT pml.product_id, (SELECT term_id FROM wp_terms WHERE slug = 'outofstock')
            FROM wp_wc_product_meta_lookup pml
            WHERE pml.stock_status = 'outofstock' AND pm.post_id = '$theId'";
            $database_query = mysqli_query($con, $database_request);
        }
    }
}



add_action('woocommerce_shop_loop_item_title','createStockZeroButton');

我现在想使用 $wpdb 类来做到这一点。我已经根据这个SOanswer尝试了下面的代码,但无法让它工作。

function createStockZeroButton(){
    global $product;
    $theId = $product-> get_id();
    if(current_user_can( 'administrator' )){
        echo "<form action='' method='POST'>
                    <button type='submit' class='stockZeroButtonClass' name="."name"."$theId".">set Stock To 0 for id".$theId."</button>
             </form>";
        if(isset($_POST["name".$theId])){
$update1 = $wpdb->query($wpdb->prepare("UPDATE wp_postmeta pm
            INNER JOIN wp_wc_product_meta_lookup pml
                ON pm.post_id =  pml.product_id
            SET pm.meta_value = '0', pml.stock_quantity = '0'
            WHERE pm.meta_key = '_stock' AND pm.post_id = '$theId'"));

$update2 = $wpdb->query($wpdb->prepare("UPDATE wp_postmeta pm
                INNER JOIN wp_wc_product_meta_lookup pml
                    ON pm.post_id =  pml.product_id
                SET pm.meta_value = 'outofstock', pml.stock_status = 'outofstock'
                WHERE pm.meta_key = '_stock_status' AND pm.post_id = '$theId'"));

$update3 = $wpdb->query($wpdb->prepare("INSERT IGNORE INTO wp_term_relationships (object_id, term_taxonomy_id)
            SELECT pml.product_id, (SELECT term_id FROM wp_terms WHERE slug = 'outofstock')
            FROM wp_wc_product_meta_lookup pml
            WHERE pml.stock_status = 'outofstock' AND pm.post_id = '$theId'"));
        }
    }
}

add_action('woocommerce_shop_loop_item_title','createStockZeroButton');

在这种情况下,我们如何使用 INNER JOIN 和 $wpdb 实现正确的查询?

也查看了官方文档链接,但在我的脑海中无法弄清楚。

php html wordpress woocommerce product
1个回答
0
投票

您应该更好地使用相关的 WooCommerce setter 方法。请尝试以下方法:

function product_zero_stock_admin_button(){
    global $product;
  
    if( wc_current_user_has_role('administrator') ) :

    $id = $product->get_id();
        
    echo '<form method="POST" action="">
        <button type="submit" class="reset-stock" name="reset-stock-'.$id.'">Set Stock To 0 for id '.$id.'</button>
    </form>';
    
    if( isset($_POST['reset-stock-'.$id]) ){
        $product->set_stock_quantity(0); // Reset stock quantity
        $product->set_stock_status('outofstock'); // Set stock status "Out of stock"
        $product->save(); // Sync and save data
    }
    endif;
}

add_action( 'woocommerce_shop_loop_item_title', 'product_zero_stock_admin_button' );

应该会更好。

© www.soinside.com 2019 - 2024. All rights reserved.