Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Created January 30, 2020 17:21
Show Gist options
  • Select an option

  • Save jacobdubail/6a187fbd5b7b3be39b81cf894dc03b77 to your computer and use it in GitHub Desktop.

Select an option

Save jacobdubail/6a187fbd5b7b3be39b81cf894dc03b77 to your computer and use it in GitHub Desktop.

Revisions

  1. jacobdubail created this gist Jan 30, 2020.
    38 changes: 38 additions & 0 deletions add_product.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?php
    $cart_update_string = "https://" . $store_domain . "/cart?cart=update&fcsid=" . $fcsid . "&output=json";

    // loop our matches and rebuild query
    foreach ( $matched_products as $key ) {

    $item = $cart_contents['items'][$key];
    $item_id = $item['id'];

    $index = $key+1;

    $options_array = array();

    foreach ( $item['options'] as $option ) {
    $options_array[$index . ':' . $option['name']] = $option['value'];
    }

    $product_array = array(
    $index . ':name' => $item['name'],
    $index . ':price' => $item['base_price'],
    $index . ':image' => $item['image'],
    $index . ':code' => $item['code'],
    $index . ':quantity' => $item['quantity'],
    $index . ':quantity_max' => $item['quantity_max'],
    $index . ':category' => $item['category'],
    $index . ':weight' => $item['weight_each'],
    $index . ':Has_Discount_Applied' => true,
    );

    $product_array = array_merge($product_array, $options_array);

    $cart_update_string .= http_build_query($product_array) . "&{$index}:" . $discount_string;

    }

    // re-add eligible products with discount
    $updated_cart = wp_remote_get($cart_update_string);
    $updated_cart_contents = json_decode( wp_remote_retrieve_body( $updated_cart ), true );