gpt4 book ai didi

wordpress - 在 WooCommerce 中只允许同一供应商的购物车中的产品

转载 作者:行者123 更新时间:2023-12-04 08:49:09 25 4
gpt4 key购买 nike

我在我的 functions.php 中写了一个函数我的 WoocCmmerce 商店中我的 child 主题的文件。add to cart 上的验证按钮检查购物车中的所有产品是否仅来自 1 个供应商。

  • 如果产品来自一个供应商,它将添加该产品。
  • 如果产品来自不同的供应商,则通知应显示警告。

  • 此功能无法正常工作,因为通知仅在我手动刷新页面时显示。它应该立即显示通知。谁能帮我?
    这是我的代码如下:
    add_action( 'wp_enqueue_scripts', 'martfury_child_enqueue_scripts', 20 );
    function martfury_child_enqueue_scripts() {
    wp_enqueue_style( 'martfury-child-style', get_stylesheet_uri() );
    if ( is_rtl() ) {
    wp_enqueue_style( 'martfury-rtl', get_template_directory_uri() . '/rtl.css', array(), '20180105' );
    }
    }

    add_action( 'woocommerce_add_to_cart_validation', function( $is_allow, $product_id, $quantity ) {
    $product = get_post( $product_id );
    $product_author = $product->post_author;

    //Iterating through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $cart_product_id = $cart_item['product_id'];
    $cart_product = get_post( $cart_product_id );
    $cart_product_author = $cart_product->post_author;
    if( $cart_product_author != $product_author ) {
    $is_allow = false;
    break;
    }
    }

    if( !$is_allow ){
    // We display an error message
    wc_add_notice( __( "Well, you already have some item in your cart. First checkout with those and then purchase other items!", "wcfm-ecogear" ), 'error' );

    return wp_redirect($woocommerce->cart->get_cart_url());
    }
    return $is_allow;

    }, 50, 3 );

    最佳答案

    似乎您应用的某些步骤是不必要的,这应该足够了

  • get_post() - 检索给定帖子 ID 或帖子对象的帖子数据
  • 通过代码中添加的注释标签解释

  • function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {       
    // Cart NOT empty
    if ( ! WC()->cart->is_empty() ) {
    // Retrieves post data given a post ID or post object.
    $product = get_post( $product_id );

    // Post author
    $product_author = $product->post_author;

    // Flag, by default false
    $flag = false;

    // Loop trough cart
    foreach( WC()->cart->get_cart() as $cart_item ) {
    // Get product ID
    $cart_product_id = $cart_item['data']->get_id();

    // Get post
    $cart_product = get_post( $cart_product_id );

    // Post author
    $cart_product_author = $cart_product->post_author;

    // Condition NOT equal
    if( $cart_product_author != $product_author ) {
    $flag = true;

    // Break loop
    break;
    }
    }

    // True
    if ( $flag ) {
    // Add notice
    wc_add_notice( __( 'Products in the cart have to come from only 1 vendor', 'woocommerce' ), 'error' );

    // NOT passed
    $passed = false;
    }
    }

    return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );

    关于wordpress - 在 WooCommerce 中只允许同一供应商的购物车中的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64165051/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com