Created
July 20, 2017 00:26
-
-
Save westcoastdigital/407537d19be1f2f9e905fe96c60d283c to your computer and use it in GitHub Desktop.
Table Creation and writing non working on live site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * Create Saved Orders Custom Table | |
| */ | |
| function woo_create_saved_orders_data_table() { | |
| global $wpdb; | |
| $charset_collate = $wpdb->get_charset_collate(); | |
| $table_name = $wpdb->prefix . 'woocommerce_saved_orders'; | |
| if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) { | |
| $sql = 'CREATE TABLE ' . $table_name . '( | |
| id int(10) UNSIGNED AUTO_INCREMENT, | |
| post_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| products_data LONGTEXT, | |
| cart_name LONGTEXT, | |
| user_id int(10), | |
| PRIMARY KEY (id) | |
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'; | |
| require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| dbDelta( $sql ); | |
| add_option( 'woo_saved_orders_db_version', '1.0'); | |
| } | |
| } | |
| add_action( 'init', 'woo_create_saved_orders_data_table', 1 ); | |
| /* | |
| * Creates The Save Cart Button | |
| */ | |
| function woo_save_cart_button() { | |
| global $woocommerce; | |
| $cart_contents = $woocommerce->cart->get_cart(); | |
| $account_url = get_permalink(get_option( 'woocommerce_myaccount_page_id' )); | |
| if (isset($_POST['submit'])) { | |
| global $woocommerce; | |
| global $wpdb; | |
| global $current_user; | |
| if (is_user_logged_in()) { | |
| $user_id = $current_user->ID; | |
| $cart_contents = $woocommerce->cart->get_cart(); | |
| $cart = serialize($cart_contents); | |
| $cartname = $_POST['cartname']; | |
| $table_name = $wpdb->prefix . 'woocommerce_saved_orders'; | |
| // Update database | |
| $wpdb->insert( $table_name, array( | |
| 'products_data' => $cart, | |
| 'cart_name' => $cartname, | |
| 'user_id' => $user_id | |
| ) ); | |
| } | |
| } | |
| echo '<div id="save-cart">'; | |
| echo '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">'; | |
| echo 'Cart Name: <input type="text" name="cartname">'; | |
| echo '<input type="submit" name="submit" value="Save Cart">'; | |
| echo '</form>'; | |
| echo '</div>'; | |
| ?> | |
| <?php | |
| } | |
| add_action( 'woocommerce_cart_collaterals', 'woo_save_cart_button'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment