当产品 A 设置为产品 B 的交叉/追加销售时,也会自动将产品 B 设置为产品 A 的交叉/追加销售

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

在 WooCommerce 中:

  • 如果产品 A 添加到产品 B 的交叉/追加销售中,则产品 B 也应自动添加到产品 A 的交叉/追加销售中。
  • 如果产品 A 从产品 B 的交叉/追加销售中删除,则产品 B 也应自动从产品 A 的交叉/追加销售中删除。

下面是我到目前为止的代码,但它仍然不起作用。有人需要帮助吗?谢谢

add_action('save_post_product', 'auto_link_cross_sells', 20, 2);

function auto_link_cross_sells($post_id, $post) {
    // Prevent infinite loop using a transient
    if (get_transient('auto_cross_sell_processing')) {
        return;
    }

    // Get the current product's cross-sell IDs
    $cross_sell_ids = get_post_meta($post_id, '_crosssell_ids', true);

    // If there are no cross-sell IDs, exit the function
    if (empty($cross_sell_ids) || !is_array($cross_sell_ids)) {
        return;
    }

    // Set a transient to prevent an infinite loop
    set_transient('auto_cross_sell_processing', true, 10);

    // Loop through each cross-sell product ID
    foreach ($cross_sell_ids as $cross_sell_id) {
        // Check if the cross-sell product ID is valid
        if (!get_post($cross_sell_id)) {
            continue;
        }

        // Get the existing cross-sell IDs for the cross-sell product
        $existing_cross_sells = get_post_meta($cross_sell_id, '_crosssell_ids', true);
        if (!is_array($existing_cross_sells)) {
            $existing_cross_sells = [];
        }

        // If the current product is not already a cross-sell of the cross-sell product, add it
        if (!in_array($post_id, $existing_cross_sells)) {
            $existing_cross_sells[] = $post_id;
            update_post_meta($cross_sell_id, '_crosssell_ids', $existing_cross_sells);
        }
    }

    // Remove the transient to allow the function to run again later
    delete_transient('auto_cross_sell_processing');
}
php wordpress woocommerce
1个回答
0
投票

您没有使用正确的钩子,并且您的代码中缺少一些内容。你想做的事情比你想象的更复杂。

此外,从 WooCommerce 3 开始,您应该更好地使用

WC_Product
可用方法而不是 WordPress Post Meta 函数,因为 WooCommerce 使用自定义数据库表。

这里我们需要使用自定义字段来跟踪以前的历史记录,允许处理已删除的交叉销售IDS

尝试使用以下代码,该代码应该处理和互连产品交叉销售:

add_action( 'woocommerce_process_product_meta', 'process_interlinked_product_cross_sells' );
function process_inter_linked_product_cross_sells( $post_id ) {
    // Get current WC_Product object
    $product = wc_get_product( $post_id ); 

    // Get the current product cross-sell IDs
    $cross_sell_ids = (array) $product->get_cross_sell_ids();

    // Get current product cross-sell IDs previous history track
    $previous_cs_ids = (array) $product->get_meta('_previous_cs_ids');

    // Check differences from history
    $differences_1 = array_diff($cross_sell_ids, $previous_cs_ids); 
    $differences_2 = array_diff($previous_cs_ids, $cross_sell_ids);

    // Added IDS case
    if ( $differences_1 ) {
        // Loop through product cross-sell IDs (added)
        foreach ( $differences_1 as $cross_sell_id ) {
            // get the WC_Product object from current cross sell ID
            $_product        = wc_get_product( $cross_sell_id ); 

            // Get cross-sell IDs for the cross-sell product
            $_cross_sell_ids = (array) $_product->get_cross_sell_ids();

            if ( ! in_array( $post_id, $_cross_sell_ids  ) ) {
                $_cross_sell_ids[] = $post_id; // Add the initial current product Id to the array
                $_product->set_cross_sell_ids( $_cross_sell_ids ); // Set updated cross-sells Ids
                $_product->update_meta_data( '_previous_cs_ids', $cross_sell_ids ); // Update history
                $_product->save(); // Sync and save
            }
        }
    }

    // Removed IDS case
    if ( $differences_2 ) {
        // Loop through product cross-sell IDs (removed)
        foreach ( $differences_2 as $cross_sell_id ) {
            // get the WC_Product object from current cross sell ID
            $_product        = wc_get_product( $cross_sell_id ); 

            // Get cross-sell IDs for the cross-sell product
            $_cross_sell_ids = (array) $_product->get_cross_sell_ids();

            if ( ! in_array( $post_id, $_cross_sell_ids  ) ) {
                unset($_cross_sell_ids[$post_id]); // Remove the initial current product Id from the array
                $_product->set_cross_sell_ids( $_cross_sell_ids ); // Set updated cross-sells Ids
                $_product->update_meta_data( '_previous_cs_ids', $cross_sell_ids ); // Update history track
                $_product->save(); // Sync and save
            }
        }
    }

    // Update current product history track
    if ( $differences_1 || $differences_2 ) {
        // Update cross sell IDs registration history
        $product->update_meta_data( '_previous_cs_ids', $cross_sell_ids ); // Update history track
        $product->save();// Sync and save
    }
}

应该可以。

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