• Resolved ilanb

    (@ilanb)


    Hi,

    In my woocommerce, products can be sold by multiples sellers.
    All product have the same author id (Admin)
    I use woocommerce_before_add_to_cart_button and woocommerce_add_cart_item_data
    to save the needed ID “pharmacie_id”

    add_action( 'woocommerce_before_add_to_cart_button', 'pharmizz_hidden_pharmaid' );
    function pharmizz_hidden_pharmaid()
    {
        $pharmacie_id = urldecode($_GET['pharmacie_id']);
        ?>
        <input type="hidden" id="pharmacieid_val" name="pharmacie_id" value="<? echo $pharmacie_id ?>">
        <?php
    }
    
    add_filter( 'woocommerce_add_cart_item_data', 'pharmizz_add_cart_item_data', 10, 2 );
    function pharmizz_add_cart_item_data( $cart_item_data, $product_id )
    {
        $cart_item_data['pharmacie_id'] = $_REQUEST['pharmacie_id'];
    
        if($_GET['pharmacie_id'] == "") {
            $cart_item_data['pharmacie_id'] = $_REQUEST['data-pharmacie-id'];
        }
        return $cart_item_data;
    }
    
    Now I need to change the product author by the saved Id (pharmacie_id)
    I tried to use woocommerce_checkout_create_order_line_item like this
    
    

    add_action( ‘woocommerce_checkout_create_order_line_item’, ‘parmizz_add_pharmaid_to_order_items’, 10, 4 );
    function parmizz_add_pharmaid_to_order_items( $item, $cart_item_key, $values, $order ) {
    //$values[‘author_id’] = $item[‘pharmacie_id’];
    //$item->add_meta_data(‘author’, $values[‘pharmacie_id’] );

    if ( empty( $values[‘pharmacie_id’] ) ) {
    return;
    }
    $item->add_meta_data(‘author_id’, $values[‘pharmacie_id’] );
    }

    But not work, How can I manage this ?

    Best,

    • This topic was modified 4 years, 4 months ago by ilanb.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ilanb

    (@ilanb)

    Actualy if I create custom add to cart like:

    add_action('woocommerce_add_to_cart', 'custom_action_add_to_cart', 20, 6);
    function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
        $pharmacie_id = urldecode($_GET['pharmacie_id']);
        global $wpdb;
        $wpdb->get_results($wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_author = {$pharmacie_id} WHERE ID = '%d'", $product_id));
    }

    Order works fine, but problem occur if another user try to add same product from other seller…

    Thread Starter ilanb

    (@ilanb)

    Tried to change change value when checkout process without success:

    add_action('woocommerce_checkout_process', 'pharmizz_checkout_process');
    function pharmizz_checkout_process() {
        global $wpdb;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
        {
            $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
            if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) )
            {
                $wpdb->get_results($wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_author = {$cart_item['pharmacie_id']} WHERE ID = '%d'", $cart_item['product_id']));
            }
        }
    }
    Thread Starter ilanb

    (@ilanb)

    Working with:

    function pharmizz_checkout_process() {
        global $wpdb;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $pdt_id = $cart_item['product_id'];
            $ph_id = $cart_item['pharmacie_id'];
            $wpdb->get_results($wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_author = '%d' WHERE ID = '%d'", $ph_id, $pdt_id));
        }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trying to change order author ID by custom cart_item_data’ is closed to new replies.