Пример #1
0
 /**
  * Creates a duplicate product of this product's data
  *
  * @author Jonathan Davis
  * @since 1.0
  * @version 1.2
  *
  * @return void
  **/
 public function duplicate()
 {
     $original = $this->id;
     $this->load_data();
     // Load everything
     $this->id = '';
     $this->name = $this->name . ' ' . __('copy', 'Shopp');
     $slug = sanitize_title_with_dashes($this->name);
     $this->slug = wp_unique_post_slug($slug, $this->id, $this->status, ShoppProduct::posttype(), 0);
     $this->created = '';
     $this->modified = '';
     $this->status = 'draft';
     // Set duplicated product to draft status
     $this->save();
     // Copy prices
     foreach ($this->prices as $price) {
         $Price = new ShoppPrice();
         $Price->copydata($price);
         $Price->product = $this->id;
         $Price->save();
         // Copy Price record meta entries
         $meta = array('donation', 'recurring', 'membership', 'dimensions');
         $priceline['settings'] = array();
         $settings = array();
         foreach ($meta as $name) {
             if (isset($price->{$name})) {
                 $settings[$name] = $price->{$name};
             }
         }
         shopp_set_meta($Price->id, 'price', 'settings', $settings);
         shopp_set_meta($Price->id, 'price', 'options', $price->options);
     }
     // Copy taxonomy assignments
     $terms = array();
     $taxonomies = get_object_taxonomies(self::$posttype);
     $assignments = wp_get_object_terms($original, $taxonomies, array('fields' => 'all_with_object_id'));
     foreach ($assignments as $term) {
         // Map WP taxonomy data to object meta
         if (!isset($term->term_id) || empty($term->term_id)) {
             continue;
         }
         // Skip invalid entries
         if (!isset($term->taxonomy) || empty($term->taxonomy)) {
             continue;
         }
         // Skip invalid entries
         if (!isset($terms[$term->taxonomy])) {
             $terms[$term->taxonomy] = array();
         }
         $terms[$term->taxonomy][] = (int) $term->term_id;
     }
     foreach ($terms as $taxonomy => $termlist) {
         wp_set_object_terms($this->id, $termlist, $taxonomy);
     }
     $metadata = array('specs', 'images', 'settings', 'meta');
     foreach ($metadata as $metaset) {
         if (!is_array($this->{$metaset})) {
             continue;
         }
         foreach ($this->{$metaset} as $metaobjects) {
             if (!is_array($metaobjects)) {
                 $metaobjects = array($metaobjects);
             }
             foreach ($metaobjects as $meta) {
                 $ObjectClass = get_class($meta);
                 $Meta = new $ObjectClass();
                 $Meta->copydata($meta);
                 $Meta->parent = $this->id;
                 $Meta->save();
             }
         }
     }
     // Duplicate summary (primarily for summary settings data)
     $Summary = new ProductSummary($original);
     $Summary->product = $this->id;
     $Summary->sold = $Summary->grossed = $Summary->stock = 0;
     $Summary->save();
     // Re-summarize product pricing
     $this->load_data(array('prices', 'summary'));
     // Duplicate (WP) post meta data
     foreach (get_post_custom($original) as $key => $values) {
         foreach ((array) $values as $value) {
             add_post_meta($this->id, $key, $value);
         }
     }
 }
Пример #2
0
/**
 * shopp_product_set_addon_options - Creates a complete set of addon product options on a specified product, by letting you
 * specify the set of options types, and corresponding options.  This function will create new addon options in the database and
 * will attach them to the specified product.
 *
 * @api
 * @since 1.2
 *
 * @param int $product (required) The product id of the product that you wish to add the addon options to.
 * @param array $options (Description...) A two dimensional array describing the addon options.
 * The outer array is keyed on the name of the option type (Framing, Matting, Glass, etc.)
 * The inner contains the corresponding option values.
 * Ex. $options = array( 'Framing' => array('Wood', 'Gold'), 'Glass' => array('Anti-glare', 'UV Protectant') );
 * @return array addon Price objects that have been created on the product.
 *
 **/
function shopp_product_set_addon_options($product = false, $options = array(), $summary = 'save')
{
    if (!$product || empty($options)) {
        shopp_debug(__FUNCTION__ . " failed: Missing required parameters.");
        return false;
    }
    $Product = new ShoppProduct($product);
    if (empty($Product->id)) {
        shopp_debug(__FUNCTION__ . " failed: Product not found for product id {$product}.");
        return false;
    }
    $Product->load_data(array('summary'));
    // clean up old variations
    $table = ShoppDatabaseObject::tablename(ShoppPrice::$table);
    db::query("DELETE FROM {$table} WHERE product={$product} AND context='addon'");
    $prices = array();
    $mapping = array();
    foreach ($options as $type => $opts) {
        foreach ($opts as $index => $option) {
            $addon = array($type => $option);
            $Price = new ShoppPrice();
            $Price->type = 'Shipped';
            $Price->product = $product;
            $Price->context = 'addon';
            $Price->sortorder = $index + 2;
            // default price sort order is 1, start at minimum 2 #2847
            list($Price->optionkey, $Price->options, $Price->label, $mapping) = $Product->optionmap($addon, $options, 'addon');
            $Price->save();
            shopp_set_meta($Price->id, 'price', 'options', $Price->options);
            $prices[] = $Price;
        }
    }
    $metaopts = shopp_product_meta($product, 'options');
    $metaopts['a'] = array();
    $i = 1;
    foreach ($options as $optname => $option) {
        if (!isset($metaopts['a'][$i])) {
            $metaopts['a'][$i] = array('id' => $i, 'name' => $optname, 'options' => array());
        }
        foreach ($option as $value) {
            $metaopts['a'][$i]['options'][$mapping[$optname][$value]] = array('id' => $mapping[$optname][$value], 'name' => $value, 'linked' => "off");
        }
        $i++;
    }
    shopp_set_product_meta($product, 'options', $metaopts);
    $Product->addons = "on";
    if ('save' == $summary) {
        $Product->sumup();
    }
    return $prices;
}