Last active
December 19, 2015 00:20
-
-
Save clifgriffin/5868339 to your computer and use it in GitHub Desktop.
This file contains 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 | |
add_action( 'acf/save_post', 'hb_shopp_card_variations', 20 ); | |
function hb_shopp_card_variations() { | |
gc_enable(); | |
if( ! isset($_REQUEST['fields']) ) return; | |
ini_set('memory_limit', '356M'); | |
set_time_limit(0); | |
global $pagenow, $Shopp, $wpdb; | |
// Only run if on the ACF options page | |
if ( $pagenow == 'admin.php' && $_GET['page'] == 'acf-options-pricing-settings' ) { | |
// Get Card Pricing | |
$card_prices = get_field( 'card_pricing', 'options' ); | |
if ( $card_prices ) { | |
$quantities = array(); | |
// Generate array of quantity options | |
foreach ( $card_prices as $key => $card_price ) { | |
$quantities[$key] = array( | |
'id' => $key + 1, | |
'name' => $card_price['card_pricing_quantity'], | |
'linked' => 'off' | |
); | |
} | |
$variant_shell = array(); | |
$variant_shell['v'] = array( | |
'1' => array( | |
'id' => 1, | |
'name' => 'Quantity', | |
'options' => $quantities | |
) | |
); | |
// Generate array of product IDs | |
$products = shopp_category_products( 47 ); | |
foreach ( $products as $product ) { | |
//echo "Starting product $product->id \n"; | |
$product_id = $product->id; | |
$existing_options = $wpdb->get_var("SELECT value FROM wp_shopp_meta WHERE parent=$product_id AND context='product' AND type='meta' AND name='options'"); | |
$existing_options = unserialize($existing_options); | |
$variant_shell_copy = $variant_shell; | |
$variant_shell_copy['a'] = $existing_options['a']; | |
print_r($existing_options['a']); die; | |
$digital_jpg_exists = false; | |
foreach($variant_shell_copy['a'] as $id => $addon) { | |
if($addon['name'] == 'Digital JPG') { | |
$digital_jpg_exists = true; | |
break; | |
} | |
} | |
end($variant_shell_copy['a']); | |
$last_key = key($variant_shell_copy['a']); | |
if( ! is_numeric($last_key) ) $last_key = 0; | |
if( has_term(62, 'shopp_category', $product->id) && !$digital_jpg_exists ) { | |
$variant_shell_copy['a'][$last_key + 1] = array( | |
'id' => $last_key + 1, | |
'name' => 'Digital JPG', | |
'options' => array( | |
'id' => $last_key + 2, | |
'name' => 'Yes' | |
) | |
); | |
} | |
$serialized_variants = serialize($variant_shell_copy); | |
$wpdb->query("DELETE FROM wp_shopp_price WHERE product=$product_id AND context='variation'"); | |
$wpdb->query("DELETE FROM wp_shopp_meta WHERE parent=$product_id AND context='product' AND type='meta' AND name='options'"); | |
$wpdb->query($wpdb->prepare("INSERT INTO wp_shopp_meta (parent, context, type, name, value, numeral, sortorder,created,modified) VALUES (%s, %s, %s, %s, %s, %s, %s, date(now()), date(now()))", $product_id, 'product', 'meta', 'options', $serialized_variants, '0.000000', '0')); | |
foreach ( $card_prices as $key => $card_price ) { | |
$weight_raw = $card_price['card_pricing_weight']; | |
$weight = array('weight' => $card_price['card_pricing_weight']); | |
$weight_meta = array('dimensions' => array('weight' => $card_price['card_pricing_weight'])); | |
$wpdb->query($wpdb->prepare("INSERT INTO wp_shopp_price (product, options, optionkey, label, context, type, sku, price, saleprice, weight, shipfee, stock, inventory, sale, shipping, tax, donation, sortorder, created, modified, dimensions, promoprice, cost, stocked, discounts) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, date(now()), date(now()), %s, %s, %s, %s, %s)", $product_id, $key + 1, ($key + 1) * 7001, $card_price['card_pricing_quantity'], 'variation', $card_price['card_pricing_type'], null, $card_price['card_pricing_price'], '0.000000', $weight_raw, '0.000000', 0, 'off', 'off', 'on', 'off', 'a:2:{s:3:"var";s:3:"off";s:3:"min";s:3:"off";}', $key + 1, serialize($weight), '0.000000', '0.000000', 0, null)); | |
$price_id = $wpdb->get_var("SELECT max(id) FROM wp_shopp_price WHERE product='$product_id'"); | |
$wpdb->query($wpdb->prepare("INSERT INTO wp_shopp_meta (parent, context, type, name, value, numeral, sortorder, created, modified) VALUES(%s,'price','meta','settings',%s, '0.000000', '0.000000', date(now()), date(now()))", $price_id, serialize($weight_meta))); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment