php代码段破坏了网站

DVJy

我的产品具有自定义meta 'wccaf_virtual_quantity'现在,我想计算并添加另一个自定义元'actual_stock'。'actual_stock'= =的stock-wccaf_virtual_quantity我正在尝试的代码破坏了我的网站,导致'The site is experiencing technical difficulties. Please check your site admin email inbox for instructions.'访问管理面板时出现错误但是当我禁用数据库中的代码并检查产品表中的时'actual_stock',我可以看到的值'actual_stock'已更新。这意味着代码可以正常工作,但是会破坏正在处理的站点。

我尝试将以下代码添加到中functions.php我正在使用“代码段”插件添加php代码段

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
);
$products_array = get_posts($args);

if (!empty($products_array)) {
// loop through each product
foreach ($products_array as $product)
{
    update_actual_stock($product->ID);
}
}

function update_actual_stock($post_id) {
$post_type = get_post_type($post_id);

if ($post_type == 'product') {
    $product = wc_get_product($post_id);

    $virtual_stock = get_post_meta( $post_id, 
'wccaf_virtual_quantity', true );
    $visible_stock = $product->get_stock_quantity();
    $actual_quantity = $visible_stock - $virtual_stock;   

    update_post_meta( $post_id, 'actual_stock',$actual_quantity);   
}
}

请检查我在做什么错。

为什么必须在每个请求上运行该功能?

当然,您的代码可能会杀死您的服务器,它会被管理员或前端的每个请求触发,并且它的查询和循环遍历所有帖子,然后更新所有产品帖子,

您应该将其挂在某处,例如创建/更新帖子时

结帐save_post函数

//Your function to update the meta
function update_actual_stock($post_id) {
    $post_type = get_post_type($post_id);

    if ($post_type == 'product') {
        $product = wc_get_product($post_id);

        $virtual_stock = get_post_meta( $post_id, 'wccaf_virtual_quantity', true );
        $visible_stock = $product->get_stock_quantity();
        $actual_quantity = $visible_stock - $virtual_stock;   
        update_post_meta( $post_id, 'actual_stock',$actual_quantity);   
    }
}


// hook it on 'save_post' action hook so it only updates meta of specific post if its updated/created
function _update_blabla_meta( $post_id ) {
    update_actual_stock($post_id)
}
add_action( 'save_post', '_update_blabla_meta' );

如果您需要在下订单后运行函数,则必须将其挂接woocommerce_checkout_order_processed,该操作上传递了三个参数do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );供您获取要更新的帖子

在此处检查代码https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#1120

编辑....

这应该可以实现您想要的,或者只是对其进行修改以适合您的需求;

//run meta update on products only after order is place
add_action( 'woocommerce_checkout_order_processed', function($order_id) {

    $order = wc_get_order( $order_id ); // get the order from ID
    $items = $order->get_items(); // get order items

    //Loop through order each items
    foreach ( $items as $item ) {

        $porduct_id = $item->get_product_id(); //get the product ID from order item

        $virtual_stock = get_post_meta( $porduct_id, 'wccaf_virtual_quantity', true ); // get your own meta value

        $visible_stock = get_post_meta( $porduct_id, '_stock', true ); // get the product current stock count

        $actual_quantity = $visible_stock - $virtual_stock;   

        update_post_meta( $porduct_id, 'actual_stock', $actual_quantity); // Update your own meta
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章