ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

woocommerce.php (13626B)


      1 <?php
      2 
      3 require_once get_template_directory() . "/inc/woocommerce/index.php";
      4 
      5 function materialis_is_woocommerce()
      6 {
      7     return function_exists('is_woocommerce') && (is_woocommerce() || is_cart() || is_checkout() || is_account_page());
      8 }
      9 
     10 function materialis_is_woocommerce_product_page()
     11 {
     12     return function_exists('is_woocommerce') && is_product();
     13 }
     14 
     15 add_filter("inner_header_show_subtitle", function ($value) {
     16     $is_woocommerce = materialis_is_woocommerce();
     17 
     18     return $value && ! $is_woocommerce;
     19 });
     20 add_action('after_setup_theme', 'materialis_add_woocommerce_support');
     21 function materialis_add_woocommerce_support()
     22 {
     23     add_theme_support('woocommerce');
     24 
     25 
     26     /* WooCommerce support for latest gallery */
     27     if (class_exists('WooCommerce')) {
     28         add_theme_support('wc-product-gallery-zoom');
     29         add_theme_support('wc-product-gallery-lightbox');
     30         add_theme_support('wc-product-gallery-slider');
     31     }
     32 }
     33 
     34 function materialis_woocommerce_register_sidebars()
     35 {
     36 
     37     $woo_sidebars_defaults = array(
     38         'before_widget' => '<div id="%1$s" class="widget %2$s mdc-elevation--z5">',
     39         'after_widget'  => '</div>',
     40         'before_title'  => '<h5 class="widgettitle"><i class="mdi widget-icon"></i>',
     41         'after_title'   => '</h5>',
     42     );
     43 
     44     register_sidebar(array_merge(array(
     45         'name'          => esc_html__('WooCommerce Left Sidebar', 'materialis'),
     46         'id'            => "ope_pro_woocommerce_sidebar_left",
     47         'title'         => "'WooCommerce Left Sidebar",
     48     ), $woo_sidebars_defaults));
     49 
     50     register_sidebar(array_merge(array(
     51         'name'          => esc_html__('WooCommerce Right Sidebar', 'materialis'),
     52         'id'            => "ope_pro_woocommerce_sidebar_right",
     53         'title'         => "'WooCommerce Right Sidebar",
     54     ), $woo_sidebars_defaults));
     55 
     56 }
     57 
     58 add_action('widgets_init', 'materialis_woocommerce_register_sidebars');
     59 
     60 
     61 add_filter('woocommerce_enqueue_styles', 'materialis_woocommerce_enqueue_styles');
     62 
     63 function materialis_woocommerce_enqueue_styles($woo)
     64 {
     65     $version = materialis_get_version();
     66 
     67 
     68     $styles = array(
     69         'materialis-woo' => array(
     70             'src'     => get_template_directory_uri() . "/woocommerce.css",
     71             'deps'    => array('woocommerce-general'),
     72             'version' => $version,
     73             'media'   => 'all',
     74             'has_rtl' => false,
     75         ),
     76     );
     77 
     78     // wp_enqueue_style('fancybox', materialis_pro_uri( "/assets/css/jquery.fancybox.min.css") , array(), $version);
     79     // wp_enqueue_script('fancybox', materialis_pro_uri( "/assets/js/jquery.fancybox.min.js"), array("jquery"), $version);
     80 
     81     return array_merge($woo, $styles);
     82 }
     83 
     84 
     85 function materialis_woocommerce_get_sidebar($slug)
     86 {
     87     $is_enabled = get_theme_mod("materialis_woocommerce_is_sidebar_{$slug}_enabled", true);
     88 
     89     if ($is_enabled) {
     90         get_sidebar("woocommerce-{$slug}");
     91     }
     92 }
     93 
     94 function materialis_woocommerce_container_class($echo = true)
     95 {
     96     $class = array();
     97 
     98     $is_left_sb_enabled  = is_active_sidebar('ope_pro_woocommerce_sidebar_left');
     99     $is_right_sb_enabled = is_active_sidebar("ope_pro_woocommerce_sidebar_right");
    100     $sidebars            = intval($is_left_sb_enabled) + intval($is_right_sb_enabled);
    101 
    102     if (is_archive()) {
    103         $class = array("enabled-sidebars-{$sidebars}");
    104     }
    105 
    106     $class = apply_filters('materialis_woocommerce_container_class', $class);
    107 
    108     if ($echo) {
    109         echo implode(" ", $class);;
    110     }
    111 
    112     return implode(" ", $class);
    113 }
    114 
    115 
    116 function materialis_woocommerce_container_class_hide_title($classes)
    117 {
    118     if (materialis_is_woocommerce_product_page()) {
    119         $template = get_theme_mod("materialis_woocommerce_product_header_type", "default");
    120         if ($template == "default") {
    121             array_push($classes, "no-title");
    122         }
    123     }
    124 
    125     return $classes;
    126 }
    127 
    128 add_filter('materialis_woocommerce_container_class', 'materialis_woocommerce_container_class_hide_title');
    129 
    130 
    131 add_action('wp_enqueue_scripts', function () {
    132     $ver = materialis_get_version();
    133     wp_enqueue_script('materialis-woocommerce', get_template_directory_uri() . "/assets/js/woo.js", array('jquery'), $ver);
    134 });
    135 
    136 
    137 add_filter('materialis_header_title', function ($title) {
    138 
    139     if (materialis_is_page_template()) {
    140         if (is_archive() && materialis_get_current_template() === "woocommerce.php") {
    141             $title = woocommerce_page_title(false);
    142         }
    143     }
    144 
    145     return $title;
    146 });
    147 
    148 function materialis_navigation_sticky_attrs_always($atts)
    149 {
    150     if (materialis_is_woocommerce()) {
    151         $atts["data-sticky-always"] = 1;
    152     }
    153 
    154     return $atts;
    155 }
    156 
    157 add_action('materialis_before_header', function ($template) {
    158     if ($template == "small") {
    159         add_filter("materialis_navigation_sticky_attrs", "materialis_navigation_sticky_attrs_always");
    160     }
    161 });
    162 
    163 add_filter('materialis_header', 'materialis_get_header_woocommerce', 10, 2);
    164 
    165 function materialis_get_header_woocommerce($template)
    166 {
    167 
    168     global $post;
    169     $header = false;
    170 
    171     if ($post) {
    172         $header = get_post_meta($post->ID, 'materialis_post_header', true);
    173     }
    174 
    175     if ( ! $header) {
    176         $header = $template;
    177     }
    178 
    179     if (materialis_is_woocommerce()) {
    180         $setting = "woocommerce_header_type";
    181         if (materialis_is_woocommerce_product_page()) {
    182             $setting = "woocommerce_product_header_type";
    183         }
    184 
    185         $template = get_theme_mod($setting, "default");
    186         if ($template == "default") {
    187             $template = "";
    188         }
    189     }
    190 
    191     return $template;
    192 }
    193 
    194 
    195 add_filter('woocommerce_show_page_title', '__return_false');
    196 
    197 
    198 add_filter('woocommerce_cross_sells_total', 'materialis_cross_sells_product_no');
    199 
    200 function materialis_cross_sells_product_no($columns)
    201 {
    202     $result = get_theme_mod('woocommerce_cross_sells_product_no', 4);
    203 
    204     return absint($result);
    205 }
    206 
    207 
    208 add_action('woocommerce_before_shop_loop', 'materialis_woocommerce_cart_button', 5);
    209 
    210 function materialis_woocommerce_cart_button()
    211 {
    212 
    213     $fragments = opr_woo_cart_button(array());
    214     ?>
    215     <div class="cart-contents-content">
    216         <h4><?php echo __('Cart Content: ', 'materialis'); ?></h4>
    217         <?php echo $fragments['a.cart-contents']; ?>
    218         <?php woocommerce_breadcrumb(); ?>
    219     </div>
    220     <?php
    221 }
    222 
    223 add_filter('woocommerce_add_to_cart_fragments', 'opr_woo_cart_button');
    224 
    225 function opr_woo_cart_button($fragments)
    226 {
    227     global $woocommerce;
    228     ob_start();
    229     ?>
    230 
    231     <a class="cart-contents button" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php esc_attr_e('View your shopping cart', 'materialis'); ?>">
    232         <i class="mdi mdi-cart"></i>
    233         <?php
    234         echo
    235             /* translators: %d is number of items */
    236         sprintf(_n('%d item', '%d items', absint($woocommerce->cart->cart_contents_count), 'materialis'), absint($woocommerce->cart->cart_contents_count)); ?> - <?php echo wp_kses($woocommerce->cart->get_cart_total(), array(
    237             'span' => array(
    238                 'class' => array(),
    239             ),
    240         )); ?></a>
    241     <?php
    242     $fragments['a.cart-contents'] = ob_get_clean();
    243 
    244     return $fragments;
    245 }
    246 
    247 add_action('woocommerce_before_single_product_summary', 'woocommerce_breadcrumb', 5, 0);
    248 
    249 add_filter('materialis_override_with_thumbnail_image', function ($value) {
    250 
    251     global $product;
    252 
    253     if (isset($product)) {
    254         $value = get_theme_mod('woocommerce_product_header_image', true);
    255 
    256         $value = (intval($value) === 1);
    257 
    258     }
    259 
    260     return $value;
    261 });
    262 
    263 
    264 add_filter('materialis_overriden_thumbnail_image', function ($url) {
    265     global $post;
    266 
    267 
    268     if (function_exists('is_shop') && is_shop()) {
    269         $page_id = wc_get_page_id('shop');
    270         $url     = get_the_post_thumbnail_url($page_id);
    271     }
    272 
    273     return $url;
    274 });
    275 
    276 add_filter('woocommerce_rest_check_permissions', function ($permission, $context, $n, $object) {
    277 
    278     $nonce        = isset($_REQUEST['materialis_woocommerce_api_nonce']) ? $_REQUEST['materialis_woocommerce_api_nonce'] : '';
    279     $isNonceValid = is_materialis_woocommerce_api_key_valid($nonce);
    280     if ($isNonceValid && $context === "read") {
    281         {
    282             return true;
    283         }
    284     }
    285 
    286     return $permission;
    287 
    288 }, 10, 4);
    289 
    290 
    291 function materialis_woocommerce_query_maybe_add_category_args($args, $category, $operator = "IN")
    292 {
    293     if ( ! empty($category)) {
    294         if (empty($args['tax_query'])) {
    295             $args['tax_query'] = array();
    296         }
    297         $args['tax_query'][] = array(
    298             array(
    299                 'taxonomy' => 'product_cat',
    300                 'terms'    => array_map('sanitize_title', explode(',', $category)),
    301                 'field'    => 'id',
    302                 'operator' => $operator,
    303             ),
    304         );
    305     }
    306 
    307     return $args;
    308 }
    309 
    310 function materialis_woocommerce_query_maybe_add_tags_args($args, $tag, $operator = "IN")
    311 {
    312     if ( ! empty($tag)) {
    313         if (empty($args['tax_query'])) {
    314             $args['tax_query'] = array();
    315         }
    316         $args['tax_query'][] = array(
    317             array(
    318                 'taxonomy' => 'product_tag',
    319                 'terms'    => array_map('sanitize_title', explode(',', $tag)),
    320                 'field'    => 'id',
    321                 'operator' => $operator,
    322             ),
    323         );
    324     }
    325 
    326     return $args;
    327 }
    328 
    329 
    330 add_filter('body_class', 'materialis_wc_body_class', 20);
    331 
    332 function materialis_wc_body_class($classes)
    333 {
    334     global $post;
    335 
    336     if (in_array('woocommerce', $classes)) {
    337         $classes[] = 'page';
    338     }
    339 
    340 
    341     return $classes;
    342 }
    343 
    344 
    345 add_filter('wc_add_to_cart_message_html', function ($message) {
    346     if ('yes' !== get_option('woocommerce_cart_redirect_after_add')) {
    347         $message = str_replace("</a>", "</a><p>", $message);
    348     }
    349 
    350     $message .= "</p>";
    351 
    352     return $message;
    353 });
    354 
    355 
    356 function materialis_woocommerce_get_size($cols)
    357 {
    358     return (intval($cols) / 12 * 100);
    359 }
    360 
    361 function materialis_woocommerce_cols_css($sel, $cols)
    362 {
    363     $size = (100 / intval($cols));
    364 
    365     return "" .
    366            "$sel {" .
    367            "-webkit-flex-basis: $size%;" .
    368            "-moz-flex-basis: $size%;" .
    369            "-ms-flex-preferred-size: $size%;" .
    370            "flex-basis: $size%;" .
    371            "max-width: $size%;" .
    372            "}";
    373 }
    374 
    375 
    376 add_action('wp_enqueue_scripts', 'materialis_woocommerce_print_layout');
    377 function materialis_woocommerce_print_layout()
    378 {
    379     
    380     $style = "";
    381     if (materialis_can_show_cached_value('materialis-woo-inline-css')) {
    382         $style = materialis_get_cached_value('materialis-woo-inline-css');
    383         $style = "/* cached */\n{$style}";
    384     } else {
    385         
    386     $list = array(
    387         "list"       => array(
    388             "sel"     => ".woocommerce ul.products li.product:not(.in-page-section)",
    389             "desktop" => get_theme_mod('woocommerce_list_item_desktop_cols', 4),
    390             "tablet"  => get_theme_mod('woocommerce_list_item_tablet_cols', 2),
    391         ),
    392         "related"    => array(
    393             "sel"     => ".woocommerce.single-product .related .products li.product",
    394             "desktop" => get_theme_mod('woocommerce_related_list_item_desktop_cols', 4),
    395             "tablet"  => get_theme_mod('woocommerce_related_list_item_tablet_cols', 2),
    396         ),
    397         "upsell"     => array(
    398             "sel"     => ".woocommerce.single-product .upsells .products li.product",
    399             "desktop" => get_theme_mod('woocommerce_upsells_list_item_desktop_cols', 4),
    400             "tablet"  => get_theme_mod('woocommerce_upsells_list_item_tablet_cols', 2),
    401         ),
    402         "cross_sell" => array(
    403             "sel"     => ".woocommerce .cart-collaterals .cross-sells .products li.product",
    404             "desktop" => get_theme_mod('woocommerce_cross_sell_list_item_desktop_cols', 2),
    405             "tablet"  => get_theme_mod('woocommerce_cross_sell_list_item_tablet_cols', 2),
    406         ),
    407     );
    408 
    409     $style = "@media (min-width: 768px) {";
    410     foreach ($list as $key => $data) {
    411         $style .= "\n /** {$data['sel']} - {$data['tablet']} */\n";
    412         $style .= materialis_woocommerce_cols_css($data['sel'], $data['tablet']);
    413     }
    414     $style .= "}";
    415 
    416     $style .= "\n@media (min-width: 1024px) {";
    417     foreach ($list as $key => $data) {
    418         $style .= "\n /** {$data['sel']} - {$data['desktop']} */\n";
    419         $style .= materialis_woocommerce_cols_css($data['sel'], $data['desktop']);
    420     }
    421     $style .= "}";
    422         
    423         materialis_cache_value('materialis-woo-inline-css', $style);
    424     }
    425 
    426     wp_add_inline_style('materialis-woo', $style);
    427 }
    428 
    429 
    430 function materialis_compare_woocommerce_version($version, $operator)
    431 {
    432     if (class_exists('WooCommerce')) {
    433         global $woocommerce;
    434 
    435         return version_compare($woocommerce->version, $version, $operator);
    436     }
    437 
    438     return false;
    439 }
    440 
    441 if ( ! defined('MATERIALIS_MIN_WOOCOMMERCE_VERSION')) {
    442     define('MATERIALIS_MIN_WOOCOMMERCE_VERSION', apply_filters('memerize_min_woocommerce_version', '3.2.0'));
    443 }
    444 
    445 add_action('admin_notices', 'materialis_woocommerce_version_notice');
    446 
    447 function materialis_woocommerce_version_notice()
    448 {
    449     if (materialis_compare_woocommerce_version(MATERIALIS_MIN_WOOCOMMERCE_VERSION, '>')) {
    450         return;
    451     }
    452 
    453     ?>
    454     <div class="notice notice-alt notice-error notice-large">
    455         <h4><?php _e('WooCommerce version outdated!', 'materialis'); ?></h4>
    456         <p>
    457             <?php _e('You need to update your <strong>WooCommerce plugin</strong> to use it with the <strong>Materialis theme</strong>.', 'materialis'); ?> <br/>
    458             <?php _e('<strong>Materialis theme</strong> requires the WooCommerce plugin version to be at least: ', 'materialis') ?> <strong><?php echo MATERIALIS_MIN_WOOCOMMERCE_VERSION; ?></strong>
    459         </p>
    460     </div>
    461     <?php
    462 }