WC_Data_Store::load PHP 메소드

load() 공개 정적인 메소드

Loads a data store for us or returns null if an invalid store.
부터: 2.7.0
public static load ( string $object_type )
$object_type string Name of object.
    public static function load($object_type)
    {
        try {
            return new WC_Data_Store($object_type);
        } catch (Exception $e) {
            return null;
        }
    }

Usage Example

 /**
  * Handle adding variable products to the cart.
  * @since 2.4.6 Split from add_to_cart_action
  * @param int $product_id
  * @return bool success or not
  */
 private static function add_to_cart_handler_variable($product_id)
 {
     $adding_to_cart = wc_get_product($product_id);
     $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']);
     $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']);
     $missing_attributes = array();
     $variations = array();
     $attributes = $adding_to_cart->get_attributes();
     // If no variation ID is set, attempt to get a variation ID from posted attributes.
     if (empty($variation_id)) {
         $data_store = WC_Data_Store::load('product');
         $variation_id = $data_store->find_matching_product_variation($adding_to_cart, wp_unslash($_POST));
     }
     // Validate the attributes.
     try {
         if (empty($variation_id)) {
             throw new Exception(__('Please choose product options…', 'woocommerce'));
         }
         $variation_data = wc_get_product_variation_attributes($variation_id);
         foreach ($attributes as $attribute) {
             if (!$attribute['is_variation']) {
                 continue;
             }
             $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
             if (isset($_REQUEST[$taxonomy])) {
                 // Get value from post data
                 if ($attribute['is_taxonomy']) {
                     // Don't use wc_clean as it destroys sanitized characters
                     $value = sanitize_title(stripslashes($_REQUEST[$taxonomy]));
                 } else {
                     $value = wc_clean(stripslashes($_REQUEST[$taxonomy]));
                 }
                 // Get valid value from variation
                 $valid_value = isset($variation_data[$taxonomy]) ? $variation_data[$taxonomy] : '';
                 // Allow if valid or show error.
                 if ('' === $valid_value || $valid_value === $value) {
                     $variations[$taxonomy] = $value;
                 } else {
                     throw new Exception(sprintf(__('Invalid value posted for %s', 'woocommerce'), wc_attribute_label($attribute['name'])));
                 }
             } else {
                 $missing_attributes[] = wc_attribute_label($attribute['name']);
             }
         }
         if (!empty($missing_attributes)) {
             throw new Exception(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)));
         }
     } catch (Exception $e) {
         wc_add_notice($e->getMessage(), 'error');
         return false;
     }
     // Add to cart validation
     $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations);
     if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) {
         wc_add_to_cart_message(array($product_id => $quantity), true);
         return true;
     }
     return false;
 }
All Usage Examples Of WC_Data_Store::load